Thread: cin.get() ?

  1. #1
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    Question cin.get() ?

    why is it that you cannot have 2 consecutive cin.get() for a char
    in a row such as this:
    (it skips the second prompt and just prints first name)
    Code:
    
    #include<iostream>
    #include<iomanip>
    #include<string>
     
    using namespace std;
     
    int main()
    {
    
    char nameF[40];
    char nameL[40];
    
    cout << "enter first name"<<endl;
    cin.get(nameF,40);
    
    cout << "enter first name"<<endl;
    cin.get(nameL,40);
    
    cout << nameF<< " " << nameL << endl;
    
    return 0;
    
    }

  2. #2
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    Try putting an cin.ignore() after the first cin.get()..
    And btw. you wrote "enter first name" twice

    Edit:
    It might be the termination character from the first cin, that's ruins it for you.
    Try reading this one:
    http://www.cprogramming.com/tips/sho...ount=30&page=0
    Last edited by MipZhaP; 02-27-2005 at 09:05 AM.
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  3. #3
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49
    thanks very much!

  4. #4
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    You're welcome.
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    get() does not extract the delimiting char from the input stream (neither does >>). However, getline() does extract the delimiting char from the input stream.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.get(); not working with my switch statement
    By tenor_jazz13 in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2008, 10:33 PM
  2. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 05:32 PM
  3. Confused about cin.get(); and classes.
    By RaccoonKing in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2005, 11:44 AM
  4. cin.get();
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2005, 07:51 AM
  5. curiosity about cin.get() and cin.getline()
    By ssjnamek in forum C++ Programming
    Replies: 18
    Last Post: 11-30-2003, 01:26 AM