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.
#include <iostream>
usingnamespace 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 $
elseif(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 21elseif(operation = 2) to elseif(operation == 2)
hope this will work
cyber dude,