Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with cin >> ignoring input in a function

Status
Not open for further replies.

mirirom

Programmer
Jul 21, 2001
110
US
hi,

i'm sure this is a pretty basic question. basically i have a function that's called within a loop. at the end of the function, i've placed

char c;
cout << "blah blah blah";
cin >> c;
cin.ignore(80, '\n');

however, the cin >> doesn't wait for the user's input. i've tested to see what's in the istream by printing out the int equivelant of char c. it's -1 every time.

any thoughts on this? many thanks in advance

..:: mirirom ::..
 
It depends on the code that comes before your little snippet. If you have a simple program:
Code:
#include <iostream>
using std::cout;
using std::cin;

int main()
{
    char c;
    cout << "blah blah blah";
    cin >> c;
    cin.ignore(80, '\n');
    cout << static_cast<int>(c);
}
it should work fine. One possibility is that the input stream still has data in it. Try posting a small but complete and compilable example that shows the problem.
 
hi uolj,

thx for the reply. turns out the code was working fine. the function call in the loop was based on a conditional bool member, which at 2am i was trying to assign char data to.

after the bug fix, things are ok. thanks again...



..:: mirirom ::..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top