if-else statement blunder

This is a discussion on if-else statement blunder within the C++ Programming forums, part of the General Programming Boards category; Hello, im still new in C++ so i need some help with this program im experimenting with the if-else statements. ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Location
    Malaysia
    Posts
    10

    if-else statement blunder

    Hello, im still new in C++ so i need some help with this program im experimenting with the if-else statements. Please help if anyone can.

    [Code]
    //This program is to be used to show the greatest value out of the three numbers which is
    //inputted by the user.


    #include <iostream.h>
    void main()
    {
    int num1, num2, num3;
    //The three numbers variable that are to be inputted by the user

    cout<<"Please input any three numbers. \n";
    cin>>num1>>num2>>num3;

    //The method of solving this is most likely by using the if-else statement

    if (num1>num2&&num3)
    cout<<num1<<" is the greatest number of all. \n";
    else if (num2>num1&&num3)
    cout<<num2<<" is the greatest number of all. \n";
    else if (num3>num1&&num2)
    cout<<num3<<" is the greatest number of all. \n";
    }

    [*Code]


    The above is the code, i don't know wats wrong with it but when i execute the program; after inputting the 3 numbers the only numbers that are considered are num1 and num2. Whilst even if num3 is actually larger it does not display num3 rather displays the second largest number of the system. So please anyone help me with this.

  2. #2
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I think you meant to put: if (num1>num2 && num1>num3) - same for the others

    You could use a library function for this, but I'll assume you wanted to do it manually.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Beware that iostream.h is a deprecated header. Do not use it. You should use <iostream> instead.
    Also, main returns int, not void.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Location
    Malaysia
    Posts
    10
    I see, thank you very much for all the help everyone's given me. XD

  5. #5
    Registered User
    Join Date
    Aug 2009
    Location
    Malaysia
    Posts
    10
    Quote Originally Posted by Dae View Post

    You could use a library function for this, but I'll assume you wanted to do it manually.

    What do you mean by a library function please tell me to experiment on this code if you will.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner question about the Return statement
    By lucidrave in forum C Programming
    Replies: 3
    Last Post: 08-07-2009, 05:19 PM
  2. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 06:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 01:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

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