Question

Let's say i have some class Employee ,and an object Director. For example :

#include <iostream>
using namespace std;

 class Employee
{
 public:
 string firsName;
 string lastName;
 int age;
};

 int main()
{
 Employee Director;
 Director.firstName = "Smith";
 etc....
}

etc...

return 0:
}

How i use a String input to let the user enter Director.firstName and etc. into the object?

cout<<"Enter Directors name: " << endl;
Director.firstName = getline(cin ,firstName);

Thank u very much in advance !

Was it helpful?

Solution

The function getline is used differently. You should pass the string as a reference in which the result should be written. The return value of the function is a reference to the stream, which you can ignore.

In order to pass the firstName you need to add the object of which you want to access the member, like you originally did with the left hand of the assignment.

getline(cin, Director.firstName);

So don't use the assignment operator (=) here.

OTHER TIPS

change Director.firstName = getline(cin, firstName); to getline(cin, Director.firstName);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top