Like Tree2Likes
  • 2 Post By laserlight

"getline" won't work but "cin" will, what am I doing wrong?

This is a discussion on "getline" won't work but "cin" will, what am I doing wrong? within the C++ Programming forums, part of the General Programming Boards category; Good afternoon everyone, I was reading the "Jumping into c++" book when I got to exercise 4-1 that states: "Ask ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Lisbon, Portugal
    Posts
    3

    Question "getline" won't work but "cin" will, what am I doing wrong?

    Good afternoon everyone,

    I was reading the "Jumping into c++" book when I got to exercise 4-1 that states:

    "Ask the user for two users' ages, and indicate who is older; behave differently if both are over 100"

    So I wrote my code (but I'm only stating what matters)

    Code:
        string name1;
        string name2;
        int age1;
        int age2;
    
        cout << "User 1 please state your name: " << endl;
        getline( cin, name1, '\n' );
        cout << "User 1 please tell us your age: " << endl;
        cin >> age1;
    
        cout << "User 2 please state your name: " << endl;
        getline( cin, name2, '\n' );
        cout << "User 2 please tell us your age: " << endl;
        cin >> age2;
    So here is my problem:

    Immediately after the "User 2 please state your name: " is printed on the screen, "User 2 please tell us your age: " is printed, making it so I can't type the User 2' name, this happens using the "getline( cin, name2, '\n' )" (underlined), but when I use a "cin >> name2", the program works just fine.

    What am I doing wrong? Can someone help?

    Thanks,

    Rodrigo

  2. #2
    Registered User sirama's Avatar
    Join Date
    Jun 2012
    Location
    Bangalore
    Posts
    7
    So you don't get that doubt for your User 1 input?

    In the console, I hope you can't move your cursor wherever you want and provide the input.

    User 2 please state your name: X
    User 2 please tell us your age: Y

    Printing the Question like said above is possible. But providing the input in the location X, Y is not possible. The input will be given like:
    User 2 please state your name:
    User 2 please tell us your age: MyName
    20

  3. #3
    Registered User
    Join Date
    Oct 2012
    Location
    Lisbon, Portugal
    Posts
    3
    @sirama

    That is what's bothering me, everything works fine for user1, but when I get to user2 I get something like

    "User 1 please state your name:
    x
    User 1 please tell us your age:
    23
    User 2 please state your name:
    User 2 please tell us your age:
    24"

    then I comes the output..

    "is older" (since no name was given for the 2nd user)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Look at this line:
    Code:
    cin >> age1;
    The user enters an age value that is stored into age1. The act of entering the input results in a newline left in the input buffer. This newline is not read. Now, when you do this:
    Code:
    getline( cin, name2, '\n' );
    The newline is read, except that you wanted to read the next line. The appropriate solution is to use cin.ignore() to ignore the newline first.
    riafs and sirama like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2012
    Location
    Lisbon, Portugal
    Posts
    3
    @laserlight

    Thank you for the explanation.

    Problem solved =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-19-2012, 07:15 AM
  2. nbin=fopen("input.txt","a"); doesn't work?
    By Adam Rinkleff in forum C Programming
    Replies: 2
    Last Post: 06-23-2011, 03:57 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread


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