Hello I am writing a Loop program in xcode C that is suppose
Hello,
I am writing a Loop program in xcode (C++) that is supposed to convert various type of currency to dollars. So far everything is working fine. But I have only one issue. I can\'t increment the currency types. How do I increment the currency types to go for example from \"Euros\" to \"Great Britan Pounds\" in the output message? Can anyone help me with a command that can work and if possible tell me where to put that command in the program. Also I need to use a sentinel value that will indicate to the program that there is no more currency to be converted to so it should stop looping. Any idea on how to implement it? Below is the program. Thank you.
// main.cpp
// Program 5 - Loops
// Allows currency to be converted from various types to US dollars
#include
#include
#include
using namespace std;
const int USD = 0;
const int EUR = 1;
const int GBP = 2;
const int INR = 3;
const int AUD = 4;
const int CAD = 5;
const int ZAR = 6;
const int NZD = 7;
const int JPY = 8;
//Outputs a menu of currency types for user, returning a constant value // as defined above representing the selected currency.
int getUserSelection ()
{
int selection;
cout << \"Available currencies for conversion: \" << endl;
cout << \"(1) Euros\" << endl;
cout << \"(2) Great Britan Pounds\" << endl;
cout << \"(3) Indian Rupees\" << endl;
cout << \"(4) Australian Dollars\" << endl;
cout << \"(5) Canadian Dollars\" << endl;
cout << \"(6) South African Rands\" << endl;
cout << \"(7) New Zealand Dollars\" << endl;
cout << \"(8) Japanese Zen\" << endl;
cout << \"Enter the number of the desired currency to convert: \";
cin >> selection;
return selection;
}
//Converts the numeric type to a String representation. //Use this to output a String representation of the type.
string convertTypeToString(int type)
{
switch (type)
{
case USD:
return \"US Dollars\";
case EUR:
return \"Euros\";
case GBP:
return \"Great Britan Pounds\";
case INR:
return \"Indian Rupees\";
case AUD:
return \"Australian Dollars\";
case CAD:
return \"Canadian Dollars\";
case ZAR:
return \"South African Rands\";
case NZD:
return \"New Zealand Dollars\";
case JPY:
return \"Japanese Zen\";
}
return \"\";
}
int main(int argc, char *argv[])
{
int userSelection = getUserSelection();
string selectionAsAString = convertTypeToString(userSelection);
double amount;
double ConversionFactor = 0.0;
double usDollars;
cout << \"You entered \" << selectionAsAString << endl;
for (int userSelection = 1; userSelection <= 8; userSelection++)
{
cout << \"Enter the amount of money in \" << selectionAsAString << \" currency to convert to US dollars: \";
cin >> amount;
if (userSelection == EUR)
ConversionFactor = 1.08611;
if (userSelection == GBP)
ConversionFactor = 1.44476;
if (userSelection == INR)
ConversionFactor = 0.01495;
else if (userSelection == AUD)
ConversionFactor = 0.69864;
if (userSelection == CAD)
ConversionFactor = 0.70112;
else if (userSelection == ZAR)
ConversionFactor = 0.05999;
if (userSelection == NZD)
ConversionFactor = 0.65371;
else if (userSelection == JPY)
ConversionFactor = 0.00850;
cout << \"You\'ve entered \" << amount << selectionAsAString << \" it is equivalent to \" << amount * ConversionFactor << \" USD \" << endl;
}
cin.get();
return 0;
}
In the loop, the code should do the following:
1. Call getUserSelection() to display the menu and get the number representing the type of currency to be converted:
2. Prompt for and input the amount of money in the selected currency to convert to US dollars.
3. Add a decision statement that uses the userSelection to determine which conversion factor to be used.
4. Convert the entered amount to US dollars using the conversion factor.
5. Output the amount of currency in US dollars.
6. Use the convertTypeToString function to convert the numeric value to a string for more readable output.
This is what the output should look like:
Available currencies for conversion:
(1) Euros
(2) Great Britan Pounds
(3) Indian Rupees
(4) Australian Dollars
(5) Canadian Dollars
(6) South African Rands
(7) New Zealand Dollars
(8) Japanese Zen
Enter the number of the desired currency to convert: 1
You entered Euros
Enter the amount of money in Euros currency to convert to US dollars: 23
You\'ve entered 23Euros it is equivalent to 24.9805 USD
...
(It should then ask again to: Enter the amount of money in Great Britan Pounds currency to convert to US dollars:
and so on until it reaches the last currency and then stop looping). Now my issue is that I can\'t increment currency type, the program asks me to enter the amount of Euros over and over but it only changes the conversion factor.
Solution
PROGRAM CODE:
#include <iostream>
using namespace std;
const int USD = 0;
const int EUR = 1;
const int GBP = 2;
const int INR = 3;
const int AUD = 4;
const int CAD = 5;
const int ZAR = 6;
const int NZD = 7;
const int JPY = 8;
//Outputs a menu of currency types for user, returning a constant value // as defined above representing the selected currency.
int getUserSelection ()
{
int selection;
cout << \"Available currencies for conversion: \" << endl;
cout << \"(1) Euros\" << endl;
cout << \"(2) Great Britan Pounds\" << endl;
cout << \"(3) Indian Rupees\" << endl;
cout << \"(4) Australian Dollars\" << endl;
cout << \"(5) Canadian Dollars\" << endl;
cout << \"(6) South African Rands\" << endl;
cout << \"(7) New Zealand Dollars\" << endl;
cout << \"(8) Japanese Zen\" << endl;
cout << \"Enter the number of the desired currency to convert: \";
cin >> selection;
return selection;
}
//Converts the numeric type to a String representation. //Use this to output a String representation of the type.
string convertTypeToString(int type)
{
switch (type)
{
case USD:
return \"US Dollars\";
case EUR:
return \"Euros\";
case GBP:
return \"Great Britan Pounds\";
case INR:
return \"Indian Rupees\";
case AUD:
return \"Australian Dollars\";
case CAD:
return \"Canadian Dollars\";
case ZAR:
return \"South African Rands\";
case NZD:
return \"New Zealand Dollars\";
case JPY:
return \"Japanese Zen\";
}
return \"\";
}
int main(int argc, char *argv[])
{
int userSelection = getUserSelection();
string selectionAsAString = convertTypeToString(userSelection);
double amount;
double ConversionFactor = 0.0;
double usDollars;
cout << \"You entered \" << selectionAsAString << endl;
for (int userSelection = 1; userSelection <= 8; userSelection++)
{
//added a line here to convert the int to string selection every time
selectionAsAString = convertTypeToString(userSelection);
cout << \"Enter the amount of money in \" << selectionAsAString << \" currency to convert to US dollars: \";
cin >> amount;
if (userSelection == EUR)
ConversionFactor = 1.08611;
if (userSelection == GBP)
ConversionFactor = 1.44476;
if (userSelection == INR)
ConversionFactor = 0.01495;
else if (userSelection == AUD)
ConversionFactor = 0.69864;
if (userSelection == CAD)
ConversionFactor = 0.70112;
else if (userSelection == ZAR)
ConversionFactor = 0.05999;
if (userSelection == NZD)
ConversionFactor = 0.65371;
else if (userSelection == JPY)
ConversionFactor = 0.00850;
cout << \"You\'ve entered \" << amount << selectionAsAString << \" it is equivalent to \" << amount * ConversionFactor << \" USD \" << endl;
}
cin.get();
return 0;
}
OUTPUT:





