+ 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; }
6 Answers
+ 1
you have to use "std::getline":
https://code.sololearn.com/c4wD32ixK8Av/#cpp
https://en.cppreference.com/w/cpp/string/basic_string/getline
+ 1
Please post your code!
+ 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;
}
+ 1
For example if I enter "Daniel Namjoo" the output will be jus "Daniel" because the compiler counts space as enter.
+ 1
try this:
string fullname;
cout << "\nPlease enter your full name: ";
cin.ignore();
getline( cin, fullname );
cout << fullname << endl;
0
This works for me:
#include <iostream>
using namespace std;
int main() {
string fullname = "John Doe";
cout << fullname << endl;
return 0;
}