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 !
change Director.firstName = getline(cin, firstName);
to getline(cin, Director.firstName);
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.