Junk Input Causes the Program to Ignore "cin.ignore(1000, '\n');"

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 7
Reputation: LordoftheFly is an unknown quantity at this point 
Solved Threads: 0
LordoftheFly's Avatar
LordoftheFly LordoftheFly is offline Offline
Newbie Poster

Junk Input Causes the Program to Ignore "cin.ignore(1000, '\n');"

 
1
  #1
Oct 8th, 2008
I'm having trouble with this section my code and am hoping that you can help me diagnose and solve the problem. I'm not sure if it makes a difference, but I compile and run my code in Cygwin.

The problem:
At run time when the program asks for the user input I can input somethimg like: "Guybrush Threepwood Throe" and the code will run just fine, but if I type the same thing and then use the left arrow button to move the cursor back over the input to make a correction (for instance to change the third word of the input from "Throe" to "Three"), the program will ignore (or seem to ignore) all of my "cin.ignore(1000, '\n');" and "cin.getline(input1, 31, '\n');" commands. I'm aware that you're not technically supposed to use the arrow keys in Cygwin, but I'm trying to make my code fool-proof.

Why does this happen, and assuming the user will eventually use those arrow keys, how can I prevent the undesired side-effects?

  1. #include <cstdlib>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7. char input1[31];
  8. char default1[31] = " ";
  9. int success = 0;
  10. int tries = 0;
  11. while ((success != 0) && (tries < 4))
  12. {
  13. cout << "[By what name shall you be known? Max: 30 Characters]" << endl;
  14. cout << ":" << endl;
  15. // Point of possible conflict follows immediately
  16. cin.getline(input1, 31, '\n');
  17. if (((input1[0] > 64) && (input1[0] < 91)) || ((input1[0] > 96) && (input1[0] < 123)))
  18. success = 1; // Success = 1 if input1 starts with a letter.
  19. else
  20. {
  21. cout << "What kind of name doesn't begin with a letter?" << endl;
  22. cout << "(Type a name that starts with a letter.)" << endl;
  23. tries++;
  24. }
  25. }
  26.  
  27. if (tries >= 4)
  28. {
  29. char guybrush[31] = "Guybrush";
  30. strcpy (input1, guybrush);
  31. cout << "Fine. I'm going to assume your name is Guybrush." << endl;
  32. }
  33.  
  34.  
  35. cout << "Some text here..." << endl;
  36. cin.ignore(1000, '\n'); // Used as a pause function
  37. cout << "Some more text here..." << endl;
  38. cin.ignore(1000, '\n');
  39.  
  40. strcyp (input1, default1);
  41. cout << "Input command." << endl;
  42. cout << ":";
  43. cin.getline(input1, 31, '\n');
  44. return 0;
  45. }

Thank you for your input.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Junk Input Causes the Program to Ignore "cin.ignore(1000, '\n');"

 
0
  #2
Oct 8th, 2008
Excellent first post - code tags, environment, statement of problem and example code. Everyone else should learn from this!

> strcyp (input1, default1);
Does this compile?
Isn't it supposed to be strcpy() ?

> the program will ignore (or seem to ignore) all of my "cin.ignore(1000, '\n');" and
> "cin.getline(input1, 31, '\n');" commands.
Or they could be returning error.
Check the return result to see if this is the case.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 7
Reputation: LordoftheFly is an unknown quantity at this point 
Solved Threads: 0
LordoftheFly's Avatar
LordoftheFly LordoftheFly is offline Offline
Newbie Poster

Re: Junk Input Causes the Program to Ignore "cin.ignore(1000, '\n');"

 
0
  #3
Oct 8th, 2008
Originally Posted by Salem View Post
> strcyp (input1, default1);
Does this compile?
Isn't it supposed to be strcpy() ?
Sorry about this one. I had to retype the code on another computer to put it online; so no.. it probably wouldn't compile. Additionally, on line 14, a correction would have to be made to cause the program to go into that while loop.

It would be changed from:
while ((success != 0) && (tries < 4))
to
while ((success == 0) && (tries < 4))
> the program will ignore (or seem to ignore) all of my "cin.ignore(1000, '\n');" and
> "cin.getline(input1, 31, '\n');" commands.
Or they could be returning error.
Check the return result to see if this is the case.
I checked the value of input1 by adding the following code after the if-statement that starts on line 30.
  1. cout << "input1 =" << input1 << "!" << endl;
I experimented with different input and arrow key combinations, and came to some realizations.

1. I started with the standard input that I used for all of the debugging:
Guybrush Threepwood T2345
2. When I used any left-right combination that started with the left arrow, the output always came out the same (regardless of whether or not I replaced characters.):
  1. input1 =Guybrush Threepwood T234ome text here...
  2. Some more text here...
3. When I used any left-right combination that started with the right arrow, I got:
  1. input1 =Guybrush Threepwood T2345 ome text here...
  2. Some more text here...
I didn't bother with the up or down arrows because it made things way more complicated.

I'm sorry that this this getting more complicated, but I thank you for your patience and responses.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Junk Input Causes the Program to Ignore "cin.ignore(1000, '\n');"

 
0
  #4
Oct 9th, 2008
So if you have
  1. while ( cin.getline(input1, 31, '\n') ) {
  2. cout << "--" << input1 << "--" << endl;
  3. }
  4. if ( cin.fail() ) {
  5. cout << "Failed" << endl;
  6. }
  7. if ( cin.eof() ) {
  8. cout << "EOF" << endl;
  9. }
  10. if ( cin.bad() ) {
  11. cout << "bad, very bad" << endl;
  12. }
If you use an arrow key, what happens here?
- loops a few times, then prints one of the exit messages?
- loops a few times, printing various fragments of the edited input, then goes back to waiting.

http://www.cppreference.com/wiki/io/fail

To get out of the loop, press ctrl-d, and you should see EOF printed.

If the code keeps in the loop, but prints various fragments of the line in several attempts, you should be able to recover.

Oh, and please find a way of copying the code accurately (pen drive, email yourself, whatever). It'll save a lot of confusion in the long run.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 7
Reputation: LordoftheFly is an unknown quantity at this point 
Solved Threads: 0
LordoftheFly's Avatar
LordoftheFly LordoftheFly is offline Offline
Newbie Poster

Re: Junk Input Causes the Program to Ignore "cin.ignore(1000, '\n');"

 
0
  #5
Oct 10th, 2008
Originally Posted by Salem View Post
  1. while ( cin.getline(input1, 31, '\n') ) {
  2. cout << "--" << input1 << "--" << endl;
  3. }
  4. if ( cin.fail() ) {
  5. cout << "Failed" << endl;
  6. }
  7. if ( cin.eof() ) {
  8. cout << "EOF" << endl;
  9. }
  10. if ( cin.bad() ) {
  11. cout << "bad, very bad" << endl;
  12. }
This is a clever test. Thanks for this insight.
If you use an arrow key, what happens here?
- loops a few times, then prints one of the exit messages?
- loops a few times, printing various fragments of the edited input, then goes back to waiting.
When I use the arrow keys it prints "Failed." And according to the link "Once set, the fail state will make all other operations on the stream fail instantly, until the error state is cleared with the clear function." (http://www.cppreference.com/wiki/io/fail)

So it sounds like the fail flag is being tripped, would which apparently cause my program to go haywire. I'm looking around and can't seem to find any information on the "clear function."
Oh, and please find a way of copying the code accurately (pen drive, email yourself, whatever). It'll save a lot of confusion in the long run.
I shall try. Again, thanks for your patience and input.
Last edited by LordoftheFly; Oct 10th, 2008 at 1:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,755
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Junk Input Causes the Program to Ignore "cin.ignore(1000, '\n');"

 
0
  #6
Oct 10th, 2008
http://www.cppreference.com/wiki/io/clear

link to the clear() method of input stream. It's under the C++ I/O link on the main page, then click on clear.

This site deserves to be on your list of favorites, if it isn't already.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Junk Input Causes the Program to Ignore "cin.ignore(1000, '\n');"

 
0
  #7
Oct 10th, 2008
Make a function like this (with iostream included and the std namespace):

  1. ostream & Flush (ostream & myStream)
  2. {
  3. myStream.clear();
  4. myStream.ignore(myStream.rdbuf()->in_avail());
  5. return myStream;
  6. }
Then call it for whatever stream you want to clear, and it supports chaining. ie Flush(cin) >> iInput;
Last edited by skatamatic; Oct 10th, 2008 at 4:23 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 7
Reputation: LordoftheFly is an unknown quantity at this point 
Solved Threads: 0
LordoftheFly's Avatar
LordoftheFly LordoftheFly is offline Offline
Newbie Poster

Re: Junk Input Causes the Program to Ignore "cin.ignore(1000, '\n');"

 
0
  #8
Oct 11th, 2008
Originally Posted by Lerner View Post
http://www.cppreference.com/wiki/io/clear
This site deserves to be on your list of favorites, if it isn't already.
It definitely is now.

Originally Posted by skatamatic View Post
Make a function like this (with iostream included and the std namespace):

  1. ostream & Flush (ostream & myStream)
  2. {
  3. myStream.clear();
  4. myStream.ignore(myStream.rdbuf()->in_avail());
  5. return myStream;
  6. }
Then call it for whatever stream you want to clear, and it supports chaining. ie Flush(cin) >> iInput;
I actually had a lot of trouble trying to figure out how exactly to implement the code, and it took three webpages and this thread for the information to make a modicum of sense to me. I came up with this seemingly simple function:
  1. void clear (ios::iostate flags = ios::goodbit);
which I call using the following segment of code, which is partially borrowed from Salem's code:
  1. if ( cin.fail() )
  2. {
  3. cout << "Failed." << endl;
  4. cout << "Please do not use the arrow keys." << endl;
  5. cin.clear();
  6. }

The sources that proved helpful to me are listed here:
http://www.cppreference.com/wiki/io/clear
http://www.cplusplus.com/reference/i...e/iostate.html
http://www.cplusplus.com/reference/i...ios/clear.html

It seems that this problem is solved, so thank you to everyone who contributed time and information to my code.
-- LordoftheFly
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC