Issue with getline() and cin..

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

Join Date: Jun 2007
Posts: 57
Reputation: Run.[it] is an unknown quantity at this point 
Solved Threads: 1
Run.[it]'s Avatar
Run.[it] Run.[it] is offline Offline
Junior Poster in Training

Issue with getline() and cin..

 
0
  #1
May 23rd, 2008
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.

  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. }
.........scaricamento.........
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Issue with getline() and cin..

 
1
  #2
May 23rd, 2008
You are screwing-up your gcount() by using ignore(). Get the count first, then ignore.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 57
Reputation: Run.[it] is an unknown quantity at this point 
Solved Threads: 1
Run.[it]'s Avatar
Run.[it] Run.[it] is offline Offline
Junior Poster in Training

Re: Issue with getline() and cin..

 
0
  #3
May 23rd, 2008
Originally Posted by Duoas View Post
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'
.........scaricamento.........
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Issue with getline() and cin..

 
0
  #4
May 23rd, 2008
It would be just as easy, and permit multi-digit numbers, to input as a string and convert it...
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 57
Reputation: Run.[it] is an unknown quantity at this point 
Solved Threads: 1
Run.[it]'s Avatar
Run.[it] Run.[it] is offline Offline
Junior Poster in Training

Re: Issue with getline() and cin..

 
0
  #5
May 23rd, 2008
Interesting Duoas, I'll give that a shot, thanks again!
.........scaricamento.........
Reply With Quote Quick reply to this message  
Reply

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




Views: 1045 | Replies: 4
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC