Thread: cin.get(); doesn't work even with cin.ignore();

  1. #1
    Code Master calumn's Avatar
    Join Date
    May 2006
    Location
    Scotland
    Posts
    16

    Post cin.get(); doesn't work even with cin.ignore();

    I have just started to learn C++ and find it very fun. I know VB very well and know a bit of Pascal. I was writing my first program and this is all the code.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    using namespace std;
    
    int main()
    {
     cout<<"Hello\n";
    cout<<"Please enter your name\n";
    
    int name;
    
    cin>> name;
    cin.ignore();
    cout<<"Now enter your age\n";
    
    int age;
    
    cin>> age;
    cin.ignore();
    cout<<"So you are called"<< name <<"and you are"<< age <<"years old\n";
    cin.get();
    }
    The program is VERY simple and it is meant to ask for your name and age and then say it back to you. When it starts is says this:

    Hello
    Please enter your name

    I enter my name and press enter but instead of asking me how old I am it ends the program. I searched the forum and found a thread that said to add cin.ignore(); so that the enter was ignored. I added this but the same thig happens. Debbuging finds nothing and I don't know how to fix it.
    Any help would be goo
    Thanks
    Calum N

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Several things. First you are trying to read a name (which is a string) into an int variable, which holds a number. That won't work. You have to #include <string> and make name a string variable. Second, you are using <iostream.h> which is outdated and non-standard. Switch to <iostream>. Finally, make sure that when you enter your name, you only enter one word. You can code your program so that it can take in multiple words, but that is more advanced so don't try to do that just yet.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Change
    Code:
    int name;
    to
    Code:
    char name[256];

  4. #4
    Code Master calumn's Avatar
    Join Date
    May 2006
    Location
    Scotland
    Posts
    16
    Thanks Dave and cunnus
    I thought about iostream.h but Dev C++ had iostream.h as the default so I left it.
    Thanks

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Or do what davide said and:
    Code:
    #include <string>
    
    string name
    Do you know how to change what Dev-C++ shows you when you make a new source?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> char name[256];
    I would not do that. There's no reason to use a C style string here.

    Use string instead.

  7. #7
    Code Master calumn's Avatar
    Join Date
    May 2006
    Location
    Scotland
    Posts
    16
    I have changed the char thing into string.
    I wondered what char did.
    I don't know how to change what Dev C++ shows.
    I have added if things so that it says whether you are a child or an adult
    This is my updated code:
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    using namespace std;
    
    int main()
    {
     cout<<"Hello\n";
    cout<<"Please enter your first name and press enter\n";
    
    string name;
    
    cin>> name;
    cin.ignore();
    cout<<"Now enter your age\n";
    
    int age;
    
    cin>> age;
    cin.ignore();
    cout<<"So you are called "<< name <<" and you are "<< age <<" years old\n";
    if ( age < 18) {
    cout<<"You are a child!\n";
    }
    else if ( age > 19) {
    cout<<"You are old\n";
    }
    
    cin.get();
    return 0;
    }

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by calumn
    I have changed the char thing into string.
    I wondered what char did.
    string is a C++ implementation of a char array with checks and boundaries and dynamic allocation and more all rolled into a nice transparent package.
    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

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Code:
    if ( age < 18) {
    cout<<"You are a child!\n";
    }
    else if ( age > 19) {
    cout<<"You are old\n";
    }
    Well that's great, but what if, when asked, the user enters 18? Your program explodes. Fix that.

  10. #10
    Code Master calumn's Avatar
    Join Date
    May 2006
    Location
    Scotland
    Posts
    16
    I have been working on it a bit and have been using more advanced ifs.
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    using namespace std;
    
    int main()
    {
     cout<<"Hello\n";
    cout<<"Please enter your first name and press enter\n";
    
    string name;
    
    cin>> name;
    cin.ignore();
    cout<<"Now enter your age\n";
    
    int age;
    
    cin>> age;
    cin.ignore();
    cout<<"So you are called "<< name <<" and you are "<< age <<" years old\n";
    if ( age <= 12 and age > 0) {
    cout<<"You are a child!\n";
    }
    else if ( age >= 13 and age <= 19) {
    cout<<"You are a teenager\n";
    }
    else ( age > 19 and age <= 55) {
    cout<<"You are an adult\n";
    }
    cin.get();
    return 0;
    }
    I added another else but it doesn't seem to work
    I will add another one saying over 55 after i get this to work but it doesn't seem to.

    These are the errors:
    c:\docume~1\neilson\mydocu~1\progra~2\c__~1\abouty ~1.cpp: In function `int main()':

    c:\docume~1\neilson\mydocu~1\progra~2\c__~1\abouty ~1.cpp:23: parse error before `age'

    c:\docume~1\neilson\mydocu~1\progra~2\c__~1\abouty ~1.cpp:25: confused by earlier errors, bailing out

    Thanks for all your help

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    'and' isn't a C++ keyword... you need '&&'
    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

  12. #12
    Code Master calumn's Avatar
    Join Date
    May 2006
    Location
    Scotland
    Posts
    16
    I used to have the && but someone on msn told me to change it to and and it still worked.
    I have changed the and back into && but it still gives me the exact same errors

  13. #13
    Code Master calumn's Avatar
    Join Date
    May 2006
    Location
    Scotland
    Posts
    16
    Yay I fixed it:
    Code:
    {
     cout<<"Hello\n";
    cout<<"Please enter your first name and press enter\n";
    
    string name;
    
    cin>> name;
    cin.ignore();
    cout<<"Now enter your age\n";
    
    int age;
    
    cin>> age;
    cin.ignore();
    cout<<"So you are called "<< name <<" and you are "<< age <<" years old\n";
    if ( age <= 12 && age > 0) {
    cout<<"You are a child!\n";
    }
    else if ( age >= 13 && age <= 19) {
    cout<<"You are a teenager\n";
    }
    else if ( age > 19 && age <= 55) {
    cout<<"You are an adult\n";
    }
    cin.get();
    return 0;
    }
    I didn't have an if after the last else

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 'and' isn't a C++ keyword... you need '&&'
    Yes, it is. However, you should probably just use && since that is the more commonly used version.

  15. #15
    Code Master calumn's Avatar
    Join Date
    May 2006
    Location
    Scotland
    Posts
    16
    It worked fine both ways
    I have added it so it will always tell you your age like a child or teenager

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    using namespace std;
    
    int main()
    {
     cout<<"Hello\n";
    cout<<"Please enter your first name and press enter\n";
    
    string name;
    
    cin>> name;
    cin.ignore();
    cout<<"Now enter your age\n";
    
    int age;
    
    cin>> age;
    cin.ignore();
    cout<<"So you are called "<< name <<" and you are "<< age <<" years old\n";
    if ( age <= 12 && age > 0) {
    cout<<"You are a child!\n";
    }
    else if ( age >= 13 && age <= 19) {
    cout<<"You are a teenager\n";
    }
    else if ( age > 19 && age <= 55) {
    cout<<"You are an adult\n";
    }
    else if ( age > 55) {
    cout<<"You are ancient\n";
    }
    else if ( age < 0) {
    cout<<"You haven't been born yet\n";
    }
    cin.get();
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  2. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 11:53 AM
  3. help getting program to work
    By jlmac2001 in forum C Programming
    Replies: 2
    Last Post: 11-13-2002, 11:04 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM