Converter/else and if

A quick converter I typed up to practice using else and if statements, was wondering why the program always executes the "start=1" section of code even when I enter any other number, even 2? Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  #include <iostream>
using namespace std;
int main()
{
float operation;      //functions
float start;

cout << "This program will convert US Dollars to British pounds and back,\n depending on what you choose!\n" << endl << endl;
cout << "Please input what you would like to do, 1 = USD->pounds, 2 = Pounds->USD: ";   //description of program
cin >> operation;

//$ to pounds
if (operation = 1)
   {
          cout << "Enter the amount of US Dollars you wish to convert to Pounds.\n";                 
          cin >> start;
          cout << start << " Dollars are equivalent to: " << start * 0.66 << " British pounds." <<endl << endl;   
   }

//pounds to $
else if(operation = 2)
     {
     cout << "Enter the amount of British Pounds you wish to convert to USD. \n";
     cin >> start;
     cout << start << " Pounds are equivalent to: " << start * 1.52 << " US Dollars." << endl << endl;
     }


//trolls
else
     {
    cout << "Please run program again and enter either 1 or 2.\n";
     }
     
     cout << "Thank you for using this converter :)\n" << endl;
system("PAUSE");
return 0;

}

I think u should change line 13 if (operation = 1) to if (operation == 1).
= is an assignment operator it will assign the value 1 to operation
while == is a logical operator.It will check if value of operation is 1 or not.
Similarly line 21else if(operation = 2) to else if(operation == 2)
hope this will work
cyber dude,
Wow, thanks that makes perfect sense :)
Topic archived. No new replies allowed.