Thread: Please Read This!

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    2

    Please Read This!

    whenever the user of my program hits ENTER it shuts down. how should the user input their age without closing the program? (i am using bloodshed dev-C++ in case your wondering)
    my code is:
    #include <iostream.h>
    using namespace std;

    int main (int argc, char *argv)
    {
    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
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    1) when posting code to this board use code tags to make your code more readable.

    2) add something like:
    cin.get();
    just before the line
    return 0;

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    2
    it still has the same problem. is there something the user should press (other than enter, apparently) so that the program reads it?

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    before cin.get(); you must clear the '\n' that still remains in the
    stream, how you clear it? with the cin.ignore();

    here's the complete code:

    Code:
    #include <iostream.h>
    using namespace std;
    
    int main (int argc, char *argv)
    {
        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";
        }
        cin.ignore();//clear the new line character
        cin.get();//wait for input
        //you can use : system("pause") too, but is not that portable 
        //and you dont need the cin.ignore if u use system("pause")
       //dont forget to include the <stdlib>header when using, 
       //system("pause");
       return 0;
      }
    please excuse my poor english

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Try:
    char ch;
    cin >> ch;

    instead of

    cin.get();

    I'm probably giving you the wrong syntax of get(). I actually use the cin >> ch; version, personally, but the other version, with correct syntax, seems to be the most common recommendation from the board. You should find plenty of posts by searching.

    If that doesn't work either edit your original post, adding code tags and the modification to your code, or repost your code, using code tags---read the sticky message at the top of the board to learn about code tags.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    two problems preventing me from reading this:

    1- Please Read This!

    2- No Code Tags!
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Not only was the title useless (did you read the posting guidelines?), you didn't read the FAQ either
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read Causes Freezing?
    By Masna in forum C Programming
    Replies: 5
    Last Post: 07-18-2008, 04:31 PM
  2. Read() problems
    By yay4rei in forum C Programming
    Replies: 2
    Last Post: 07-21-2005, 10:47 AM
  3. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM
  4. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  5. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM