Function using cin
I am using a function which can be called many times. Inside the function I am using cin to take a maximum off 255 characters type char. However, sometimes when the function is called more than once, debugging has shown that it is just going past the cin and not waiting for input.
Can anyone tell me why this is happening?
Many thanks, Sub.
>Can anyone tell me why this is happening?
I'd guess it's because there is already input left in the buffer when the function is called. But without seeing any code this is just speculation. What library functions are you using to obtain input?
Without seeing the code all i can give is some general advice.
cin >> leaves the delimiter in the stream
cin.getline () does not leave the delimiter in the stream.
this is the most common cause of this error. Using cin>> and then next using cin.getline() which appears to have taken no input. This is because the previous cin>> left the delimiter in the stream and getline takes that as its input. Use cin.ignore(80,'\n') after cin>> but before cin.getline(). Whatever the cause you have some chars left in the stream that need clearing.
so basically the variable that will be used again has to be cleared before cin?
The variable declaration is: char Answer[MAX_BUFFER];
where MAX_BUFFER is 255
The input is taken form the user in the following way:
cin >> Answer;
I can't understand why c++ has to have the variable cleared each time you want to use it. Seems a bit inefficent.
no you're not understanding us.
cin is an object. Inside that object is a buffer called a streambuf.
It is that streambuf that is internal to cin that needs clearing.
show us your code if you want more help. If we cant see the problem we cant help you fix it.
Are you trying to get input seperated by whitespace with operator >> ? If so anything entered after a whitespace character will be left to be picked up by the next call to an input function.
As I said above, unless you post some code we could be guessing at your problem for some time.
cin >> Answer;
if(strlen(Answer) == 1 && Answer== 'y')
i = 0; // Loop is reset to check new passwords
else //User has selected no or invalid response returning back to main menu
i = MAX_LEVEL;//exit loop
I was using cin.getline but i had to press enter twice, then once , then wasn't taking input upon pressing enter; loads of weird stuff.