0

I am new to c++ and working through the book'Programming principles and practice using c++'

I am asked to write a program to convert 3 currencies into dollars. I have written the program and it works perfectly for 2 of the currencies, but it wont work for the third (Euros) and I have no idea why.

I'm sure it is something very simple that I am missing, but would love the help! Would also welcome any critique on my code.

#include <iostream>
using namespace std;

int main(){

constexpr double yen_to_dollars=0.0091;
constexpr double euro_to_dollars=1.071;
constexpr double pound_to_dollars=1.279;

double amount=1;
char currency =' ';

cout<<"please enter an amount of currency to be converted to dollars (Y, E or P):\n";
cin >> amount >> currency;


if(currency=='Y')
cout<<amount<<"Yen = " << yen_to_dollars*amount << "dollars\n";
else if(currency=='P')
cout<<amount<<"Pounds = "<<pound_to_dollars*amount << "dollars\n";
else if(currency='E')
cout<<amount<<"Euros = "<<euro_to_dollars*amount << "dollars\n";
else
cout<<"That is an invalid currency\n";

}
Ross_Barkley
  • 3
  • 1
  • 6
  • 2
    `using namespace std;` [is a bad practice](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – tambre Apr 21 '17 at 05:37
  • Turn on warnings in your compiler, this would have been helpful: `[Warning] suggest parentheses around assignment used as truth value` – Donnie Apr 21 '17 at 05:41

2 Answers2

0

You are not comparing to 'E' properly: else if(currency='E') <-- this assigns 'E' to currency variable instead of comparing it.

Here's fixed example:

#include <iostream>
using namespace std;

int main(){

constexpr double yen_to_dollars=0.0091;
constexpr double euro_to_dollars=1.071;
constexpr double pound_to_dollars=1.279;

double amount=1;
char currency =' ';

cout<<"please enter an amount of currency to be converted to dollars (Y, E or P):\n";
cin >> amount >> currency;


if(currency=='Y')
cout<<amount<<"Yen = " << yen_to_dollars*amount << "dollars\n";
else if(currency=='P')
cout<<amount<<"Pounds = "<<pound_to_dollars*amount << "dollars\n";
else if(currency=='E') // < -- fixed
cout<<amount<<"Euros = "<<euro_to_dollars*amount << "dollars\n";
else
cout<<"That is an invalid currency\n";

}

Not sure if it's stackoverflow thing, but you should try to format your code nicely, so make it clear. This would be a bit better:

#include <iostream>
using namespace std;

int main()
{
    const double yen_to_dollars = 0.0091;
    const double euro_to_dollars = 1.071;
    const double pound_to_dollars = 1.279;

    double amount = 1;
    char currency;

    cout << "please enter an amount of currency to be converted to dollars (Y, E or P):\n";
    cin >> amount >> currency;


    if (currency == 'Y')
        cout << amount << "Yen = " << yen_to_dollars*amount << "dollars\n";
    else if (currency == 'P')
        cout << amount << "Pounds = " << pound_to_dollars*amount << "dollars\n";
    else if (currency == 'E') // < -- fixed
        cout << amount << "Euros = " << euro_to_dollars*amount << "dollars\n";
    else
        cout << "That is an invalid currency\n";
}
Pavel P
  • 15,789
  • 11
  • 79
  • 128
0

else if(currency = 'E') assigns 'E' to currency.

To compare use == as you did in the first two cases:

else if(currency == 'E')

Tip:

whenever doing comparisons, put the rvalue (if any) on the left hand side of the comparison operator. Then if you wrongly type = for ==, compiler will complain and make your life easier.

CinCout
  • 9,486
  • 12
  • 49
  • 67