In: Computer Science
](Currency Exchange) You are given the exchange rate from Canadian to US dollars. You are asked to convert an amount either from Canadian to US or vice versa based on the user's request. Write a C++ program that prompts the user to enter: a. the exchange rate from currency in Canadian dollars to US dollars b. either 0 to convert from Canadian to US or 1 to convert from US to Canadian (the program displays a message for invalid input) c. either the amount in Canadian or US dollars according to the request The program then calculates and displays the amount in either Canadian or US dollars according to the request. Here are sample runs: Rate from CAD to USD: 0.747 0 for CAD to USD or 1 vice versa: 0 CAD amount: 100 100 CAD is 74.7 US Rate from CAD to USD: 0.75 0 for CAD to USD or 1 vice versa: 1 US amount: 100 100 US is 133.33 CAD Rate from CAD to USD: 0.754 0 for CAD to USD or 1 vice versa: 5 Invalid input
/*C++ program that display the conversion from canadian to
usd for 0
and usd to canadian for 1 .Then do the conversion and print the
results on
console output window The program continues till user enters an
invalid
input value.*/
//main.c
//include header file
#include<iostream>
using namespace std;
//start of main function
int main()
{
//declare variables
int userchoice;
//set repeat to true
bool repeat=true;
double canadianDollars=0;
double americanDollars=0;
//constants for conversion
const double CAD_TO_USD=0.747;
const double USD_TO_CAD=1.36;
cout<<"Rate from CAD to USD:
0.747"<<endl;
cout<<"0 for CAD to USD"<<endl;
cout<<"1 for USD to CAD"<<endl;
//read userchoice
cin>>userchoice;
while(repeat)
{
//check if userchoice is 0
if(userchoice==0)
{
cout<<"CAD
amount:";
cin>>canadianDollars;
//convert candian to american dollars
americanDollars=canadianDollars*CAD_TO_USD;
cout<<canadianDollars<<" CAD is
"<<americanDollars<<" US"<<endl;
}
//check if userchoice is 1
else if(userchoice==1)
{
cout<<"USD
amount:";
cin>>americanDollars;
//convert american dollars to candian dollars
canadianDollars=americanDollars*USD_TO_CAD;
cout<<americanDollars<<" USD is
"<<canadianDollars<<" CAD"<<endl;
}
else
{
//set
repeat to false to stop repetition of while loop
repeat=false;
cout<<"Invalid input"<<endl;
}
//prompt for input if repeat is
true
if(repeat)
{
cout<<"Rate from CAD to USD: 0.747"<<endl;
cout<<"0
for CAD to USD"<<endl;
cout<<"1
for USD to CAD"<<endl;
cin>>userchoice;
}
}
//pause console output on console
//till user enters a key on keyboard
system("pause");
return 0;
}
-------------------------------------------Sample Run#------------------------------------------------