Pregunta

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 !

¿Fue útil?

Solución

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.

Otros consejos

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top