#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    1
    Rep Power
    0

    Arrow "easy" C++ help plz


    ok my problem is when i write all the code and then press execute the program work great but when i enter the number to make the program works it close automatically what i coud do to fix this? here is my code:
    (sorry for my english)





    #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"; //Executed if no other statement is executed

    }

    return 0;

    }
  2. #2
  3. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    the program runs and finishes so it exits.

    try pausing the program before the return 0 (if this works in your compiler)

    sleep(5000);//in milliseconds so five seconds

    Better, try putting a while loop and setting up a condition (value ) that the user can enter to leave the program

    Code:
    do
    {
         cout<<"Please input your age (enter zero to exit):  "; 
          cin>>age;
          //chack to see if the user tried to exit
          if(0 != age)
          {
                    //all your code here
          }
    }while(0 != age)
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    6
    Rep Power
    0
    You could either do that or you can simply run your program from a DOS prompt or console NOT from Windows.

    I made the same mistake, when I first started teaching myself C++ programming. :D
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    When I was teaching my students quickly learned to keep a command shell open and they did all runs from the command line, never the Execute button. That button is really only useful for Windows applications.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    4
    Rep Power
    0

    just pause...


    Hi,

    This looks like one of the program I gave to my students...

    we are using Dev-C++ software (which works great on Windows platform apart from Windows XP) and the easiest way for your particular problem is to include this line just before the return 0:

    system ("PAUSE");

    so your program will look like this:

    .
    .
    .
    .
    cout<<"You are really old";
    }

    system ("PAUSE");
    return 0;
    }

    when you use this your string will be displayed and everything will stand still until you press any key (and will display"Press any key to continue...")

    I know that you posted this quite a while ago but just in case...

    Enjoy learning C++!

    Annie.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    37
    Rep Power
    12
    Here is two simple lines of code that could help okay actually 3.

    first #include<conio.h>

    then right before your return 0 include the following:

    cerr<<"Press any key to continue";
    getch();

    the cerr is part of the iostream header file similar to the cout statement allowing you to display something the to the string.

    the getch(); is part of the conio.h header file it basicly allows you to store junk variable allowing you to pause the screen until as in the cerr statement says press any key to continue. then whatever key is pressed is stored then the program will go to the return 0 and then end the program

    hope this helps

IMN logo majestic logo threadwatch logo seochat tools logo