Reputation: 9
Hi I'm new with c++ and am having a hard time with creating a code to convert currencies. Could you please look at my current code and give any suggestions. the first goal is first to determine the type of currency. then the amount. finally the conversion.
#include <iostream>
#include <string>
using namespace std;
int main()
{
//declaring constant conversion values of currency per dollar
const float ColombianPeso = 2000;
const float MexicanPeso = 13.25;
const float ArgentinePeso = 8.4;
const float VenesuelanBolivar = 6.28;
const float ChileanPeso = 593.719;
//designing statement to allow user to input curency type
char currency[] = "show me the money (USDollar, MexicanPeso, ArgentinePeso, ColombianPeso, VenesuelanBolivar, or ChileanPeso):\n";
char answer1[17];
cout << currency;
cin >> answer1;
//designing statement to imput amount
float amount = 0;
cout << "enter amount:\n";
cin >> amount;
//creating if/else statement to convert for diffent money values
if (answer1 == USDollar)
cout << "number of Colombian Pesos:\n" << amount * ColombianPeso;
cout << "number of Venesuelan Bolivars:\n" << amount * VenesuelanBolivar;
cout << "number of Mexican Pesos:\n" << amount * MexicanPeso;
cout << "number of Argentine Pesos:\n" << amount * ArgentinePeso;
cout << "number of Chilean Pesos:\n" << amount * ChileanPeso;
else if (answer1 == MexicanPeso)
cout << "number of US Dollars:\n" << amount / MexicanPeso;
else if (answer1 == ColombianPeso)
cout << "number of US Dollars:\n" << amount / ColombianPeso;
else if (answer1 == ArgentinePeso)
cout << "number of US Dollars:\n" << amount / ArgentinePeso;
else if (answer1 == ChileanPeso)
cout << "number of US Dollars:\n" << amount / ChileanPeso;
else if (answer1 == VenesuelanBolivar)
cout << "number of US Dollars:\n" << amount / VenesuelanBolivar;
else
cout << "try again with VenesuelanBolivar, USDollar, ChileanPeso, ArgentinePeso, ColombianPeso, or MexicanPeso:\n";
return 0;
}
Upvotes: 0
Views: 10400
Reputation: 5606
You're comparing char[]
and float
, I think you don't want it.
const float ColombianPeso = 2000;
//...
char answer1[17];
//...
cin >> answer1;
//...
else if (answer1 == ColombianPeso)
I'd recommend you to use std::unordered_map
; it allows you to in example get float
used to conversion when you give a string. Also, it allows modify currency and their ratio in runtime.
#include <string>
#include <unordered_map>
#include <iostream>
#include <stdexcept>
using std::cout;
using std::cin;
using std::clog;
int main(){
std::unordered_map<std::string, float> ratio{
{"ColombianPeso", 2000},
{"MexicanPeso", 13.45},
{"ArgentinePeso", 8.4},
{"VenesueleanBolivar", 6.28},
{"ChileanPeso", 593.719}
};
clog << "Available currency:\n";
for(auto it=ratio.cbegin(); it!=ratio.cend(); ++i)
clog<< '\t' << it->first << ' ' << it->second << '\n';
clog << "Pick one of currency above: ";
std::string choice;
cin >> choice;
try{
auto value=ratio.at(choice); /*if element doesn't exist,
program jumps to the catch*/
float amount;
clog >> "Enter amount: ";
cin >> amount;
cout << value*amount << '\n';
}catch(std::out_of_range&){
clog << "Given currency is not available.\n";
}
}
Upvotes: 0
Reputation: 117876
Use a std::string
instead of char[]
. Also you need to compare to string literals, otherwise it will think those are variables.
std::string answer1;
cin >> answer1;
if (answer1 == "USDollar")
{
// do stuff
}
Upvotes: 2