class - How to space first_name + last_name C++ -
my question how can use spaces between first_name , last_name after instruct user input name. using code blocks , i'm kinda confused. in dev c++ can do
cout<<first_name<<" "<<last_name;
now doing practicing c++ using variables in class. here code.
#include<iostream> #include<string> using namespace std; class danielclass { public: int setnamefunction(string first_name, string last_name) { cout<<"enter first name here: "; cin>>first_name; cout<<"enter last name here: "; cin>>last_name; name = first_name + last_name; } string getnamefunction() { return name; } private: string name; }; int main() { danielclass nameobject; nameobject.setnamefunction("", ""); cout<<nameobject.getnamefunction(); cout<<"\n\n"; return 0; }
you can add string literals (which term string entered directly code quotes around -- " " in second line of code below) string variables using '+' operator.
you need change line reads:
name = first_name + last_name;
to:
name = first_name + " " + last_name;
Comments
Post a Comment