Need help with this programmig

char choice = '2';
double num = 2.6, gain = 5.5, result;
if (choice = '1')
result = num*gain*gain;
else if (choice = '2')
result = num / 3*gain;
else
result=0;
cout<<result<<endl;

The answer is 78.65. Why is that so?
Firstly this code is wrong.
This:
if (choice = '1')
is assigning the value of '1' to your choice variable. It is not testing for equality.
You want something like this:
if (choice == '1')

i.e. 2 equal signs.

Why is that so?

You can step through the code with a debugger, looking at the values of your variables at each step.
I see mutexe has solved your problem :)

Next time use code tags please, makes it easier to read for anyone trying to help you

1
2
3
4
5
6
7
8
9
char choice = '2';
double num = 2.6, gain = 5.5, result;
if (choice = '1')
result = num*gain*gain;
else if (choice = '2')
result = num / 3*gain;
else
result=0;
cout<<result<<endl;


Like that :)

More info: http://www.cplusplus.com/forum/articles/16853/
Topic archived. No new replies allowed.