how to stop strings separating: cin

I'm currently learning c++ from programming books. Codeblocks is the IDE I am using. One of the exercises asks me to compare two string sizes inputted by the user and display the result all using a do while loop. The problem I have seems to be cropping up in some programs. When I input the two strings, I intend to press the enter button to mark the end of creating the first string and to then start the input process on the second string, yet the second string is interpreted literally as the second word. Whatever I put after the first space will be used as the second string. While I know this has useful implications in many programs, I'd like to know whether there is a way to force a change in this behavior so that the program starts reading input for the second string after I press enter.
hmm if you are using cin, try using cin.ignore();
if that doesn't work please paste the code up here so we have a better understanding.
You can use getline

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string name;

    getline( cin, name );

    cout << "Hello, " << name << "!";
}


http://www.cplusplus.com/reference/string/string/getline/
.ignore didnt work, here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
using namespace std;

int main()
{
    cout << "Enter two strings to compare length\nNo pun intended\n\n";
    string tst;
    do {
        string v1, v2;
        cin >> v1;
        cin >> v2;
        if (v1.size() > v2.size()) {
            int size1 = v1.size() - v2.size();
            cout << "input 1 is bigger than input 2 by " << size1;
        }
        if (v2.size() > v1.size()) {
            int size2 = v2.size() - v1.size();
            cout << "input 2 is bigger than input 1 by " << size2;
        }
        if (v2.size() == v1.size()) {
            int size3 = v1.size();
            cout << "both inputs are the same: " << size3;
        }
        cout << "\n\nWould you like to make another comparison?\nPlease enter Yes or No\n\n";
        cin >> tst;
    } while (!tst.empty() && tst[0] != 'n' && tst[0] == 'y');
}
use getline like the guy below me suggested
getline worked perfectly. Thanks for the help. Really appreciate it since it's been bug-ging me for ages.
Topic archived. No new replies allowed.