943,823 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2363
  • C++ RSS
Oct 8th, 2008
1

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

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 32
Solved Threads: 0
Newbie Poster
LordoftheFly is offline Offline
7 posts
since Oct 2008
Oct 8th, 2008
0

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

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 8th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by Salem ...
> 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))
Quote ...
> 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.
C++ Syntax (Toggle Plain Text)
  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.):
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 32
Solved Threads: 0
Newbie Poster
LordoftheFly is offline Offline
7 posts
since Oct 2008
Oct 9th, 2008
0

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

So if you have
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 10th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by Salem ...
C++ Syntax (Toggle Plain Text)
  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.
Quote ...
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."
Quote ...
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.
Reputation Points: 32
Solved Threads: 0
Newbie Poster
LordoftheFly is offline Offline
7 posts
since Oct 2008
Oct 10th, 2008
0

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

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 10th, 2008
0

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

Make a function like this (with iostream included and the std namespace):

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Oct 11th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by Lerner ...
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.

Click to Expand / Collapse  Quote originally posted by skatamatic ...
Make a function like this (with iostream included and the std namespace):

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 32
Solved Threads: 0
Newbie Poster
LordoftheFly is offline Offline
7 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Segmentation fault problem
Next Thread in C++ Forum Timeline: Structs to Functions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC