문제

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 !

도움이 되었습니까?

해결책

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top