### What's my problem?

I'm going to print the highest and the lowest among 3 numbers.

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include int main() { int num1,num2,num3,num4; int lowest, highest; printf("First input: "); scanf("%d", &num1); printf("Second input: "); scanf("%d" ,&num2); printf("Third input: "); scanf("%d, &num3); lowest=num1; if(lowest > num2) { lowest=num2; } else if (lowest>num3) { lowest=num3; printf("The lowest number is: %d\n" ,lowest); } highest=num1; if(highest < num2) { highest=num2; } else if (highest
You're missing a close quote on line 12, as the syntax highlighting clearly shows.

EDIT: also your logic is not quite right. If num2 is less than num1 and num3 is less than num2 (example: 5, 3, 1), then your code will record num2 (3) as the lowest value.

Also, you will only print the output if num3 is determined to be the highest/lowest value. If it turns out to be num1 or num2, you won't print anything.
Last edited on
Thank you know here's my code
 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include int main() { int num1,num2,num3; int lowest, highest; scanf("%d", &num1); scanf("%d" ,&num2); scanf("%d", &num3); lowest=num1; if(lowest > num2) { lowest=num2; } else if (lowest>num3) { lowest=num3; } printf("The lowest number is: %d\n" ,lowest); int high1=num1; int high2=num2; int high3=num3; highest=high1; if(highest < high2) { highest=high2; } else if (highest
Better, but you still have the problem with it favoring num2 over num3.

Take a look at lines 14 - 23:

 ``123456789`` ``````lowest=num1; if(lowest > num2) { lowest=num2; } else if (lowest>num3) { lowest=num3; }``````

Plug in these numbers:
num1 = 10
num2 = 5
num3 = 2

Step through the logic of what the code is doing. Your program will report '5' as the lowest number when the correct answer is '2'.

Hint: Remember what `else` means.
help me please what's the correct code for it.
Remember that an `else` clause will only execute if the preceeding if statement was false.

For example:

 ``12345678910`` ``````int foo = 5; if(foo == 3) { cout << "this will NOT print"; } else { cout << "this WILL print"; }``````

Similarly:

 ``12345678910`` ``````int foo = 5; if(foo == 5) { cout << "this WILL print"; } else { cout << "this will NOT print"; }``````

That said... you are doing an `else if` statement, which is the same conceptually as what's illustrated below:

 ``12345678910111213141516171819202122232425`` ``````// This code... lowest=num1; if(lowest > num2) { lowest=num2; } else if (lowest>num3) { lowest=num3; } // is the same as this code... lowest=num1; if(lowest > num2) { lowest=num2; } else { if (lowest>num3) { lowest=num3; } }``````

Notice how (due to the else clause) the check against num3 will only happen if the check against num2 came back as false. If it comes back as true, the num3 check will be skipped.
Topic archived. No new replies allowed.