Problem with cin.getline

This is a discussion on Problem with cin.getline within the C++ Programming forums, part of the General Programming Boards category; Here's what is shown: Code: Input name: John Mendez Input newvar1: 2 Input newvar2: 5 Input newvar3: 1 Input name: ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    Problem with cin.getline

    Here's what is shown:

    Code:
    Input name: John Mendez
    Input newvar1: 2
    Input newvar2: 5
    Input newvar3: 1
    
    Input name: Input newvar1: 5
    Input newvar2: 3
    Input newvar3: 8
    Press any key to continue. . .
    My program isn't allowing me to input a name a second time via getline.
    May someone please help?

    Here's code:
    Code:
    void addperson(char name[40][20],int&newvar1,int&newvar2,int&newvar3,slot)
    {
    cout<<"Input name: ";
    cin.getline(name[slot],20); //Problem is here
    cout<<"Input newvar1: ";
    cin>>newvar1[slot];
    cout<<"Input newvar2: ";
    cin>>newvar2[slot]; 
    //etc
    slot++;
    }
    
    char choice()
    {
    char tchoice;
    cin>>tchoice;
    return tchoice;
    }
    
    void main()
    {
    char name[40][20];
    int newvar1[40],newvar2[40],newvar3[40],slot=0;
    
    do addperson(name,newvar1,newvar2,newvar3,slot);
    while (choice()=='Y');
    }

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    This is most likely the standard problem of not emptying the cin buffer. Try not to mix calls to operator>> with calls to getline(). Things get all out of sync, as you see here.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The getline function reads from the input buffer until it sees a newline character, at which point it removes that character and stops.

    This works the first time around. However, cin>> does things a little different. It skips all whitespace (spaces, tabs, newlines) and then reads in the value and stops.

    What is happening is that after the user types 2 they hit <enter> which leaves a newline in the stream. This is fine here, because cin>>newvar2[slot] will skip over that newline when it reads in the 5. Similarly, cin >> newvar2[slot] will skip the newline that came after 5 and then read in 1.

    Now, the next time through, the newline that came when the user hit <enter> after typing 1 is still there. The getline functions sees this but does not skip it. Instead it thinks it is done, so it stops.

    One solution is to ignore the trailing newline when you use cin>> to read in values. That means adding cin.ignore(); after each call to cin>> (or at least the ones that come before getline). Don't put cin.ignore() after getline because getline already removes that final newline character.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    Thanks Daved again - you've been really helpful.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    More helpful tips for you: you should learn to indent your code (makes it easier to read, helps find bugs) and not use void main because it's undefined. Use int main.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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