cin issue

I am working on a lengthy program (2000+ lines so far).
When the code enters a certain loop, it encounters the following segment:
1
2
3
4
5
6
do
{
	cout<< "What would you like to do?" <<endl;
	cin >> action;
// eventually this do..while loop is properly closed.

followed by a series of if statements to handle various possible inputs.(a sort of custom repeating switch loop for string action)
a similar segment has already been successfully implemented in a different method within the same header file. (proof of concept)

The Problem:
The program compiles perfectly, but while running it, when the code reaches this segment, the program does not halt and wait for the user to input a value for string action.
Does anyone have any ideas why this might happen?
I would post the full code, but this header alone is over 600 lines.
I have verified that the string has been properly declared (gave it an initial value and printed it to the screen after the apparently ignored 'cin>>action' statement.)

Thanks in advance for any help.
-Luki
You probably have junk left in your input buffer from other unformatted (>>) inputs. Either filter out the junk or don't use unformatted input.
I'd guess there is still data in cin's buffer. You can try
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );, but it may be solved by getting your strings with getline(cin,action); which will pull the whole string and not just a word. May be able to help more if you show a bit more code.
Last edited on
if i understand you correctly, you are saying that it is automatically taking in other information that was waiting in line.
This loop repeats infinitely, however. Wouldn't the program eventually run out of 'junk' to use as inputs?
When the code reaches this point, it falls to the default error message, and then re-enters the loop and repeats infinitely.
If action is an integer, it could be running into say, a character. This causes cin to go into a error state which would produce the results you are describing.
naraku-
What section of code do you think would be most helpful to display?
I am assuming you are looking for whatever comes immediately before this section
firedraco-
The error state I mentioned is one that I defined, analogous to the default case in a switch loop. The computer seems to have no idea that an error is occurring.
Also, as I stated, action is declared as a string. It should be able to handle whatever input it is given.
Last edited on
Lukipaela wrote:
What section of code do you think would be most helpful to display?
I am assuming you are looking for whatever comes immediately before this section

Yes, before and after the code you posted.

Using cin >> action could be a problem, as it is delimited by white space so the string "this is a string" passed to cin will set action to just "this". If you use getline(cin, action); you will get the entire string. I dont know if this is the cause of your issue but its worth a look.
Last edited on
The computer seems to have no idea that an error is occurring.


How do you know? Do you ask it?

Do you check the state of cin after an attempted extraction? If cin is in an error state when it's time to read in action, what does it matter what type action is?

http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.2
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.6


cire-
How do you know? Do you ask it?

This statement was meant to clarify that the error message that I have been getting infinite copies of was not generated automatically by c++. It was a message that I defined within the default case of the switch that i created.
I thought this was clear.

If cin is in an error state when it's time to read in action, what does it matter what type action is?

I am not certain that I understand the suggestion that was being made, about "junk" left in the cin>> buffer.
My understanding was that he meant that there was a queue of extraneous data that was being read into the variable 'action' and that was the source of my error.
The type of variable that 'action' is declared as matters because I should not get infinitely many repetitions of my error message (infinitely many repetitions of the switch inside the d0..while loop) unless the queue of data holds an infinite amount of whitespaces.
If it were an integer, it would be read out as a single string and only provide me with a single error message before allowing me to input my own value for 'action', because a string will capture all data up to a whitespace.

I hope this clarifies my statements.

EDIT: I think that this explanation would also be sufficient to explain why I dont think it is likely that I am encountering either error type that is discussed in the links that you provided. But Thank you for taking the time to dig them up and suggest them anyway.
Last edited on
naraku-
Using cin >> action could be a problem, as it is delimited by white space so the string "this is a string" passed to cin will set action to just "this". If you use getline(cin, action); you will get the entire string. I dont know if this is the cause of your issue but its worth a look.


The program is designed to operate using single word commands for precisely this reason. I realize that if this type of statement was used immediately prior to this point in the code,( say something like cin >> action0;) and the user input multiple words separated by whitespaces, ("this is a string") this cin >> action; segment would then take in the word "is" as its argument.
However, if this was the case, the program would generate my error message "Invalid input, please re-enter command". Then it would repeat the loop, and take in "a", generate an error, repeat, take in "string", generate an error, and then when it repeated it would await user input.
This is what i was saying to Cire just now. If there were extra tokens in the queue for cin, they would eventually work their way through, and, after a series of error messages, eventually allow user input again. This is not the case here, though. I get an infinite number of error messages.

I have also tried the following, as a method of debugging:
1
2
3
4
5
6
7
8
9
//pre-define the string action:
action= "Hello, World!";
//then enter the loop:
do
{
cout << "What would you like to do?" << endl;
cin >> action;
//then test what has been inserted into the variable action:
cout << action << endl; 


The output using this code is:
What would you like to do?
Hello, World!
Invalid input, please re-enter command

What would you like to do?
Hello, World!
Invalid input, please re-enter command

(this repeats infinitely)


So it appears that cin >> action; is being completely ignored by the program.
cin is probably in a fail state (as already stated), have you checked either of these calls?

Check these flags from the input stream before you use the extraction operator...
cin.good() cin.fail()

So it appears that cin >> action; is being completely ignored by the program.

Have you tried to step into the operator when you hit that line, you will see that mystate for the ios_base is set to some failure, because of this no extraction takes place and the call to the operator is returned without extracting anything.
Last edited on
clanmjc-
Testing shows that you are correct, cin is in the fail state.
However, my inexperience prevents me from understanding two main concepts-
What could cause this to happen?
How do I restore cin?

Thanks everybody for all of your help and suggestions.
Perhaps you'll want to revisit the links you dismissed earlier.
http://stackoverflow.com/questions/5864540/a-simple-question-about-cin

After looking at this and a few other pages, I think i have the hang of it. After a few corrections, everything is running smoothly.
Thanks again,
-Luki

EDIT: cire-
Your links did not aid in the solution of the problem. I read them carefully, and they were not directly involved with the problem i was having.
Last edited on
Your links did not aid in the solution of the problem. I read them carefully, and they were not directly involved with the problem i was having.


Actually the first link is the issue you were having. You weren't ever checking if cin was in an error state.
From the first link:
The problem with this code is that it lacks any checking to see if someone entered an invalid input character. In particular, if someone enters something that doesn't look like an integer (such as an 'x'), the stream std::cin goes into a "failed state," and all subsequent input attempts return immediately without doing anything


I'd say that was pretty much spot-on. The next section of the faq specifies a method to deal with it. Unsurprisingly, that same method is indicated in:

http://stackoverflow.com/questions/5864540/a-simple-question-about-cin
Last edited on
Topic archived. No new replies allowed.