I am a beginner in C++ so if I did anything wrong in this program, please explain it without mocking me. I wrote this program so that it could convert currency however every time I try it, it always gives me 0. In other words, all my conversions always end up being 0.
#include <iostream>
usingnamespace std;
float a;
string nameofcurr;
float p;
float d;
float r;
float e;
float pounds (float x, float p)
{
if (nameofcurr == "dollars") //converts dollars to pounds
{
p = 1.64 * x;
return (p);
}
elseif (nameofcurr == "rupees") // converts rupees to pounds
{
p = 101.25 * x;
return (p);
}
elseif (nameofcurr == "euros") // converts euros to pounds
{
p = 1.19 * x;
return (p);
}
elseif (nameofcurr == "pounds") // converts pounds to pounds
{
cout << "The number you entered is already the converted amount \n";
}
}
float euros (float x, float e)
{
if (nameofcurr == "dollars" ) //converts dollars to euros
{
e = 1.37 * x;
return (e);
}
elseif (nameofcurr == "rupees")// converts rupees to euros
{
e = 84.86 * x;
return (e);
}
elseif (nameofcurr == "euros") // converts euros to euros
{
cout << "The number you entered is already the converted amount \n";
}
elseif (nameofcurr == "pounds") // converts pounds to euros
{
e = .84 * x;
return (e);
}
}
float rupees (float x, float r) // converts dollars to rupees
{
if (nameofcurr == "dollars") //converts dollars to euros
{
r = .016 * x;
return (r);
}
elseif (nameofcurr == "rupees")// converts rupees to euros
{
cout << "The number you entered is already the converted amount \n";
}
elseif (nameofcurr == "euros") // converts euros to euros
{
r = .012 * x;
}
elseif (nameofcurr == "pounds") // converts pounds to euros
{
e = .0099 * x;
return (e);
}
}
float dollars (float x, float d) // converts any of the three currencies to dollars
{
if (nameofcurr == "dollars") //converts dollars to euros
{
cout << "The number you entered is already the converted amount \n";
}
elseif (nameofcurr == "rupees")// converts rupees to euros
{
d = 61.95 * x;
return (d);
}
elseif (nameofcurr == "euros") // converts euros to euros
{
d = .73 * x;
return (d);
}
elseif (nameofcurr == "pounds") // converts pounds to euros
{
d = .61 * x;
return (d);
}
}
int main()
{
float x;
cout << "Enter the number of dollars, rupees, euros, or pounds you would like to convert (do nont forget to type out the currency name all lowercase!) \n";
cin >> x >> nameofcurr;
if (nameofcurr != "dollars" || nameofcurr != "rupees" || nameofcurr != "euros" || nameofcurr != "pounds" )
{
cout << "Please type the currency name correctly \n";
cin >> nameofcurr;
}
cout << "What currency would you like to convert to? Enter the number that is next to the currency to indicate which one you want." << endl;
cout << "1. Pounds " << endl;
cout << "2. Euros " << endl;
cout << "3. Rupees " << endl;
cout << "4. Dollars" << endl;
cin >> a;
if (a == 1)
{
cout << "The conversion gives you " << pounds (x, p) << " pounds" << endl;
}
elseif (a == 2)
{
cout << "The conversion gives you " << euros (x, e) << " euros" << endl;
}
elseif (a == 3)
{
cout << "The conversion gives you " << rupees (x, r) << " rupees" << endl;
}
elseif (a == 4)
{
cout << "The conversion gives you " << dollars (x, d) << " dollars" << endl;
}
else
cout << "Please enter 1, 2, 3, or 4 based on the currency type you would like. \n";
return 0;
}
There are no errors given. Also, even when I type the currency name properly, it still tells me to spell it correctly. I need to know how to fix this as well. Thank you very much for your time!
@shalinmehta
Your problem, lies with this line of code. if (nameofcurr != "dollars" || nameofcurr != "rupees" || nameofcurr != "euros" || nameofcurr != "pounds" )
You are using the OR symbol between nameofcur instead of AND, ( '&&' ). With OR, if one of the checks is false, the whole line becomes false, so you get the line that says the currency name is not spelled correctly. Change the checking, and you should be okay.
PS : To get your program working, I also had to add #include <string>