simple programme to find the largest of three

This is a discussion on simple programme to find the largest of three within the C++ Programming forums, part of the General Programming Boards category; Hi
I was trying to write a simple program to find the largest out of the three given numbers. But ...

simple programme to find the largest of three

Hi

I was trying to write a simple program to find the largest out of the three given numbers. But when I try to compile it gives first error at the bold line. Could you please correct it?

By the way, is there any way that I could check it for all the errors before I try to compile it? e.g. When I tried to compile the program the compiler (Dev-C++) highlighted only the bold line, so I don't know (I'm sure there are!) if there are errors in other lines too. If I had known this I would have also boldfaced those lines too. Do you get what I'm trying to ask?

Q1: This is the question from my post no. 1 above.
By the way, is there any way that I could check it for all the errors before I try to compile it? e.g. When I tried to compile the program the compiler (Dev-C++) highlighted only the bold line, so I don't know (I'm sure there are!) if there are errors in other lines too. If I had known this I would have also boldfaced those lines too. Do you get what I'm trying to ask?

Q2: Now I think there is still some logical errors or syntax related. Not sure. You see the error in the running program below.

Please help me with these issues. It would be really kind of you. Thanks.

This is the copy of running program from the command prompt:

Code:

Enter the three numbers
Enter a = 901
Enter b = 4
Enter c = 0
a is the largest
c is the largest

One simple way to make it work when you have some values equal is to replace all of your ">" operators with ">=". However if you don't consider a value to be the largest when it's equal to another, I'm afraid you'll just have to either do this in a more clever way, or add more case statements, e.g.:

Code:

else if (a == b && a > c)
cout << "a and b are the largest" << endl;

Also, what is "#incude <conio.h>" doing in there? Did you get that from a book or tutorial or something? Consider switching to something more up to date. conio.h is not standard; don't use it. I'm guessing conio.h provides the getch() you use, you should use cin.get() instead.

Finally, your main() is supposed to return an int. You should add return 0; to the end of main().

In the below code am I using nested if statements? When do we use braces with the if statements?

You can use the braces all of the time if you wish, some people will say you should always use braces on them. But braces are required any time there are more than one line in the clause. My feeling is to use braces when it makes the flow of the program clearer or when you have a if/else inside your outer if

Code:

if(condition)
{
if(something else)
{
do this;
}
else
{
this
}
}

As it makes it clear to what if the else belongs.

This program only works correctly when no two numbers are equal. Is this possible to make it work even when the two or all three numbers are equal? Any idea?

You will need to add another if to test if a == b == c.

Also any time you are using if/else if clauses you should end your statements with an else, even if it is an empty clause ( {} ). This will help to highlight that the clause is ending here.

Also you should prefer to test against one of your variables at a time, instead of

Code:

if ( (a > b)&&(b > c) )

Start by checking against a

Code:

if (( a > b ) || ( a > c))
// a is largest
else if( b > c ) // don't need to check against a because if you get here a is not the largest
// b is largest
else
// c is the largest

I think it needs to be pointed out that while we know what you mean, that's not how you'd write the expression in C++. In C++, if a, b, and c were all equal, that statement would evaluate to false because it is interpreted like this:

Code:

a == b == c
(a == b) == c
true == c
false

So you should instead write:

Code:

a == b && b == c

Originally Posted by jimblumberg

Also any time you are using if/else if clauses you should end your statements with an else, even if it is an empty clause ( {} ). This will help to highlight that the clause is ending here.

I suppose this is really a matter of taste, but I disagree. I don't see why the an empty else clause is beneficial to readability. If, when writing the code, you're intending that one of the clauses in the if/else if sequence is entered, you should do this:

Code:

if (...)
{
...
}
else if (...)
{
...
}
else
{
assert(false);
}

Originally Posted by jimblumberg

Code:

if (( a > b ) || ( a > c))
// a is largest

This is wrong. Consider a = 2, b = 3, c = 1. Then a > b is false but a > c is true. So you have false || true which is true, but a is not the largest.

Thanks a lot both of you, Mozza, Jim. You are really nice and very helpful. My best wishes.

I'm guessing conio.h provides the getch() you use, you should use cin.get() instead.

Finally, your main() is supposed to return an int. You should add return 0; to the end of main().

Okay, I would try to use cin.get() insstead of getch() once I convince that 'old way' instructor of mine to shift to something up-to-date.

Seriously I read the bold part several times and tried to unsuccessfully decipher the meaning. Could you please help me in easy words as you have already been doing?

I don't really know what the instructor was saying. Do you understand this? He was telling something that:

Code:

if (1 < 2) // as the statement is true therefore it will show "1"
if (1 > 2) // as the statement is false it will show "0"
if (radius of a circle) /* as in this statement you haven't specified any condition therefore it will be interpreted as true always/*

Seriously I read it several times and tried to unsuccessfully decipher the meaning. Could you please help me in easy words as you have already been doing?

Ok, the "int" here mains "main()" returns an int:

Code:

int main()

So you need to return an integer in the main() function, like this:

Code:

int main()
{
....
return 0;
}

This integer returned by main is accessible back in the console or whatever program calls it. Returning 0 indicates that your program ran to completion correctly. If something goes wrong in your program, you generally want main() to return 1 instead.

Actually, you don't. The global main function is a special exception where if control reaches its end without encountering a return statement, the effect is return 0;

Of course, placing such a return statement there anyway is more consistent and does not hurt, even though it is unnecessary.

There's EXIT_FAILURE from <cstdlib>, if I remember correctly.

Good point. I should have said "You should return an int" :-).

Of course there is also the question of why this exception exists. I suppose it was to avoid breaking code that was written before standardisation? I would have preferred if void main() was allowed as an alternative instead of giving an exception to int main(), or better yet I would have preferred no exception at all.

I don't really know what the instructor was saying. Do you understand this? He was telling something that:

Code:

if (1 < 2) // as the statement is true therefore it will show "1"
if (1 > 2) // as the statement is false it will show "0"
if (radius of a circle) /* as in this statement you haven't specified any condition therefore it will be interpreted as true always/*

if (1 < 2) // as the statement is true therefore it will show "1"
if (1 > 2) // as the statement is false it will show "0"

The only thing I get from this, is if you look at this from a C point of view, since C doesn't have boolean types, it uses integers with 0 indicating false and anything non-zero (but usually 1) indicating true. So since 1 < 2 is true, it evaluates to 1, in a way. Similarly, 1 > 2 evaluates to 0 in C.

if (1 < 2) // as the statement is true therefore it will show "1"
if (1 > 2) // as the statement is false it will show "0"
if (radius of a circle) /* as in this statement you haven't specified any condition therefore it will be interpreted as true always/*

The logic is not correct, the reverse is correct.
The rule is when you have integers