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 ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    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?

    Please help me. Thanks a lot.

    Code:
    #include <iostream>
    #include <conio>
    
    using namespace std;
    
    int main()
    
    {
    	float a, b, c;
    	cout << "Enter the three numbers" << endl;
    	cout << "Enter a = " << endl;
    	cin >> a;
    	cout << "Enter b = ";
    	cin >> b;
    	cout << "Enter c= " << endl;
    	cin >> c;
    	if (a > b)&&(c < b)
    	cout << "a is the largest" << endl;
    	if (b > a)&&( a < c)
    	cout << "b is the largest" << endl;
    	else
    	cout << "c is the largest" << endl;
    	getch();
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,693
    You need to surround the statement with ()
    Code:
    if ((a > b) && (c  <  b))

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks, Jim.

    Now please help me with these queries.

    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

    Amended code is:

    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    
    {
    	float a, b, c;
    	cout << "Enter the three numbers" << endl;
    	cout << "Enter a = ";
    	cin >> a;
    	cout << "Enter b = ";
    	cin >> b;
    	cout << "Enter c = ";
    	cin >> c;
    	if ((a > b)&&(c < b))
    	cout << "a is the largest" << endl;
    	if ((b > a)&&( c < a))
    	cout << "b is the largest" << endl;
    	else
    	cout << "c is the largest" << endl;
    	getch();
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,693
    You probably should be using nested if statements similar to:

    Code:
       if((a > b) && (a > c))
          cout << "a is the largest" << endl;
       else if(b > c)
          cout << "b is the largest" << endl;
       else
          cout << "c is the largest" << endl;
    Note I didn't test all possibilities so the logic might not be accurate.

    Jim

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks, Jim.

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

    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?

    Please help me. Thanks.

    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    
    {
    	float a, b, c;
    	cout << "Enter the three numbers" << endl;
    	cout << "Enter a = ";
    	cin >> a;
    	cout << "Enter b = ";
    	cin >> b;
    	cout << "Enter c = ";
    	cin >> c;
    	if ( (a > b)&&(b > c) )
    	cout << "a is the largest" << endl;
    	else if ( (c > b)&&( a > c) )
    	cout << "a is the largest" << endl;
    	else if ( (b > a)&&( b > c) )
    	cout << "b is the largest" << endl;
    	else if ( (c > a)&&( c > b) )
    	cout << "c is the largest" << endl;
    	getch();
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  6. #6
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    I suggest you pay more attention to your indentation. It makes it hard to read your code. It should be more like this:

    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    
    {
    	float a, b, c;
    	cout << "Enter the three numbers" << endl;
    	cout << "Enter a = ";
    	cin >> a;
    	cout << "Enter b = ";
    	cin >> b;
    	cout << "Enter c = ";
    	cin >> c;
    	if ( (a > b)&&(b > c) )
    		cout << "a is the largest" << endl;
    	else if ( (c > b)&&( a > c) )
    		cout << "a is the largest" << endl;
    	else if ( (b > a)&&( b > c) )
    		cout << "b is the largest" << endl;
    	else if ( (c > a)&&( c > b) )
    		cout << "c is the largest" << endl;
    	getch();
    }
    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().

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,693
    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
    Jim

  8. #8
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by jimblumberg View Post
    You will need to add another if to test if a == b == c.
    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
    Quote Originally Posted by jimblumberg View Post
    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);
    }
    Quote Originally Posted by jimblumberg View Post
    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.
    Last edited by Mozza314; 04-01-2011 at 06:43 PM.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    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/*
    Thanks a lot for all the help, and your time.

    Best wishes
    Jackson
    Last edited by jackson6612; 04-01-2011 at 07:20 PM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  10. #10
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by jackson6612 View Post
    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.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by Mozza314
    So you need to return an integer in the main() function, like this:
    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.

    Quote Originally Posted by Mozza314
    If something goes wrong in your program, you generally want main() to return 1 instead.
    There's EXIT_FAILURE from <cstdlib>, if I remember correctly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by laserlight View Post
    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.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by jackson6612 View Post
    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/*
    Hi

    Does this make any sense to you? Please let me know. Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  14. #14
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by jackson6612 View Post
    Hi

    Does this make any sense to you? Please let me know. Thanks.
    No it doesn't make much sense actually.

    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"
    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.

    For the third line I can't make any sense of it at all.

  15. #15
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by jackson6612 View Post
    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/*
    The logic is not correct, the reverse is correct.
    The rule is when you have integers
    Code:
    if (INT) = true (for INT !=0)
    if (INT) = false (for INT = 0)
    so if the "radius of a circle" is 0 it will be false, not true.

    When you have
    Code:
    if (LOGICAL EXPRESSION)
    then the compiler just needs to do
    Code:
    if (LOGICAL EXPRESSION) = true (for LOGICAL EXPRESSION=true)
    if (LOGICAL EXPRESSION) = false (for LOGICAL EXPRESSION=false)
    Now, I don't see why the compiler will make a logical expression to an integer, then compare that integer with 0 to see if the if-statement is true or not.

    The use of the integers is mostly needed in C because there are no boolean type, so when you do something like
    Code:
    if( trySomething() ) .... ;
    then trySomething() will return 0 if it is false or non-zero if it is true and the if-statement will work.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple error i can't find /code
    By Alecroy in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2010, 01:00 PM
  2. Find last largest integer in array?
    By schmidtc in forum C Programming
    Replies: 2
    Last Post: 07-05-2010, 01:00 PM
  3. Largest / Smallest (5 integers)
    By Ripley in forum C Programming
    Replies: 4
    Last Post: 10-09-2005, 08:58 PM
  4. Replies: 5
    Last Post: 04-16-2004, 01:29 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21