0
What if the Entered first Name here have Spaces (e.g. Don Don)? How can i make it run smoothly?
2 ответов
+ 3
Spaces are part of the string, so they shouldn't be a problem depending on what you're doing.
You should make your string variables more localized and pass them. The way you currently have them makes the variables directly accessible from the class and main. Also, you're assigning the first name to one object and the last name to another.
See code below for changes:
#include <iostream>
#include <string>
using namespace std;
class Employee{
private:
string fName, lName;
public:
void setfName(string x){
fName = x;
}
string getfName(){
return fName;
}
void setlName(string y){
lName = y;
}
string getlName(){
return lName;
}
};
int main() {
string fName, lName;
cout << "Enter Employee's first name: ";
cin >> fName;
cout << "Enter Employee's last name: ";
cin >> lName;
Employee obj;
obj.setfName(fName);
obj.setlName(lName);
cout << obj.getfName() << " " << obj.getlName() << endl;
}
+ 1
getline(cin,my_string);