Loop Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HectorBerlioz
    New Member
    • Oct 2006
    • 2

    #1

    Loop Problem

    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 "accidental ly" enters a letter instead of a number, the program goes nuts!!!

    How do I account this possible user entry error?

    Thanks,

    Terence
  • sjsn
    New Member
    • Oct 2006
    • 5

    #2
    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!

    Comment

    • momotaro
      Contributor
      • Sep 2006
      • 357

      #3
      Man read how to post and learn to INDENT your code!!!
      its realy hard to understand

      Comment

      • momotaro
        Contributor
        • Sep 2006
        • 357

        #4
        plz explain clearly what u r expecting from ur prog!

        Comment

        • momotaro
          Contributor
          • Sep 2006
          • 357

          #5
          HINT! use this to handel the input errors
          PS: its a hint u have to undes=rstand it then u can play with it...;)
          Code:
          int err=1;
          err=scanf("%d" &input);
          if err<=0 (here u can use a loop statement to ask the user to re enter his value!)

          Comment

          • sjsn
            New Member
            • Oct 2006
            • 5

            #6
            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?

            Code:
            #include <iostream>
            #include <iomanip>
            
            using std::cout ;
            using std::cin ;
            using std::setprecision ;
            using std::ios;
            
            int main()
            {
            
            char cont	= ' ';	
            
            do
            	{	
            	double startingMiles	=0;
            	double endingMiles		=0;
            	double milesDriven		=0;
            	double gallonsOfGas		=0;
            	double milesPerGallon	=0;
            	
            cout << "\n\n--------------- Gas Mileage Calculator -----------------\n\n" ;
            
            	do
            	{
            		cout << "Enter the starting miles          : " ;
            		cin  >> startingMiles ;
            
            			if ( startingMiles < 0 )
            			{
            				cout << "\n" << startingMiles << " is an invalid value for starting miles. \n\n" ;
            			}
            	}while (startingMiles < 0);
            
            	do
            	{
            		cout << "Enter the ending miles            : " ;
            		cin  >> endingMiles ;
            
            			if ( endingMiles < 0 )
            			{
            				cout << "\n" << endingMiles << " is an invalid value for ending miles. \n\n" ;
            			}
            
            			if ( endingMiles < startingMiles )
            			{
            				cout << "\nEnding miles " << endingMiles << " cannot be less than starting miles " << startingMiles << ". \n\n" ;
            			}
            
            	}while (startingMiles < 0 || endingMiles < startingMiles);
            
            cout << "Enter the gallons of gas purchased: " ;
            cin  >> gallonsOfGas ;
            
            	if ( gallonsOfGas <= 0 )
            	{
            		cout << "\n" << gallonsOfGas << " is an invalid value for gallons of gas purchased. \n\n" ;
            		return 0 ;
            	}
            
            milesDriven = endingMiles - startingMiles ;
            
            milesPerGallon = milesDriven / gallonsOfGas ;
            
            cout << setprecision( 2 ) << setiosflags(ios::fixed) << setiosflags( ios::showpoint ) ;
            
            	cout << "\nBased on " << milesDriven << " miles driven and " 
            		 << gallonsOfGas << " gallons of gas purchased, \nthe miles per gallon is " 
            		 << milesPerGallon << "\n\n" ;
            	
            cout << "\nWould you like to do another calculation? (y/n): ";
            cin >> cont;
            cont = toupper (cont);
            
            }while (cont == 'Y');
            
            	return 0 ;
            }

            Comment

            Working...