Aug 5, 2015 at 3:36am UTC
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.
#include <iostream>
#include <string>
#include <cctype >
using namespace std;
const float RUSSIAN_RUBLE = 31.16840f;
const float NORTH_KOREAN_WON = 135.00f;
const float CHINESE_YUAN = 6.83200f;
const float CANADIAN_DOLLAR = 1.1137f;
const float CUBAN_PESO = 1.00f;
const float ETHIOPIAN_BIRR = 9.09f;
const float EGYPTIAN_POUND = 5.6275f;
const float TUNISIAN_DINAR = 1.3585f;
const float 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();
letter = getCurrencySelection();
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;
system("cls");
}
}
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;
}
char getCurrencySelection()
{
char letter;
bool letterValid = false;
while (letterValid == false)
{
cout << "Please enter your selection: ";
cin >> letter;
letterValid = isSelectionValid(letter);
}
return letter;
}
bool isSelectionValid(char letter)
{
if ((toupper(letter) >= 'A') && (toupper(letter) <= ' I'))
{
return true;
}
else
{
return false;
}
}
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;
}
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()