Newbie Question: Error Checking User Input

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

Join Date: Jun 2008
Posts: 3
Reputation: Koinutron is an unknown quantity at this point 
Solved Threads: 0
Koinutron Koinutron is offline Offline
Newbie Poster

Newbie Question: Error Checking User Input

 
0
  #1
Aug 4th, 2009
I'm a newbie to c++, and as I'm reading my textbook, I'm applying different rules to this program for converting gallons to liters and vice versa, My question is how can I check that the user input is numeric and not a string of characters? I've tried several things with isalpha and isdigit, but they didn't seem to work and the program just continued to return an infinite loop. I really appreciate any input on this.

Thanks!

  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int main()
  6. {
  7. const double GAL_CONVERSION = 3.78533;
  8. const double LIT_CONVERSION = .264;
  9. double gallons, litres;
  10. char ans;
  11. const char error_neg[] = "Unable to calculate negative volume, please enter a positive amount\n";
  12.  
  13.  
  14. cout << "\nWelcome to the Volume Converter!\n" << "Enter 0 to quit" <<endl;
  15.  
  16. do
  17. {
  18. cout <<"Please insert the number of gallons you would like to convert: ";
  19. cin >> gallons;
  20.  
  21. litres = gallons * GAL_CONVERSION;
  22.  
  23. if (gallons > 0)
  24. {
  25. cout << "You have " << litres << " litres " << "if you have " << gallons;
  26.  
  27. if (gallons == 1)
  28. cout << " gallon\n";
  29.  
  30. else
  31. cout << " gallons\n";
  32. }
  33.  
  34. if (gallons < 0)
  35.  
  36. cerr << error_neg;
  37. else;
  38.  
  39. } while (gallons != 0);
  40.  
  41.  
  42. cout << "Do you want to convert litres to gallons? (y/n): ";
  43. cin >> ans;
  44.  
  45. if (ans == 'y')
  46.  
  47. { do
  48. {
  49. cout << "please enter the number of liters you would like to convert: ";
  50. cin >> litres;
  51.  
  52. gallons = litres * LIT_CONVERSION;
  53.  
  54. if (litres > 0)
  55. {
  56. cout << "You have " << gallons << " gallons " << "if you have " << litres;
  57.  
  58. if (litres == 1)
  59. cout << " litre\n";
  60.  
  61. else
  62. cout << " litres\n";
  63. }
  64.  
  65. if (litres < 0)
  66.  
  67. cerr << error_neg;
  68.  
  69.  
  70.  
  71. }while (litres != 0);
  72. }
  73.  
  74. else;
  75.  
  76. cout << "Good-bye\a" << endl;
  77.  
  78.  
  79. return 0;
  80. }
Last edited by Koinutron; Aug 4th, 2009 at 4:31 pm. Reason: missed closing bracket in code paste
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Newbie Question: Error Checking User Input

 
0
  #2
Aug 4th, 2009
Get the input as a string then check each of the characters. If all is ok then convert that string to int (or other data type). Something along these lines.
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int gallons;
  9. std::string input;
  10. cout << "Enter gallons\";
  11. cin >> input;
  12. bool ok = true;
  13. for(int i = 0; i < input.length(); i++)
  14. {
  15. if( !isdigit(input[i]) )
  16. {
  17. cout << "Error\n";
  18. ok = false;
  19. }
  20. }
  21. if( ok == true )
  22. {
  23. stringstream str(input);
  24. str >> gallons;
  25. }
  26.  
Last edited by Ancient Dragon; Aug 4th, 2009 at 4:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,261
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 157
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso

Re: Newbie Question: Error Checking User Input

 
0
  #3
Aug 4th, 2009
if you don't want to get the user's input as a string you can
do the following (not tested) :

  1. int num = 0;
  2. cout<<"Number please : ";
  3. cin >> num;
  4.  
  5. while(!cin)//if input fails
  6. {
  7. cin.clear();
  8. while(cin.get() != '\n')
  9. continue;
  10. cout<<"\nInvalid Input\n";
  11. cout<<"Try again : ";
  12. cin >> num;
  13. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 3
Reputation: Koinutron is an unknown quantity at this point 
Solved Threads: 0
Koinutron Koinutron is offline Offline
Newbie Poster

Re: Newbie Question: Error Checking User Input

 
0
  #4
Aug 5th, 2009
Thank you very much for the replies. I'll give these a try today and report back with the results
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 3
Reputation: Koinutron is an unknown quantity at this point 
Solved Threads: 0
Koinutron Koinutron is offline Offline
Newbie Poster

Re: Newbie Question: Error Checking User Input

 
0
  #5
Aug 5th, 2009
At this point, passing the input as a string seems a bit above my level of experience, so I tried the other solution, and it absolutely worked (except in the case of both valid and invalid characters in the input *will work on that*)

Thank you!
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC