Thread: If Program from the tutorial section

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    4

    Question If Program from the tutorial section

    I am doing the if program on this website. It wont print out 'you are young", or "you are old". What is wrong with the code, or do I have to have an option enabled in my compiler?

    Code:
    //This will give a remark after inputting an age.
    
    #include <iostream.h>
    
    int main()
    
    {
        int age;
        
        cout<<"Please input your age: ";
        
        cin>>age;
        
        if(age<100)
        
        {
        
            cout<<"You are pretty young!";
            
        }
        
        else if(age==100)
        
        {
        
            cout<<"You are old";
            
        }
        
        else 
        
        {
            cout<<"You are really old";
        
        }
        
         return 0;
        
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Hmmm, try some endl's at the end of those cout's.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    4
    Can you post an example, I am confused at your response.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    161
    Code:
      cout << "hello world!" << endl;
    The << endl on the end adds a newline and flushes the buffer.

    If you never flush the output buffer, then the data that you are trying to display may never get written to the screen (as you've noticed in your program).

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Use the new header, <iostream>, and
    using namespace std;

    The tutorials need updating!
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    4
    Could someone e-mail me what the proper code is, or can the site have the code updated? I am a newbie at C++, and I am trying to follow the tutorials. E-mail me through the profile if you can help out.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If your console's disappearing, you may need to add this before the return 0;

    cin.ignore();
    cin.get();

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    4
    Thanks swoopy it worked. But how do I use the
    Code:
    endl;
    , like another member sugggested?

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Just add it to your cout's anytime you need a newline.
    Code:
        if(age<100)
        
        {
        
            cout<<"You are pretty young!" << endl;
            
        }
        
        else if(age==100)
        
        {
        
            cout<<"You are old" << endl;
            
        }
        
        else 
        
        {
            cout<<"You are really old" << endl;
        
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Add 4 accounts to this program
    By kellymart87 in forum C++ Programming
    Replies: 5
    Last Post: 05-02-2007, 04:31 AM
  2. Compiling Tutorial program with Dev-c++
    By h3ro in forum C++ Programming
    Replies: 15
    Last Post: 10-24-2006, 03:02 AM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  5. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM