(newbie) problem with if statement.

Trying to calculate exchange rate and the program keeps calculating first if statement even when currencyType != 1. I'm new to programming so I'm sure im overlooking something small. Help please?!


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
//declare variables
double USD;
const double EUR = 0.72508;
const double RUP = 45.6814;
const double YEN = 81.4614;
double currencyType;
double exchangeRate;

//prompt user for input
cout << "Enter U.S. Dollar amount: ";
cin >> USD;
cout << endl;
cout << "Choose which currency to convert to: " << endl;
cout << endl;
cout << "1: Euro" << endl << "2: Indian Rupee" << endl << "3: Japanese Yen\n" << endl;
cout << "Enter Appropriate Number: ";
cin >> currencyType;
cout << endl;


//calculate exchange rate
if ( currencyType = 1 )
{
exchangeRate = USD * EUR;
cout << "The amount in Euro is: " << exchangeRate <<endl;
}
else if ( currencyType = 2 )
{
exchangeRate = USD * RUP;
cout << "The amount in Indian Rupees is: " << exchangeRate << endl;
}
else if ( currencyType = 3 )
{
exchangeRate = USD * YEN;
cout << "The amount in Japanese Yen is: " << exchangeRate << endl;
}

cout << endl;


system("PAUSE");
return EXIT_SUCCESS;
}
if(currencyType = 1)

should be

if(currencyType == 1)

for all comparisons. And currencyType should be an int because all the options are ints.
I knew it was something small. Thank you for your help.
closed account (3pj6b7Xj)
someday they are going to have to change that comparison operator.
^Why?
this may sound stupid but sometimes i write it backwards so that mistake can never happen. like the following.

1
2
3
4
5
6
7
8


// assignment cant happen. using = results in error.
if ( 1 == currencytype)
{
    //do stuff
}
Topic archived. No new replies allowed.