By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,599 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,599 IT Pros & Developers. It's quick & easy.

Loop Problem

P: 2
Ok, I'm still new at this, so bear with me...
I have a question with regards to the code below:

# include <iostream>
# include <string>

using namespace std;

double test;

int main()
{

do
{
cout << "Test to see if a number is between 0 and 1000:" << endl;
cin >> test;
if (test >= 1000)
{
cout << "NUMBER IS GREATER THAN 1000!!!" << endl;
}
else if (test <= 0)
{
cout << "THE NUMBER IS LESS THAN 0!!!" << endl;
}
}
while ((test >= 1000) || (test <= 0));
cout << "The following number is between 0 and 1000: " << test << endl;
cout << endl;
cout << "Done" << endl;
return 0;

}


If the user "accidentally" enters a letter instead of a number, the program goes nuts!!!

How do I account this possible user entry error?

Thanks,

Terence
Nov 10 '06 #1
Share this Question
Share on Google+
5 Replies


P: 5
I'm dying on this one too... I've built a program that will contain the user in a loop if they enter an invalid value (any number less than 0), to get out of the loop they have to enter the correct value, or a letter... the letter will make my program go nuts too..!

Good question!
Nov 10 '06 #2

momotaro
100+
P: 357
Man read how to post and learn to INDENT your code!!!
its realy hard to understand
Nov 11 '06 #3

momotaro
100+
P: 357
plz explain clearly what u r expecting from ur prog!
Nov 11 '06 #4

momotaro
100+
P: 357
HINT! use this to handel the input errors
PS: its a hint u have to undes=rstand it then u can play with it...;)
Expand|Select|Wrap|Line Numbers
  1. int err=1;
  2. err=scanf("%d" &input);
  3. if err<=0 (here u can use a loop statement to ask the user to re enter his value!)
  4.  
Nov 11 '06 #5

P: 5
Run this and for the startingMiles value enter a letter, the program then continues to the end return 0; this is the incorrect response.
Then run the program again and for the startingMiles value enter a negative number. This is the correct response to the error...

The question is, how do you include characters in the invalid response?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using std::cout ;
  5. using std::cin ;
  6. using std::setprecision ;
  7. using std::ios;
  8.  
  9. int main()
  10. {
  11.  
  12. char cont    = ' ';    
  13.  
  14. do
  15.     {    
  16.     double startingMiles    =0;
  17.     double endingMiles        =0;
  18.     double milesDriven        =0;
  19.     double gallonsOfGas        =0;
  20.     double milesPerGallon    =0;
  21.  
  22. cout << "\n\n--------------- Gas Mileage Calculator -----------------\n\n" ;
  23.  
  24.     do
  25.     {
  26.         cout << "Enter the starting miles          : " ;
  27.         cin  >> startingMiles ;
  28.  
  29.             if ( startingMiles < 0 )
  30.             {
  31.                 cout << "\n" << startingMiles << " is an invalid value for starting miles. \n\n" ;
  32.             }
  33.     }while (startingMiles < 0);
  34.  
  35.     do
  36.     {
  37.         cout << "Enter the ending miles            : " ;
  38.         cin  >> endingMiles ;
  39.  
  40.             if ( endingMiles < 0 )
  41.             {
  42.                 cout << "\n" << endingMiles << " is an invalid value for ending miles. \n\n" ;
  43.             }
  44.  
  45.             if ( endingMiles < startingMiles )
  46.             {
  47.                 cout << "\nEnding miles " << endingMiles << " cannot be less than starting miles " << startingMiles << ". \n\n" ;
  48.             }
  49.  
  50.     }while (startingMiles < 0 || endingMiles < startingMiles);
  51.  
  52. cout << "Enter the gallons of gas purchased: " ;
  53. cin  >> gallonsOfGas ;
  54.  
  55.     if ( gallonsOfGas <= 0 )
  56.     {
  57.         cout << "\n" << gallonsOfGas << " is an invalid value for gallons of gas purchased. \n\n" ;
  58.         return 0 ;
  59.     }
  60.  
  61. milesDriven = endingMiles - startingMiles ;
  62.  
  63. milesPerGallon = milesDriven / gallonsOfGas ;
  64.  
  65. cout << setprecision( 2 ) << setiosflags(ios::fixed) << setiosflags( ios::showpoint ) ;
  66.  
  67.     cout << "\nBased on " << milesDriven << " miles driven and " 
  68.          << gallonsOfGas << " gallons of gas purchased, \nthe miles per gallon is " 
  69.          << milesPerGallon << "\n\n" ;
  70.  
  71. cout << "\nWould you like to do another calculation? (y/n): ";
  72. cin >> cont;
  73. cont = toupper (cont);
  74.  
  75. }while (cont == 'Y');
  76.  
  77.     return 0 ;
  78. }
Nov 11 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.