I am working on this currency program, so far it works however whenever a user is prompted to enter an amount and enters a bunch of letters the program goes crazy. For example a user enters "afbajkdbak" - program goes in a infinite loop and I have to close the program. When a user enters "11a" the program does the error checking and the program still works. What am I doing wrong, I tried changing my code around, but still it doesn't work quite right.
Code:#include <iostream.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <iomanip.h> void main(void) { // Declaration of Variables const double Jap_Rate = 1.177; // Japanese Rate const double Can_Rate = 1.021; // Canadian Rate const double Bpd_Rate = 1.559; // British Pounds Rate const double Aus_Rate = 0.652; // Australian Rate const double Eur_Rate = 1.833; // Euro Rate bool bCont = true; char choice; // Hold selection of which currency is chosen char curName[25]; // Hold name of currency double curRate; // Hold foreign rates float usAmount; // Hold US dollar amount double convertAmount; // Hold converted amount do{ cout << "\tCurrency Conversion Program"<< endl; cout << "\t---------------------------"<< endl << endl; // User will be prompted for input cout << "What currency would like to converted?" << endl; cout << "\t1 for Japanese Yen" << endl; cout << "\t2 for Canadian" << endl; cout << "\t3 for British Pounds" << endl; cout << "\t4 for Australian Dollar" << endl; cout << "\t5 for Euro" << endl; cout << "\t6 Exit" << endl; cin >> choice; // Switch for the currency type and rate switch(choice){ case '1': strcpy(curName,"Yen"); curRate = Jap_Rate; break; case '2': strcpy(curName,"Canadian"); curRate = Can_Rate; break; case '3': strcpy(curName,"British Pounds"); curRate = Bpd_Rate; break; case '4': strcpy(curName,"Australian"); curRate = Aus_Rate; break; case '5': strcpy(curName,"Euro"); curRate = Eur_Rate; break; case '6': exit(1); default: cout << "\tTry Again" << endl << endl; break; } 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); } // End Main



LinkBack URL
About LinkBacks


