cin.getline error

This is a discussion on cin.getline error within the C++ Programming forums, part of the General Programming Boards category; i am writeing a new program, it has no errors but wen i'm working with it, there's a part that ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    13

    cin.getline error

    i am writeing a new program, it has no errors but wen i'm
    working with it, there's a part that asks 4 info from the user like
    Code:
    cout<<"First name: \n";
    cin.getline(fstname, 90);
    cout<<"Last name: \n";
    cin.getline(lstname, 90);
    both appear after each other the 1st one don't wait till the user
    press enter they appear as:
    First name:
    Last name:
    _

    what shall i do?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Somewhere above those lines you have something like:

    int number;
    cin>>number;

    The >> operator leaves the \n in the input stream from when you hit return after entering some data. And, as you probably know getline() terminates when it reads a \n. So, the first getline() reads in that \n left in the stream and terminates, and execution continues. Note: Unlike the >> operator, getline() removes the \n from the input stream.

    Read #4 here for the explanation and the fix:

    http://www.augustcouncil.com/~tgibso...al/iotips.html

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    This problem gets asked a lot. It got em too when I first started. Before any cin statement call the function cin.ignore(1,'\n') This takes the new line out of the buffer and makes your programs work properly.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Make it easy on yourself:

    Code:
    #include <iostream.h>
    const int SIZE = 50;
    char fullName[SIZE];
    
    int main()
    {
         cout<<"Enter your name: \n";
         cin.getline(fullName, SIZE);
         cout<<"\nHello, "<<fullName<<endl;
    
         return 0;
    }
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    #include <iostream.h>

    Huh?

    And your code does nothing to alleviate the problem the poster is having. Add these lines to the beginning of your code and see what happens:

    int number;
    cout<<"Enter an integer: ";
    cin>>number;
    Last edited by 7stud; 08-07-2003 at 11:53 AM.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    It's not a problem for me (?)...I'm using MSVC
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  7. #7
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Originally posted by funkydude9
    It's not a problem for me (?)...I'm using MSVC
    Ditto in .NET

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It has nothing to do with your compiler--it has to do with what happens in the stream when you use the >> operator and then subsequently use getline().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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