A string that returns what I type in?

Ok, so I am trying to write a string that returns what I type in and it works until I start putting spaces between my characters. How would I go about fixing the problem of the string not returning anything after I hit the space bar? Thanks in Advance


#include <string>
#include <iostream>
using namespace std;


int main()
{
string SomethingHere;

cout << "Write something here: ";
cin >> SomethingHere;
cout << SomethingHere << endl;


system("pause");
return 0;
}

Hello. All you need to do is replace your cin with a getline statement. If I understand it correctly, cin (or maybe it's the >> operator or the combination of the two) will only ready your string until it hits a blank space and it stops. The use of getline here will avoid that issue. It'll read the string even if there are spaces.

For example, as you know, if you enter "Hello World" as the cin statement, the program only outputs "Hello" to the screen. Again, your current cin statement only reads until it hits a blank space. If you were to replace the cin with getline, you should be fine. Try this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;


int main()
{
string SomethingHere;

cout << "Write something here: ";
getline(cin, SomethingHere);
cout << SomethingHere << endl;

return 0;
}
it worked thanks
Topic archived. No new replies allowed.