My problem is related to string. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

My problem is related to string.

I declared a variable of type string to store my name and last name. In output I just see my name because I pressed space after it. What is the solution? Here is the code: #include <iostream> using namespace std; int main() { string fullName; cout << "Enter your full name: "; cin >> fullName; cout << "Your full name is " << fullName << endl; return 0; }

28th Apr 2019, 9:24 AM
ALi
ALi - avatar
6 Answers
+ 1
Please post your code!
28th Apr 2019, 9:48 AM
Sergiu Panaite
Sergiu Panaite - avatar
+ 1
#include <iostream> using namespace std; int main() { string fullName; cout << "Enter your full name: "; cin >> fullName; cout << "Your full name is " << fullName << endl; return 0; }
28th Apr 2019, 10:24 AM
ALi
ALi - avatar
+ 1
For example if I enter "Daniel Namjoo" the output will be jus "Daniel" because the compiler counts space as enter.
28th Apr 2019, 10:27 AM
ALi
ALi - avatar
+ 1
try this: string fullname; cout << "\nPlease enter your full name: "; cin.ignore(); getline( cin, fullname ); cout << fullname << endl;
28th Apr 2019, 11:14 AM
Sergiu Panaite
Sergiu Panaite - avatar
0
This works for me: #include <iostream> using namespace std; int main() { string fullname = "John Doe"; cout << fullname << endl; return 0; }
28th Apr 2019, 9:50 AM
Sergiu Panaite
Sergiu Panaite - avatar