getline (cin,name)

This is a discussion on getline (cin,name) within the C++ Programming forums, part of the General Programming Boards category; Hi I am trying to use getline in order to read a name and surname and sore them in 1 ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    getline (cin,name)

    Hi I am trying to use getline in order to read a name and surname and sore them in 1 variable.
    The program is going over getline like it does not exist.What is the reason for this?
    / /
    Code:
    void Vstore::addemployee(string name)
    
    {float amount;
      string date;
      empsal[name].name=name;
      cout<<"Salary? :";
      cin>>amount;
        empsal[name].sal=amount;
        cout<<"Date "<<name<<" joined us? :"<<endl;
        cin>>date;
        empsal[name].dateJoined=date;
    }
    
    in main() I have
    
    break;
     case 4:
      Vtown.calcprofit();
      break;
      case 5:
      cout<<"Name?   :";
      getline(cin,name);
      Vtown.addemployee(name);
    
    This is what happens when I run the program:
    
    hello
    What would you like to do
    
    1  Get an Employees Details
    2  Add an amount to income
    3  Add an amount to expenses
    4  See current profit
    5  Add an employee
    5
    Name?   :Salary? :
    It should display name,read a value into it and then display salary?

    Thanks

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Add this line right after the call to getline():

    Code:
    cout << "name = \"" << name << "\"" << endl;
    The problem is possibly that you're doing something bad with cin so that the buffer has something inside of it when you call getline(). The above line will tell you what was picked up.

    Be careful with how you're messing with input streams. I suppose a quick fix would be to add another getline(), but you should really identify exactly what is going wrong before you try to fix it.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    Code:
    getline(cin,name);
      cout << "name = \"" << name << "\"" << endl;
    
    gives me:
    
    
    What would you like to do
    
    1  Get an Employees Details
    2  Add an amount to income
    3  Add an amount to expenses
    4  See current profit
    5  Add an employee
    5
    Name?   :name = ""
    Salary? :

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I'm guessing you have a '\n' lying in the buffer before you call getline(). You need to clear the newline char. As I said in my last post, you can just issue another call to getline() to get around the problem, or perhaps more appropriately, you should use the C++ equivalent to a getchar().

    What input operation are you doing beforehand btw?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > cin>>date;
    This leaves a newline

    > getline(cin,name);
    This stops reading after a newline.
    Which if it immediately follows the other variety looks like it does 'nothing'.

    Always use getline() to read a whole line into a std::string.
    Then use a string stream say to extract the information you require.

    See scanf() vs. fgets() + sscanf() in C.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    Thanks for the help.The problem has been soved.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    The other option is to use std::getline(std::cin >> std::ws, name); What this gets is a single line, not including terminator '\n', where the terminator is consumed, with no leading whitespace. This is usually what you want. The only problem with this is when you might want a blank line to return a null string or an error, this silently skips blank lines.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I prefer to just always add cin.ignore() to ignore the newline (or other trailing whitespace) after calls to operator>>.

    Or something like this:

    Wrong input handling with cin
    Last edited by Daved; 04-26-2007 at 12:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 10:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 01:59 PM

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