943,704 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1381
  • C++ RSS
May 23rd, 2008
0

Issue with getline() and cin..

Expand Post »
Im trying to carry out input validation on a piece of code and have come across an issue.

Basically for the 1st input I used getline for the string and then for the 2nd input I use cin for the integer. I am aware you can convert the integer from a string with getline but I wish to use the validation underneath to cover for the fact that a user may enter a string instead of the required integer.

When this code runs the second revolution of the For Loop it fires out the first two requests without waiting for user input. I suspect its the clash with getline and cin but Im unsure how to resolve except for using getline which nullifies the validation code.

Any ideas?
If you need any more clarification on my long winded, tedious problem just ask.

Cheers.

c++ Syntax (Toggle Plain Text)
  1.  
  2. int main ()
  3. {
  4.  
  5. int n,x,numb;
  6. string mystr;
  7.  
  8. for (n=0; n<N_TEAMS; n++)
  9. {
  10. cout << "Enter Seeded Team League : ";
  11. getline (cin,seeded[n].stdetails.league, '\n');
  12. cin.clear();
  13.  
  14. cout << "Enter Seeded Team League Placing : ";
  15. cin >> seeded[n].stdetails.numb;
  16.  
  17. cin.ignore(numeric_limits<int>::max(), '\n');
  18.  
  19.  
  20.  
  21. if (!cin || cin.gcount() != 1)
  22. {
  23. cout << "Not a numeric value.";
  24. cin.ignore(numeric_limits<int>::max(), '\n');
  25. }
  26.  
  27. else
  28. {
  29. cout << "Your entered number: " << seeded[n].stdetails.numb <<endl;
  30. cin.ignore(numeric_limits<int>::max(), '\n');
  31. }
  32.  
  33.  
  34.  
  35. cout <<"\n";
  36. system("Pause");
  37. system("cls");
  38.  
  39.  
  40. }
  41.  
  42. for (n=0; n<N_TEAMS; n++)
  43. {
  44. cout << "League is " << seeded[n].stdetails.league << endl;
  45. cout <<"\n";
  46. cout << "Numb is " << seeded[n].stdetails.numb << endl;
  47. system("Pause");
  48. cout <<"\n";
  49. }
  50. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Run.[it] is offline Offline
57 posts
since Jun 2007
May 23rd, 2008
1

Re: Issue with getline() and cin..

You are screwing-up your gcount() by using ignore(). Get the count first, then ignore.

Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
May 23rd, 2008
0

Re: Issue with getline() and cin..

Click to Expand / Collapse  Quote originally posted by Duoas ...
You are screwing-up your gcount() by using ignore(). Get the count first, then ignore.

Hope this helps.
Yep you are spot on, cheers.

I was close to figuring it as I had placed cin.clear(); after the ignore and it started to work, wasnt sure why though and youve cleared (no pun intended) that up.

Thanks again! 'Solved'
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Run.[it] is offline Offline
57 posts
since Jun 2007
May 23rd, 2008
0

Re: Issue with getline() and cin..

It would be just as easy, and permit multi-digit numbers, to input as a string and convert it...
C++ Syntax (Toggle Plain Text)
  1. #include <algorithm>
  2. #include <cctype>
  3. #include <functional>
  4. #include <sstream>
  5. #include <string>
  6. using namespace std;
  7. int main()
  8. {
  9. string s;
  10. int n;
  11. cout << "Please enter a number> ";
  12. getline( cin, s );
  13. if (find_if( s.begin(), s.end(), not1(ptr_fun<int,int>(isdigit)) ) != s.end())
  14. {
  15. cout << "That's not a number.\n";
  16. }
  17. else
  18. {
  19. stringstream( s ) >> n;
  20. cout << "You entered the number " << n << endl;
  21. }
  22. cout << "Have a nice day!\n";
  23. return EXIT_SUCCESS;
  24. }
Last edited by Duoas; May 23rd, 2008 at 3:14 pm.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
May 23rd, 2008
0

Re: Issue with getline() and cin..

Interesting Duoas, I'll give that a shot, thanks again!
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Run.[it] is offline Offline
57 posts
since Jun 2007

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: Sometimes consts are a pain??
Next Thread in C++ Forum Timeline: problem with if-else





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


Follow us on Twitter


© 2011 DaniWeb® LLC