Thread: If statement's

  1. #1
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172

    If statement's

    Here's the code(if you could call it that)
    Code:
    #include <iostream.h>
    
    int main()
    {
      int input;
      cout<<"1. play game: ";//mabey no two dot's?
      cout<<"2. Load game: ";
      cout<<"3. play multiplayer: ";
      cout<<"4. Exit: ";
      cin>>input;
      {
        if(input==1)
        cout<<"Play on";
        if(input==2)
        cout<<"Your game is loading";
        if(input==3)
        cout<<"Play multiplayer, your gonna go down";
        if(input==4)
        cout<<"wooooos";
        else 
        cout<<"program over";
      }
      return 0;
    }
    The problem is with the executable when it's run, when you input 1,2,3, or 4 it display's the text given with cout, but it also display's
    the Else "program over" even when i'm inputting 1,2,3,or 4.
    So if anyone feel's like it answer me that. Thank's
    loopy
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    25

    Lightbulb aaah

    Try to use: else if

    Im a newb too. But I think it will work..
    Tell me if it works=)
    Code:
        if(input==1)
        cout<<"Play on";
        else if(input==2)
        cout<<"Your game is loading";
        else if(input==3)
        cout<<"Play multiplayer, your gonna go down";
        else if(input==4)
        cout<<"wooooos";
        else 
        cout<<"program over";

  3. #3
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172

    else if

    No unfortunatly that make's it not even compile.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    25

    Angry Strange.

    Code:
    #include <iostream.h>
    
    int main()
    {
      int input;
      cout<<"1. play game: ";//mabey no two dot's?
      cout<<"2. Load game: ";
      cout<<"3. play multiplayer: ";
      cout<<"4. Exit: ";
      cin>>input;
    
        if(input==1)
        cout<<"Play on";
        else if(input==2)
        cout<<"Your game is loading";
        else if(input==3)
        cout<<"Play multiplayer, your gonna go down";
        else if(input==4)
        cout<<"wooooos";
        else 
        cout<<"program over";
    
    
      return 0;
    }

    This code works on my compiler.. I use Dev C++ 4

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The problem with a series of if statements and then a final else condition is that it doesn't perform quite as you might expect. Think of each if statement as its own entity and those entities can only have one else. So 1, 2, and 3 are tested but it any of those is true, the final if will fail and the else will also be executed, resulting in the unusual output. This works on my system:
    Code:
    #include <iostream.h>
    
    int main()
    {
      int input;
      cout<<"1. play game: ";//mabey no two dot's?
      cout<<"2. Load game: ";
      cout<<"3. play multiplayer: ";
      cout<<"4. Exit: ";
      cin>>input;
      {
        if(input==1)
        cout<<"Play on";
        else if(input==2)
        cout<<"Your game is loading";
        else if(input==3)
        cout<<"Play multiplayer, your gonna go down";
        else if(input==4)
        cout<<"wooooos";
        else 
        cout<<"program over";
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int main()
    {
      clrscr();
      int input;
      cout<<"1. play game: ";//mabey no two dot's?
      cout<<"2. Load game: ";
      cout<<"3. play multiplayer: ";
      cout<<"4. Exit: ";
      cin>>input;
      {
        if(input==1)
        cout<<"Play on";
        else if(input==2)
        cout<<"Your game is loading";
        else if(input==3)
        cout<<"Play multiplayer, your gonna go down";
        else if(input==4)
        cout<<"wooooos";
        else
        cout<<"program over";
      }
      getch();
      return 0;
    }
    this should compile, what compiler are you using? Also you might want to try a switch statement

    Code:
    cin >> input;
    switch (input) {
          case 1:
                cout<<"Play on";
                break;
          case 2: 
                cout<<"Your game is loading";
                break;
          case 3:  
                cout<<"Play multiplayer, your gonna go down";
                break;
          case 4:
               cout<<"wooooos";
               break;
          default:
                cout<<"program over";
    }
    Last edited by xlnk; 03-24-2002 at 06:39 PM.
    the best things in life are simple.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    25

    remove the { }

    Thats the problem in your code..

    { //Should not be there
    if(input==1)
    cout<<"Play on";
    else if(input==2)
    cout<<"Your game is loading";
    else if(input==3)
    cout<<"Play multiplayer, your gonna go down";
    else if(input==4)
    cout<<"wooooos";
    else
    cout<<"program over";
    } // neither should it be here.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Thats the problem in your code..
    That's not a problem, you can place a block anywhere you want in your program. Free floating blocks aren't very common, but they are quite legal.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172

    Red face My bad thing, hehe

    I'm actually at the case switch area of the tut on this site and it say's to mentally put in if statment's in the place of the case switch, so i decided to write this little prog. this worked
    Code:
    #include <iostream.h>
    
    int main()
    {
      int input;
      cout<<"1. play game: ";//mabey no two dot's?
      cout<<"2. Load game: ";
      cout<<"3. play multiplayer: ";
      cout<<"4. Exit: ";
      cin>>input;
      {
        if(input==1)
        cout<<"Play on";
        else if(input==2)
        cout<<"Your game is loading";
        else if(input==3)
        cout<<"Play multiplayer, your gonna go down";
        else if(input==4)
        cout<<"wooooos";
        else 
        cout<<"program over";
      }
      return 0;
    }
    thank's alot you guy's.
    I'm useing djgpp(could'nt get borland to work)
    The if statment's section does'nt mention "else if"
    There's probibly alot more it doesn't mention..my..head is ...getting filled with...fuzzyness.
    thank's
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    22

    imo

    from what i've seen case statements are much easier to use for something like what your describing, so u might want to get in the habit of using them anywayz.

  11. #11
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    Now this is what i call bringing up the dead. Die, thread, die.......
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. C++ If Statements Help
    By moporho in forum C++ Programming
    Replies: 19
    Last Post: 01-18-2008, 08:40 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Need help with "if" statements
    By Harryt123 in forum C Programming
    Replies: 22
    Last Post: 05-14-2006, 08:18 AM