IF statements

This is a discussion on IF statements within the C++ Programming forums, part of the General Programming Boards category; Hey all. I'm new to programming, and im currently at lesson 2 about If statements. I used the example given ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    5

    IF statements

    Hey all.

    I'm new to programming, and im currently at lesson 2 about If statements. I used the example given in the thread, and tried to create one very similar, but it's not made correctly.

    Here is the code, hope someone can tell me whats wrong:

    Code:
    #include iostream
    
    using namespace std;
    
    int main()
    
    {
        int age;
    
        cout<<"How old are you? ";
        cin>> age;
        cin.ignore();
        if (age < 20); {
                cout<<"You are young.\n";
        }
        else if (age = 20) {
             cout<<"You are pretty.\n";
        }
        else (age > 20) {
             cout<<"You are old!\n";
        }
    }

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    no semicolon after a 'IF' statement allowed:
    Code:
    if (age < 20);
    'else' is not a conditional statement.. it is a block of code that is executed if all other 'if's or 'else if's' have been attempted:
    Code:
    else (age > 20)
    
    //should just be
    
    else 
    { //stuff here }
    Last edited by The Brain; 03-05-2006 at 08:07 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > else if (age = 20)
    Also, use == for comparison,
    = is assignment.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    Hmm.. It's still not working, the program says there is something wrong with this line:

    Code:
    cout<<"Please input your age: ";
    I tried to fix by adding \n, but it's still not working.

    Here's the hole code again:

    Code:
    #include iostream
    
    using namespace std;
    
    int main()
    
    {
        int age;
    
        cout<<"Please input your age: ";
        cin>> age;
        cin.ignore();
        else (age < 20) {
             cout<<"You are young.\n";
        }
        else if (age == 20) {
             cout<<"You are pretty.\n";
        }
        else (age > 20) {
             cout<<"You are old!\n";
        }
    }
    Last edited by Viperz0r; 03-05-2006 at 09:47 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Your first else should be an if.
    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

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    Still doesnt work, look at my post again. The program (dev-C++) makes an error at this line, and i really can't see whats wrong. I tried adding \n in the end, but that doesnt help.

    Code:
     cout<<"Please input your age: ";

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > The program (dev-C++) makes an error at this line
    Well posting the actual error message would help.

    Also, a single line in isolation doesn't say much. It's also pretty likely that the actual error is on a preceding line and this is just the first line that the compiler notices that there is something wrong.

    For such a short program, reposting the whole thing again is the best course.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Could be that you use #include iostream instead of #include <iostream>, but the compiler should complain about that.
    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

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    #include iostream
    Can't imagine 3 people missed this one
    See anything that's missing?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    The error line is:
    'cout' undeclared (first use this function).

    This is the hole code:
    Code:
    #include iostream
    
    using namespace std;
    
    int main()
    
    {
        int age;
    
        cout<<"Please input your age: ";
        cin>> age;
        cin.ignore();
        if (age < 20) {
             cout<<"You are young.\n";
        }
        else (age == 20) {
             cout<<"You are pretty.\n";
        }
        else (age > 20) {
             cout<<"You are old!\n";
        }
    }

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    I had to use:

    <iostream>
    instead of
    iostream

    thx for all the replies.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > Here is the code, hope someone can tell me whats wrong:
    Your compiler will tell you what's wrong.

    Code:
    $ g++ foo.cpp
    foo.cpp:1:10: error: #include expects "FILENAME" or <FILENAME>
    foo.cpp: In function ‘int main()’:
    foo.cpp:10: error: ‘cout’ was not declared in this scope
    foo.cpp:11: error: ‘cin’ was not declared in this scope
    foo.cpp:16: error: expected primary-expression before ‘else’
    foo.cpp:16: error: expected `;' before ‘else’
    foo.cpp:19: error: expected primary-expression before ‘else’
    foo.cpp:19: error: expected `;' before ‘else’
    Start at the top, and work your way through the error messages. Just fixing the #include gets rid of 3 errors.

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, 11: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, 12:36 PM
  5. Need help with "if" statements
    By Harryt123 in forum C Programming
    Replies: 22
    Last Post: 05-14-2006, 09:18 AM

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