getline in a class

Hi folks, I'm only just getting into classes. I wrote a simple one to get employee imformation. When I got to read in the name string it just skips right past it, with this output:
Enter name: Enter age: <cursor here>
//lets me proceed

Here's my class header:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef CLASS_EMPLOYEE_H
#define CLASS_EMPLOYEE_H

#include <string>

class Employee
{
private:
    int m_nID;
    std::string m_strName;
    short m_nAge;
    double m_dWage;
    double m_dHours;

public:
    // single setters
    void setID(int nID) { m_nID = nID; }
    void setName(std::string strName) { m_strName = strName; }
    void setAge(short nAge) { m_nAge = nAge; }
    void setWage(double dWage) { m_dWage = dWage; }
    // complete setter
    void setEmployee()
    {
        // get the data
        static int s_nCurrEmployeeID = 1;
        std::cout << "Employee ID: " << s_nCurrEmployeeID << std::endl << std::endl;

        std::cout << "Enter name: ";
            std::string strName;
            std::getline(std::cin, strName);
        std::cout << "Enter age: ";
            short nAge;
            std::cin >> nAge;
        std::cout << "Enter wage: ";
            double dWage;
            std::cin >> dWage;
        std::cout << "Enter hours: ";
            double dHours;
            std::cin >> dHours;
        std::cout << std::endl;

        // set the data
        m_nID = s_nCurrEmployeeID;
        m_strName = strName;
        m_nAge = nAge;
        m_dWage = dWage;
        m_dHours = dHours;

        s_nCurrEmployeeID++;
    }

    // getters
    /* ... */
};

#endif // CLASS_EMPLOYEE_H


I used getline(std::cin, strName); to read in the name as I was taught this would allow whitespace (for a full name). The getline works as expected when I use it in main, but something is up with it being in this class?

I never get my head around strings / C-style strings!

Thankyou all.
Last edited on
closed account (Dy7SLyTq)
have you tried flushing the input buffer?
I'm ashamed to say I do not know how to do that! I will look it up. I'm learning from a website and book (with the website having a substantial head start), but I trust the book will eventually teach this :)

Edit: I put this std::cin.ignore(1000, '\n'); which worked.

Thanks
Last edited on
closed account (Dy7SLyTq)
its easy to understand. so when you type something on the keyboard, it is stored in a buffer, to be later grabbed by the program. c++ reads in from the buffer into a variable up until it hits white space (ie [space] tab newline etc) so getline reads in all of thats in the buffer, so if you already had junk left in the buffer, it will be read in with getline, giving it a value unintenionally. if you write cin.flush() above it, it should work

edit: ignore the commands i told you to write. instead do:
http://www.cplusplus.com/forum/beginner/107404/#msg582623
Last edited on
Thanks for the explanation DTS :)

Is there a right time and place to flushing the buffer? Should it be after every this or that or for when I'm reading in lines?
Last edited on
closed account (Dy7SLyTq)
really whenever your about to deal with strings. i dont think it will do much to your program speed/memory wise to flush it at every input though
Topic archived. No new replies allowed.