Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking a range of values

Status
Not open for further replies.

hawaiian1975

Programmer
Nov 7, 2003
26
US
I am working on a currency program, however when someone inserts a letter, my program goes crazy. Here is the code where I do error checking, however I can never get it to work right.

if(choice < '6'){
// Get amount to be converted
cout << "Enter amount to convert: ";
cin >> usAmount;
// Error checking for anything not numerical
if(usAmount >= 0 && usAmount <= 10000){
// Perform conversion
convertAmount = usAmount * curRate;
// Conversion output
// The following line formats the output to show a dollar format
cout << setiosflags(ios::showpoint | ios::fixed) << setprecision(2);
cout << "Rate as of today: " << usAmount << " of " << curName << " is ";
cout << convertAmount << " US Dollar(s)." << endl << endl;
}else{
cout << "\tError: Invalid Choice." << endl << endl;
bCont = false; // The loop will end upon invalid input
exit(1);
}
}else{
cout << "\tError: Invalid Choice." << endl << endl;
}
} while(bCont);
 
check cin.fail() for a 'true' value to know that an error has been discover while processing input, and 'false' if everything is good.

When you do it, do it right.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top