Thread: Pushing enter exits the Program...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    3

    Pushing enter exits the Program...

    Hello I am a newbie & I know you people have better things to do then help me but I would really like it if someone can help me with this problem.

    I read a program that looks that the one bellow. It works but when I add my age & press enter the program exits. Is there a code that I can add so that the program only exits when I type "exit" or any key word that I choose?

    Code:
    #include <iostream.h>			
    int main()				//Most important part of the program!
    {
      int age;				//Need a variable...
      cout<<"Please input your age: ";	//Asks for age
      cin>>age;				//The input is put in age
      if(age<100)				//If the age is less than 100
      {
         cout<<"You are pretty young!";     //Just to show it works
      }
      else if(age==100)		//I use else just to show an example 
      {
         cout<<"You are old";		//Just to show you it works...
      }
      else
      {
        cout<<"You are really old";	//Executed if no other statement is executed
      }
      return 0;
    }
    You can download the code at the Attach file:

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Read the faq.
    There should be something there that shows you how to pause your program before it finishes.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I read a program that looks that the one bellow. It works but when I add my age & press enter the program exits. Is there a code that I can add so that the program only exits when I type "exit" or any key word that I choose?
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string str;
        cin >> str;
        while(str != "exit")
        {
            // Your code, making sure to ask the user for another value of str.
        }
         return 0;
    }
    If you just want to pause it so you can see the output:

    #include <cstdlib>
    using namespace std;

    ... and ...

    system("pause");

    ... which is in the faq by the way.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    3

    Unhappy X01:

    I know how to pause the Program using the "cin.get();" command but that is not my problem...
    The Problem is when I try to input my age in the program when I press enter the program exits...

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Try using 2 cin.get() statements. If it works, then it means that there was something left in the input buffer, probably from the enter key. Now search the board for how to flush the input buffer.

  6. #6
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    The Problem is when I try to input my age in the program when I press enter the program exits...
    It just appears that way. It prints out the result very fast before it exits. Just add the system("pause") before return 0 and see it for yourself.

    Code:
    #include <iostream>
    
    using namespace std ;
    
    int main()				//Most important part of the program!
    {
      int age;				//Need a variable...
      
      cout<<"Please input your age: ";	//Asks for age
      
      cin >> age;				//The input is put in age
      
      if(age<100)				//If the age is less than 100
      {
         cout<<"You are pretty young!";     //Just to show it works
      }
      else if(age==100)		//I use else just to show an example
      {
         cout<<"You are old";		//Just to show you it works...
      }
      else
      {
        cout<<"You are really old";	//Executed if no other statement is executed
      }
      system("pause") ;
      return 0;
    }

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    3

    X01:

    Thank you so much!
    All of you for Helping me with this
    I realy thank you all...

    Thanks once again...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program doesnt enter function
    By avisik in forum C Programming
    Replies: 5
    Last Post: 12-17-2008, 11:06 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 08:39 AM
  4. My I/O Program crashes, I need some help
    By Guti14 in forum C Programming
    Replies: 4
    Last Post: 09-24-2004, 01:16 AM
  5. can't get my program to let me enter anything into it
    By allthekandi in forum C++ Programming
    Replies: 9
    Last Post: 02-12-2003, 12:15 PM