Some help with if and else if statements please.

This is a discussion on Some help with if and else if statements please. within the C++ Programming forums, part of the General Programming Boards category; Hello, i am a beginner in C++ programming. If someone could point out why this program will not compile and ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    Some help with if and else if statements please.

    Hello, i am a beginner in C++ programming. If someone could point out why this program will not compile and run, i would appreciate it.

    Thanks.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int money;
        cout<<"How much money do you own at the moment (rounded to the nearest integer)?";
        cin>> money;
        cin.get();
        if ( money <10 )
        {cout<<"YOU ARE DIRT POOR\n";}
        else if ( 10 >= money && money <= 100 )
        {cout<<"YOU'VE GOT TO GET A JOB!\n";}
        else ( money >100 )
        {cout<<"That should buy you food and shelter for a few days!\n";}
        cin.get();
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    else isn't supposed to have a condition specified because it's condition is else, meaning everything that hasn't been picked up, yet. Just take out the condition after your else statement, and while your at it, fix your formatting.
    Sent from my iPadŽ

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    else ( money >100 )
    else doesn't have conditions.

    This might be simpler to read:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
       int money;
       cout<<"How much money do you own at the moment (rounded to the nearest integer)?";
       cin>> money;
       cin.get();
       if ( money <10 )
       {
          cout<<"YOU ARE DIRT POOR\n";
       }
       else if ( money <= 100 )
       {
          cout<<"YOU'VE GOT TO GET A JOB!\n";
       }
       else
       {
          cout<<"That should buy you food and shelter for a few days!\n";
       }
       cin.get();
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    2
    Thanks guys

Popular pages Recent additions subscribe to a feed

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