Hi,


printf and cout are not flushed before input is prompted at the command 
line.


This problem exists in chrome and firefox running on linux fedora 24. Also 
even running the a.out.js program in node, has the same problems. 


This example code works fine in native linux however with emscrpten nothing 
works as expected.  However using \n does improve the situation by showing 
the input prompt before waiting for keyboard input.


All systems and software are fully updated.This problem has been reported  
on this list since 2013.  I read all the suggestions on multiple posts and 
nothing has worked, short of re-writing in asynchronous javascript (which I 
have not attempted).


Here is the code I wrote to demonstrate that none of the suggestions that I 
found work:


Thanks in advance for any help.

++Regards, Grant


#include <iostream>
#include <string>
#include <stdio.h>

void get_input_cout_nl(std::string& ttyinput_ref) {
    std::cout << "Print hello world output via cout_nl? (y/n):\n";
    std::cin >> ttyinput_ref;
}

void get_input_cout_endl(std::string& ttyinput_ref) {
    std::cout << "Print hello world output via cout_endl? (y/n):" << 
std::endl;
    std::cin >> ttyinput_ref;
}

void get_input_cout_flush(std::string& ttyinput_ref) {
    std::cout << "Print hello world output via cout_flush? (y/n):" << 
std::flush;
    flush(std::cout);  // doesn't help
    std::cin >> ttyinput_ref;
}

void get_input_cout(std::string& ttyinput_ref) {
    std::cout << "Print hello world output cout? (y/n):";
    std::cin >> ttyinput_ref;
}

void get_input_printf_nl(std::string& ttyinput_ref) {
    printf("Print hello world output printf_nl? (y/n):\n");
    char temp[80];
    scanf("%s", temp);
    ttyinput_ref = temp;
}

void get_input_printf_flush(std::string& ttyinput_ref) {
    printf("Print hello world output printf? (y/n):");
    fflush(stdout);
    char temp[80];
    scanf("%s", temp);
    ttyinput_ref = temp;
}

void get_input_printf(std::string& ttyinput_ref) {
    printf("Print hello world output printf_nl? (y/n):");
    char temp[80];
    scanf("%s", temp);
    ttyinput_ref = temp;
}

int main()
{
    std::cin.tie(&std::cout); // does not change anything as far as I can 
tell.
    std::string ttyinput = {"default"};
    std::string yy = {"y"};
    for (int j = 6; j>=0; --j ) {   // one can run these tests in either 
order and get same result (I had thought that cout might mess up the stream 
for printf, so tested each first)
        switch (j) {
            case 0: get_input_cout_nl(      ttyinput); break;
            case 1: get_input_cout_endl(    ttyinput); break;
            case 2: get_input_cout_flush(   ttyinput); break;
            case 3: get_input_cout(         ttyinput); break;

            case 4: get_input_printf_nl(    ttyinput); break;
            case 5: get_input_printf_flush( ttyinput); break;
            case 6: get_input_printf(       ttyinput); break;
        }
        if (ttyinput == "y") {
            std::cout << "Hello World!" << std::endl;
        } else {
            std::cout << "output not run." << std::endl;
        }
    }
    return 0;
}


Output from node.js:
[grostig@localhost emsripten1]$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 
1.36.0
clang version 3.9.0 (https://github.com/kripken/emscripten-fastcomp-clang/ 
271ce598c3d1fe74efadc254f5be1b57edea9f41) (
https://github.com/kripken/emscripten-fastcomp/ 
61acfb230665464544f2e8db292f8999fc3c628c) (emscripten 1.36.0 : 1.36.0)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: 
/home/grostig/src/emsdk_portable/clang/fastcomp/build_master_64/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/6.1.1
Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/6.1.1
Candidate multilib: .;@m64 <https://github.com/m64>
Candidate multilib: 32;@m32 <https://github.com/m32>
Selected multilib: .;@m64 <https://github.com/m64>
INFO:root:(Emscripten: Running sanity checks)
[grostig@localhost emsripten1]$ 

[grostig@localhost emsripten1]$ emcc -std=gnu++11 main.cpp
[grostig@localhost emsripten1]$ node ./a.out.js 
y
Print hello world output printf_nl? (y/n):Hello World!
y
Print hello world output printf? (y/n):Hello World!
Print hello world output printf_nl? (y/n):
y
Hello World!
y
Print hello world output cout? (y/n):Hello World!
y
Print hello world output via cout_flush? (y/n):Hello World!
Print hello world output via cout_endl? (y/n):
y
Hello World!
Print hello world output via cout_nl? (y/n):
y
Hello World!







-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to emscripten-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to