Hey guys i need help on my project. So everything runs smoothly until the user inputs a different answer other than the ones asked for (the choices are a-i). So if the user puts a different answer its suppose to runt he program again. I do not know what I am doing wrong.
I'm new to c++ as well but I don't think you can use >= and <= on char.
Yes you can, char is converted to ascii int and compared.
if ((toupper(letter) >= 'A') && (toupper(letter) <= ' I'))
Notice the space before the I, this is messing things up.
It is still probably useful to have a default: case for your switch statement where you set sum equal to 0 and print out some sort of error, just in case.
#include <iostream>
#include <string>
#include <cctype >
usingnamespace std;
constfloat RUSSIAN_RUBLE = 31.16840f;
constfloat NORTH_KOREAN_WON = 135.00f;
constfloat CHINESE_YUAN = 6.83200f;
constfloat CANADIAN_DOLLAR = 1.1137f;
constfloat CUBAN_PESO = 1.00f;
constfloat ETHIOPIAN_BIRR = 9.09f;
constfloat EGYPTIAN_POUND = 5.6275f;
constfloat TUNISIAN_DINAR = 1.3585f;
constfloat THAI_BAHT = 34.40f;
float getDollarAmt();
void displayCurrencies();
char getCurrencySelection();
float calcExchangeAmt(char letter, float dollar);
void displayResults(float dollar, float sum, char letter);
bool isSelectionValid(char letter);
int main()
{
float dollar;
char letter;
float sum;
char answer = 'Y';
while (toupper(answer) == 'Y')
{
dollar = getDollarAmt();
displayCurrencies();
cin>>letter;
calcExchangeAmt(letter, dollar);
sum = calcExchangeAmt(letter, dollar);
displayResults(dollar, sum, letter);
cout << "\nDo you wish to continue (Y for yes - N for no): ";
cin >> answer;
}
}
float getDollarAmt()
{
float dollar;
cout << "Please enter the total dollar amount to exchange: $";
cin >> dollar;
return dollar;
}
void displayCurrencies()
{
cout << "Please select a target currenc y:" << endl << endl
<< "A Russian Ruble" << endl
<< "B North Korean Won" << endl
<< "C Chinese Yuan" << endl
<< "D Cuban Peso" << endl
<< "E Ethiopian Birr" << endl
<< "F Thai Baht" << endl
<< "G Canadian Dollars" << endl
<< "H Tunisian Dinar" << endl
<< "I Egyptian Pound" << endl << endl;
}
float calcExchangeAmt(char letter, float dollar)
{
float sum;
switch (toupper(letter))
{
case'A': sum = RUSSIAN_RUBLE * dollar;
break;
case'B': sum = NORTH_KOREAN_WON * dollar;
break;
case'C': sum = CHINESE_YUAN * dollar;
break;
case'D': sum = CUBAN_PESO * dollar;
break;
case'E': sum = ETHIOPIAN_BIRR * dollar;
break;
case'F': sum = THAI_BAHT * dollar;
break;
case'G': sum = CANADIAN_DOLLAR * dollar;
break;
case'H': sum = TUNISIAN_DINAR * dollar;
break;
case' I': sum = EGYPTIAN_POUND * dollar;
break;
default:
cout << "Please enter your selection: ";
cin >> letter;
}
return sum;
}
void displayResults(float dollar, float sum, char letter)
{
string word;
string word2;
string word3;
switch (toupper(letter))
{
case'A': word = "Russian";
word2 = "Ruble";
break;
case'B': word = "North";
word2 = "Korean";
word3 = "Won";
break;
case'C': word = "Chinese";
word2 = "Yuan";
break;
case'D': word = "Cuban";
word2 = "Peso";
break;
case'E': word = "Ethiopian";
word2 = "Birr";
break;
case'F': word = " Thai";
word2 = "Baht";
break;
case'G': word = "Canadian";
word2 = "Dollars";
break;
case'H': word = "Tunisian";
word2 = "Dinar";
break;
case' I': word = "Egyptian";
word2 = "Pound";
break;
}
cout << "$" << dollar << " is " << sum << " " << word << " "
<< word2 << " " << word3;
} // end of displayResults()
Will prompt the user to reenter if the entered value is invalid.
But after 3 times of invalid inputs the program will return nan(not a number).
Can anyone fix this?