-
March 23rd, 2011, 01:43 AM
#1
Infinite loop help
Ok, so I've been trying to finish this program, but when I do things a certain way, I go through an infinite loop in which I am not sure how to stop or keep it from happening. The object of the program is to convert feet/inches into centimeters, provide exceptions on the datatypes so that a not numeric number or a negative number can be entered, then keep asking the user to input datatypes until they choose the option to close the program.
My problem happens ONLY when I enter a negative number in the sequence BEFORE I enter a character exception, this is where is throws me in the loop. But I do not get thrown in the loop if I enter the character first and then the negative number and then the normal numbers to close.
Can anyone identify the problem here? I've been reading the chapter in this book and trying to understand the examples but I see no fault in their explaining and my coding in comparison. I am sure its a user error and I over looked something.
Code:
#include <iostream>
using namespace std;
int main()
{
double feet = 0, inches = 0, centimeters;
string input = "ERROR: Input stream in fail state";
bool done = false;
cout << "Use this program to convert Feet and Inches into Centimeters." << endl; //Heading to explain usefulness of the program
cout << endl;
do //Does the following code until done = true when input is correct.
{
try
{
cout << "Enter amount of feet: "; //Asks for input of feet
cin >> feet; //User enters value to store in feet
cout << "Enter amount of inches: "; //Asks for input of inches
cin >> inches; //User enters value to store in inches
cout << endl;
cout << "Your current values are: " << feet << " feet, " << inches << " inches." << endl; //Outputs feet and inches
cout << endl;
//throws exceptions to be caught and errors to be found if any
if(inches < 0)
throw inches;
else if (feet < 0)
throw feet;
else if (!cin)
throw input;
//Calculation to convert Feet/Inches into Centimeters
inches = inches * 2.54; //One inch equals 2.54 centimeters; Multiplies user input by centimeter value.
feet = feet * 30.48; //One foot = 30.48 centimeters; Multiplies user input by centimeter value.
centimeters = feet + inches; //Adds the centimeter values together and stores in centimeters
cout << "Here is the conversion in centimeters: " << centimeters << " centimeters" << endl; //Output centimeters
cout << endl;
done = true;
}//End try
catch(double x)
{
cout << "You have entered a negative number. ERROR: "<< x << endl;
}//End feet catch
catch(string invalidInput)
{
cout << invalidInput << endl;
cin.clear();
cin.ignore(100,'\n');
}//End string catch
}//End do
while(!done);
system("pause");
return 0;
};//End Main
If you run the code, enter a negative in the first value and then a character, you will get thrown in the loop. If you run the character first and then the negative, no loop.. I'm so confused.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|