Search
| C++ Currency Conversion |
|
| Written by Chris Gountanis |
|
This application demonstrates currency conversions using C++.
/* Name: Currency Conversion Copyright: Copyright 2007 by Chris Gountanis student of University of Phoenix. Author: Chris Gountanis Date: 09/25/07 Description: Currency Conversion assignment for weeks 4 and 5. */
//INCLUDES #include <iostream> #include <iomanip>
//NAMESPACE DEFINITION using namespace std;
//DEFINE RATES FOR EASY UPDATES IF NEEDED AS RATES CHANGE #define Canadian_Dollars_Rate 0.99970009; #define Euros_Rate 0.710378632; #define Swiss_Francs_Rate 1.17250064; #define British_Pounds_Rate 0.496598302; #define Russian_Rubles_Rate 25.0620285;
//CUSTOM FUNCTION FOR CHECKING VALID CURRENCY AMOUNT int isCurrency(char sAmount[]) { int x = 0; int iPointControl = 0; int iReturn = 0;
//LOOP ENTIRE STRING TILL EOF CHECKING FOR NUMERIC AND ONLY ONE DECIMAL while (sAmount[x] != '\0') { if (isdigit(sAmount[x])) { iReturn = 1; } else {
//IF MORE THAN 1 DECIMAL PLACE RETURN FALSE if ((sAmount[x] == '.') && (iPointControl < 1)) { iPointControl++; } else { iReturn = 0; break; } } x++ ; } return iReturn; }
void mycalc() {
//SETUP VARIABLES const int input_max = 11; char inputbuffer[input_max] = {0}; int storeid = 0; int myswitch = 0; double cashamount = 0; double totalamount = 0; system("CLS"); //CLEAR SCREEN cout << "Welcome to Currency Conversion\n";
//GET AMOUNT LOOP UNTIL VALID myswitch = 0;
while (myswitch == 0) { cout << "\nENTER CASH AMOUNT: \n>$"; cin.getline(inputbuffer, input_max); if(cin.fail()) { // IF LIMIT WAS EXCEEDED RESET CIN AND REMOVE LINE RETURN cin.clear(); cin.ignore(INT_MAX, '\n');; } //CHECK FOR VALID NUMERIC/CURRENCY FORMAT BY CALLING CUSTOM FUNCTION if (isCurrency(inputbuffer)) { cashamount = atof(inputbuffer); //CONVERT CHAR TO FLOAT/DOUBLE WITH ATOF myswitch = 1; } else { system("CLS"); //CLEAR SCREEN cout << "Please enter a valid numeric value!\n\n" ; } }
//GET STORE LOOP UNTIL VALID system("CLS"); //CLEAR SCREEN myswitch = 0;
while (myswitch == 0) { //PRINT NUMBERED STORE MENU cout << "[1].\tCanadian Dollars\n" << "[2].\tEuros\n" << "[3].\tSwiss Francs\n" << "[4].\tBritish Pounds\n" << "[5].\tRussian Rubles\n" << "CHOOSE A CURRENCY (1-5):\n>";
//REQUEST STORE NUMBER BASED ON MENU cin.getline(inputbuffer, input_max); if(cin.fail()) { // IF LIMIT WAS EXCEEDED RESET CIN AND REMOVE LINE RETURN cin.clear(); cin.ignore(INT_MAX, '\n'); }
//CHECK FOR NUMERIC if (isdigit(inputbuffer[0])) { storeid = atoi(inputbuffer); //CONVERT CHAR TO INTEGER WITH ATOI
//CHECK FOR 1,2 or 3 ONLY if ((storeid >= 1) && (storeid <= 5)) { myswitch = 1; } else { system("CLS"); //CLEAR SCREEN cout << "Please enter a valid option number!\n"; } } else { system("CLS"); //CLEAR SCREEN cout << "Please enter a numeric value only!\n"; } }
//DEPENDING ON STOREID RUN THE MATH FOR TOTAL CALCULATION switch (storeid) { case 1: //CANADIAN DOLLARS totalamount = cashamount * Canadian_Dollars_Rate; break; case 2: //EUROS totalamount = cashamount * Euros_Rate; break; case 3: //SWISS FRANCS totalamount = cashamount * Swiss_Francs_Rate; break; case 4: //BRITSH POUNDS totalamount = cashamount * British_Pounds_Rate; break; case 5: //RUSSIAN RUBLES totalamount = cashamount * Russian_Rubles_Rate; break; }
system("CLS"); //CLEAR SCREEN
//PRINT TOTAL WITH CURRENCY BASED DECIMAL PLACE cout << "USD TOTAL: $" << setiosflags(ios::fixed) << setprecision(2) << totalamount << endl<< endl; }
void main() { mycalc(); //RUN MAIN CODE
//WAIT FOR USER TO PRESS ENTER cout << "\nPress ENTER to continue.\n"; cin.get(); } |
| Last Updated on Saturday, 09 February 2008 21:02 |
