Basic stuff/ closing

This is a discussion on Basic stuff/ closing within the C++ Programming forums, part of the General Programming Boards category; Hi when i make a simple c++ program: It closes too early it says please input your age, so i ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    15

    Unhappy Basic stuff/ closing

    Hi when i make a simple c++ program: It closes too early it says please input your age, so i do then press enter and it closes itself i have used systempause("") and cin,get(); but it doesn't get further then my typing in my age?! Please can u help?
    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
      }
      cin,get();
      return 0;
    }
    Oooh also i'm using the compiler 4.0 from www.bloodshed.net
    Last edited by Black&White; 05-16-2004 at 01:35 PM.

  2. #2
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Its not systempause("") its system("PAUSE"); (only on windows machines)
    Also, you have a comma between cin and get instead of a dot .
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    8
    I had this problem A LOT before I figured out what was wrong. First, cin,get(); is not correct. It should be cin.get();

    Next, what is happening is cin.get(); waits for someone to press [ENTER], but is enter not pressed after inputting your age?

    Basically, it is doing this :

    Code:
    What is your age?
    ....enter age....then the user hits [enter]
    result is displayed
    the user having already [enter] when they entered their age, it returns 0 and then ends the program
    So, to fix this problem all you need to do is use two cin.get();s in your coding.

    I hope that helps!

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    15
    were abouts do i put the 2 cin.get();s?

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    15

    eh

    if u go on the cprogramming.com site and then on tutorials it says to enter this in
    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;
    }
    and it's sposed to work. Also were do i put the two cin.get();'s?

  6. #6
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    hmm, I've never heard of anyone using 2 before?

    OK, the above code is normally used in compilers such as VC++ which has a built in function to ask the user to press any key at the end of the program.

    When using compilers such as gcc and its variants (Dev-C++ uses this) then you need to tell the program to wait before it exits if required.

    In windows environemnts this is normally done via system("PAUSE");
    In unix environments people sometimes use an old function getch(); which will also work in windows.

    Probably best you experiment with those 2 and drop the cin.get(); for now.
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  7. #7
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    btw, if you want to use something like this a few times in a program, like when displaying results etc, you can make a quick function as follows:

    Code:
    void pause(char* message) {
       cout << endl << message;
       getch();
       cout << endl;
    }
    you can then call this within your program giving different messages e.g.
    Code:
    pause("Press any key to continue......");
    pause("Press any key to retuen to menu");
    pause("New item added");
    etc...

    hth
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  8. #8
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    btw, nearly forgot, you'll need to include an older header called conio.h in windows.
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    btw, nearly forgot, you'll need to include an older header called conio.h in windows.
    It's non-standard. You can't be assured that it will work in every compiler and every OS. Stick to cin.get() to be sure.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    15

    Benny?!

    So my original question still asks, were do i put the cin.get(); 's or system("PAUSE")?

    Code:
     #include <iostream.h>
               #include <concio.h>
    will that work at the top of my c++ code?

    omg i just tried it and it said error with <concio.h>
    Last edited by Black&White; 05-17-2004 at 10:00 AM.

  11. #11
    Registered User
    Join Date
    Feb 2004
    Posts
    8
    Actually, <conio.h> is the header file, not <concio.h>

    And, as for cin.get(), it goes right before the return 0;

    The reason for using two is because it waits for enter to be pressed, but enter is pressed when entering your age. This is how I figure it, at least. Anyone else know?

    P.S. I use Dev C++ also and the above is how I must do it.

  12. #12
    Registered User
    Join Date
    May 2004
    Posts
    15
    Sorry to sound noobish. It still doesn't work!
    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
       cin.get();
       cin.get();
       return 0;
       }

  13. #13
    Registered User
    Join Date
    Feb 2004
    Posts
    8
    On the last else statement you forgot to close it so it won't work unless you do this :

    Code:
    ....
    else
    {
    cout<<"You are really old"; //executed if no other statement is executed
    [b]}[b]
    cin.get();
    cin.get();
    return 0;
    }
    As for sounding "noobish," don't even worry about that. I'm still new at this too and, believe me, I've asked a LOT noobier questions! Plus, everyone on these boards was a noob at one point or another.

  14. #14
    Registered User
    Join Date
    May 2004
    Posts
    19
    try this, kinda dumb but it works:
    Code:
    #include <iostream.h>
    #include <stdio.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
    getchar();
       return 0;
       }

  15. #15
    Registered User
    Join Date
    May 2004
    Posts
    15
    I've tried em all and i'm afraid no luck





    EDIT
    HOLD ON I GOT IT SORTED. None of that works for that piece but i made this
    Code:
        #include <iostream.h> 
    int
    main() 
    { 
      int thisisanumber; 
      cout<<"Please enter a number:";
      cin>>thisisanumber; 
      cout<<"You entered: "<<thisisanumber;
      cin.get;
      cin.get;
      return 0; 
    }
    and that works well but i think it's all fkd pardon my french with the actual proper code to do with age
    Last edited by Black&White; 05-17-2004 at 02:47 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic stuff but dont work.
    By peckitt99 in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2006, 04:20 AM
  2. References vs Pointer vs copying and storing stuff in classes
    By Monkeymagic in forum C++ Programming
    Replies: 20
    Last Post: 09-01-2006, 10:40 AM
  3. need help with some basic stuff
    By JOlszewski in forum C Programming
    Replies: 2
    Last Post: 01-22-2006, 02:18 AM
  4. Basic Calc
    By Universe in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2002, 11:07 PM
  5. Your stuff
    By smog890 in forum C Programming
    Replies: 6
    Last Post: 06-14-2002, 12:50 AM

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