getchar function working differently after Ubuntu 19.04 update?

61 views Asked by At

I am writing a program using both Assembly and c++ to collect user-inputted data. In my c++ file, I read in user numbers with a cin loop:

    while (cin >> input)
    {
       myArray.push_back(input);
    }

    cin.clear();

The user is also told to type CTRL-D when finished. In my linked Assembly file I ask the user Are these numbers correct (y/n)?

Previously before updating to Ubuntu 19.04, I would do cin.clear() as noted above after reading in the inputs so when I performed call getchar back in my Assembly file, I could actually receive input from the user. However, after updating Ubuntu, my program only outputs the question if the numbers are correct and does not read in ’y/n‘.

I have attempted cin.ignore() as well after my cin loop but that does not seem to fix my issue. I can only assume that call getchar in my Assembly file is reading some newline character rather than taking in a user input, but no matter the fixes I have found online, nothing seems to accept it.

For further clarification I am compiling/linking with g++ in my terminal and assembling with nasm. I can provide further code if needed. In Assembly, I am simply doing:

    mov rax, 0
    mov rdi, correctnumbersprompt ;This holds the string asking y/n
    call printf
    call getchar
1

There are 1 answers

0
lawgik On BEST ANSWER

I have found a fix.

To clear the stream state properly, I had to clear it from both my C++ file with cin.clear(); and within my Assembly file, I cleared the stream by performing:

extern stdin
extern clearerr

mov rax, 0
mov rdi, [stdin]
call clearerr

This made it so I could successfully perform call getchar to grab user input after using CTRL+D to end an input stream.