473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 "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
5 1587
sjsn
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
357 100+
Man read how to post and learn to INDENT your code!!!
its realy hard to understand
Nov 11 '06 #3
momotaro
357 100+
plz explain clearly what u r expecting from ur prog!
Nov 11 '06 #4
momotaro
357 100+
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
sjsn
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

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

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
5
by: build | last post by:
G'day All, I have a problem with this loop. There are a number of .txt files in 'myPath'. tmpFile = Dir(myPath & "\*.txt") 'PROCESS FOLDER Do Until tmpFile = "" <lottsa code> <too much to...
11
by: ritterhaus | last post by:
Just a simple bit of code to toggle between two state at intervals... import time for i in range(4): print 'On' time.sleep(1) print 'Off' time.sleep(1) .... SHOULD toggle On and Off four...
12
by: reynoldscraigr | last post by:
Hi All, hope someone can see what wrong here I have the following function function RemoveMenuFromHoldArray(menuName) { var i = 0; for (i=0;i<=MenusToHoldOpen.length-1;i++) { if...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
63
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
15
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a...
2
by: d3vkit | last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies. I am using PHP5 with...
5
by: sgurukrupagmailcom | last post by:
Hi, I haven't come accross an elegant solution to a design problem that I show below. Have a look at the piece of code here: class Exc { Exc () { System.out.println ("Haribol"); }
1
by: JavaJon | last post by:
Hello, I'm Jon. I've recently picked up Java after using a "gimmick" programming language called GML ( Game Maker Language ). I've read a lot of tutorials and even a Java for Dummies *.pdf book....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.