I have been reading up on C++ for months now and have copied many examples but have now I have just set out on my own with trying a basic project and have become stuck almost almost straight away.
My idea is to create a currency converter where I enter the currency to be converted, then the amount in that currency and then the currency to be converted to.
Here is the code I have so far ( I appreciate its not much but I am only getting started ).
#include<iostream>
usingnamespace std;
string Currency1;
string Currency2;
float Amount1;
float Amount2;
double GBR = 1.64969;
int main () {
cout << "Please enter the currency to be converted: " ;
cin >> Currency1 ;
cout << "Please enter the amount in " << Currency1 << " to be converted: " ;
cin >> Amount1 ;
cout << "Please enther the currency to be converted to: ";
cin >> Currency2 ;
if (Currency1 == "USD") {
USD * Currency2
}
cout << "The amount in "<< Currency2 << " is: " << Amount1*Amount2 << "\n" ;
return 0;
}
I know that the line where I try to multiply the Amount1 with Currency2 is definitely wrong but I am wondering if anyone out there can help me with how to input a string and then link that through to multiplying by a number.
There's really no reason to link them together. What you have now is perfectly fine. You get the currency they want to convert from. Then you get the amount of it. Lastly you get the currency they want to convert to. You don't need to actually link the currency to a value although you can using an std::map.
#include <iostream>
#include <map>
usingnamespace std;
string Currency1;
string Currency2;
float Amount1 = 0;
float Amount2 = 0;
int main () {
map <string, float> rates;
rates["EUR"] = 1.37757;
rates["GBR"] = 1.64821;
rates["INR"] = 0.01646;
rates["AUD"] = 0.91220;
rates["CAD"] = 0.89178;
rates["ZAR"] = 0.09195;
rates["NZD"] = 0.85401;
rates["JPN"] = 1.64969;
cout << "Please enter the currency to be converted: " ;
cin >> Currency1 ;
cout << "Please enter the amount in " << Currency1 << " to be converted: " ;
cin >> Amount1 ;
cout << "Please enther the currency to be converted to: ";
cin >> Currency2 ;
AmountinUSD = Amount1
if (Currency2 == "USD" ) {
Amount 2 =
}
cout << "The amount in "<< Currency2 << " is: " << Amount1*Amount2 << "\n" ;
return 0;
}
I think all of the rates (from USD to currency) are now in the map but I don't know how to call them. I was also thinking that to make it shorter I might include a line that converts which ever Currency1 it is into USD first and then from there into which ever Currency2 is. I am now not sure how to create that initial conversion into USD (line 31). Basically I need to know how to call the conversion rate associated with the string entered as Currency1.