What if the Entered first Name here have Spaces (e.g. Don Don)? How can i make it run smoothly? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What if the Entered first Name here have Spaces (e.g. Don Don)? How can i make it run smoothly?

https://code.sololearn.com/cIxetbQDq3Cz/?ref=app

1st Sep 2017, 4:58 AM
Jan Paul Echaveria
Jan Paul Echaveria - avatar
2 Answers
+ 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; }
1st Sep 2017, 5:22 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
getline(cin,my_string);
1st Sep 2017, 7:38 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar