I am coding this currency conversion app (into to prog class) and need to error check the user inputs. Currently if a user inputs an ASCII alpha character it throws the app into an infinante loop.
My logic is to simply check the input to see if its an integer:
{
(currency !== int) // ????????
}
However the HOW to do this escapes me. I simply do not want the user to enter in an Alpha character, but I dont want to explicitly limit the range either.
Any suggestions?
Below is my code for your review.
Thanks in advance.
Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
//................................
// defines constants
//................................
#define EUR 1.07892
#define BRI 1.57719
#define JAP 0.00835838
#define AUS 0.601142
#define IRA 3.21647
int main ()
{
//................................
//head
//................................
cout<<"Currency Conversion Calculator\n\n";
//.................................
//Declarations of variables
//.................................
int input = 0;
double currency = 0;
double EURC = 0;
double BRIC = 0;
double JAPC = 0;
double AUSC = 0;
double IRAC = 0;
//.................................
//request user input
//................................
while (input == 0 || !(input>= 1 && input <= 5))
{
cout<<"Select from the following 5 options:\n\n";
cout<<"To convert the Euro to US $, please press 1\n\n";
cout<<"To convert the British Pound to US $, please press 2\n\n";
cout<<"To convert the Yen to US $, please press 3\n\n";
cout<<"To convert the Australian $ to US $, please press 4\n\n";
cout<<"To convert the Iraqi Dinar to US $, please press 5\n\n";
cout<<"....:";
cin>> input;
cout<<"\n";
}
//.................................
// request user's amount to be converted
//................................
{
cout<<"Input the Currency Ammount to be Converted: ";
cin>> currency;
//................................
// Conversion table
//................................
EURC = currency * EUR;
BRIC = currency * BRI;
JAPC = currency * JAP;
AUSC = currency * AUS;
IRAC = currency * IRA;
}
/* switch(currency)
{
case1:
{
(currency !=int)
}
default:
{
printf("Invalid input, please enter a valid currency. \n\n");
break;
}
}
*/
//................................
// Calculate conversions and print
//................................
switch(input)
{
case 1:
{
printf ("%0.2lf Euros is equal to %0.2lf US dollars\n\n", currency, EURC);
break;
}
case 2:
{
printf ("%0.2lf British pounds is equal to %0.2lf US dollars\n\n", currency, BRIC);
break;
}
case 3:
{
printf ("%0.2lf Yen is equal to %0.2lf US dollars\n\n", currency, JAPC);
break;
}
case 4:
{
printf ("%0.2lf Australian dollars is equal to %0.2lf US dollars\n\n", currency, AUSC);
break;
}
case 5:
{ printf ("%0.2lf Iraqi Dinar is equal to %0.2lf US dollars\n\n", currency, IRAC);
break;
}
default:
{
printf("Invalid input, please enter a valid currency. \n\n");
break;
}
}
//................................
// stablize shell
//................................
system("PAUSE");
//................................
// statisfy interger expectation of int main
//................................
return 0;
}