cin not waiting for user input!!!

Hello,

since around 2hours im really fighting with the cin command.
It doesn't matter what exactly im trying, it's just not waiting for user input and keeps directly using 0 as "entered" value. What the hell is wrong with this code?:
1
2
3
4
int Value;
cout << "Enter a new value: " << endl;
cin >> Value;
cout << "The value has been set to: " << Value;
Last edited on
Does the cin have some unprocessed data before this step?
It's the endl you have after the first cout

Try this:
1
2
3
4
5
int Value;
cout << "Enter a new value: " << endl;
cin.ignore();
cin >> Value;
cout << "The value has been set to: " << Value;
or you could do
1
2
3
4
5
6
7
8
9
10
#include <stdlib.h>
string str, error;
int Value;
//cout << "Enter a new value: "; // I don't like doing this.
do { cout << "Enter a new value: " << flush;
 getline(cin,str);
 for(unsigned int i = 0; i<Value.size(); i++) if(isalpha(str[i])) error = "true"; else error = "false";
} while(error == "true");
Value = atoi(str.c_str());
cout << "The value has been set to: " << Value;




Last edited on
^ unnecessary complicated.

> It's the endl you have after the first cout
that makes no sense.

@OP: minimal code that does reproduce your problem
And input that you are testing with.
Last edited on
Topic archived. No new replies allowed.