quick question

I have this question for a homework assignment:

The following program segment is not supposed to print anything, yet it outputs the first error message, "Error in maximum: 100". What’s wrong, and how would you correct it? Why doesn’t the code output both error messages?

maximum = 75
minimum = 25;
If (maximum = 100)
cout << "Error in maximum: "<< maximum << endl;
If (minimum = 0)
cout << "Error in minimum: "<< minimum << endl;


Okay, so I believe it is because the maximum variable is set at 75. It's an assignment expression and not a logical expression which means that now the maximum has a value of 75 so, the if statement now causes an error. Now, as to why the code doesn't output both error messages, I am a little confused. Is it because it's set to 0?

Any help would be appreciated.

Thanks in advanced.
Look more carefully at the if() statements.
in your if statement, equals is '==' not '='.
So the if (maximum = 100) should be if (maximum == 75) since that is the value it was assigned to right? But then why isn't the minimum error coming up? Is it because it's just a 0?
No. All they are saying is that your syntax is wrong. Look at what these lines do:

1
2
3
4
if(maximum = 100) // this changes maximum from 75 to 100
  ...
if(maximum == 100) // this checks to see if maximum equals 100
   ...


The same applies to your minimum code.

Also, it might be more logical to write, "if maximum/minimum is greater-than/smaller-than 75/25" respecitvely.
Oh, this isn't a program I wrote. It's just a problem in the book.

I understand putting the logical expression in there (the ==), it's the minimum part that is throwing me off. The question says: why doesn't the code output both error messages? I understand why it would output the error of the maximum, but it's not outputting the minimum.
Last edited on
Look at what each variable is being assigned to.
So since the minimum is assigned to 0, that's why it output an error message?
Because doesn't 0 mean false?
Last edited on
0 DOES mean false, which is exactly why it DOESN'T print anything. if(false) //... will never execute.
that's what I thought, thanks!
Topic archived. No new replies allowed.