Click to See Complete Forum and Search --> : Endless loop for no good reason!!


Thraner
12-19-2002, 01:08 PM
Guys, I see no reason why this should go into an endless loop when the user enters a bad value.. Any ideas? I guess what I mean to say is, if it fails the IF statement, it just keeps repeating the error message, without asking the user for another input.



#include <iostream>
#include <string>
using namespace std;


void main()
{
//Title
cout << "Currency Conversion\n";

//Skip a line
cout << "\n";

/*Declare the variables, which later will contain the
equivalents to a US Dollar */
float auDollars = 0.0;
float euEuros = 0.0;
float gbPounds = 0.0;
float caDollars = 0.0;
float jaYen = 0.0;

//Declare a variable for a user to enter an amount of US Dollars
float userDollars = 0.0;
bool calculationDone = false;

//Declare a constant value for US Dollars
const float USDOLLARS = 1.0;

//Compute the equivalents to US Dollars
auDollars = float(USDOLLARS * 1.78380);
euEuros = float(USDOLLARS * 0.998301);
gbPounds = float(USDOLLARS * 0.636951);
caDollars = float(USDOLLARS * 1.55837);
jaYen = float(USDOLLARS * 124.527);

// Output the equivalents to a US Dollar
cout << "$1 US = " << auDollars << " Australian Dollars" << endl;
cout << "$1 US = " << euEuros << " European Euros" << endl;
cout << "$1 US = " << gbPounds << " Great British Pounds" << endl;
cout << "$1 US = " << caDollars << " Canadian Dollars" << endl;
cout << "$1 US = " << jaYen << " Japanese Yen" << endl;


//Error-check the amount of US Dollars the user has entered
while (calculationDone == false)
{
cout << "Please enter an amount of US Dollars" << endl;
userDollars = 0.0;
cin >> userDollars;

if (userDollars > 0)
{
//do and output calculations
cout << userDollars << " US Dollars = " << (userDollars * auDollars) << " Australian Dollars.";
calculationDone = true;
}
//end if
}
//end while

}

Frostburn
12-19-2002, 04:54 PM
I don't know all the in's and outs of iostream, using cin and cout, But what I'd reccomend is creating an if statement

Psuedo code:
string check;
read line from common in and place it into check;
if check is not a number set userDollars to 0;

peterlak
12-19-2002, 09:43 PM
Try makeing the following modicication to your code.

Originally posted by Thraner
Guys, I see no reason why this should go into an endless loop when the user enters a bad value.. Any ideas? I guess what I mean to say is, if it fails the IF statement, it just keeps repeating the error message, without asking the user for another input.



//Error-check the amount of US Dollars the user has entered
while (calculationDone == false)
{
cout << "Please enter an amount of US Dollars" << endl;
userDollars = 0.0;

do {
cin >> userDollars;
} while(isNumeric(userDollars));
/* your going to have to write the function isNumeric your self. */

if (userDollars > 0)
{
//do and output calculations
cout << userDollars << " US Dollars = " << (userDollars * auDollars) << " Australian Dollars.";
calculationDone = true;
}
//end if
}
//end while