If I use spaces in a string, it trails over into the next string input

I am using strings, but if the user includes spaces in the input, the next word after the space is used as the next string input. Even if I use cin.ignore, this is still occuring. Is there a way I can fix this?

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()
{
    string x;
    string y;
    string z;
    string g;
        cout << "Hello there traveler! Romeo the royal coachman at your service! erm... my bad... I was getting in ahead of myself. Please, introduce yourself!" << endl;
        cout << "Tell the coachman your name: ";
        cin >> x;
        cin.ignore();
        cout << endl;
        cout << "Ah... " << x << "... That sounds familiar... Where are you from?" << endl;
        cout << "Tell the coachman your hometown: ";
        cin >> y;
        cin.ignore();
        cout << endl;
        cout << "Ohhh! That is where I remember you from! I grew up in " << y << "!";




    return 0;
}
Use
getline( cin, x );
rather than
cin >> x;
This will split inputs at newlines rather than spaces.
Hello theotherlegend27,

As lastchance says "std::getline()" may be a better choice to get what the user has entered.

What you may not understand is that "std::cin >> strVar" will read the input buffer until it finds a new line or white space which ever comes first leaving anything left i the input buffer for the next "std::cin" to extract.

Your use of "std::cin.ignore()" is most likely only ignoring one character of the input buffer. If you want to clear the whole input buffer try this:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.

Hope that helps,

Andy
Hello theotherlegend27,

Just so you can get an idea of how things work:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
#include <limits>

//using namespace std;  // <--- Best not to use.

int main()
{
	std::string x;
	std::string y;
	std::string z;
	std::string g;

	std::cout << "Hello there traveler! Romeo the royal coachman at your service! erm... my bad... I was getting in ahead of myself. Please, introduce yourself!" << std::endl;

	std::cout << "\nTell the coachman your name: ";

	//std::getline(std::cin, x);

	std::cin >> x;
	// <--- This next line not needed with the "std::getline", but makes a good pause.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

	std::cout << std::endl;

	std::cout << "Ah... " << x << "... That sounds familiar... Where are you from?" << std::endl;

	std::cout << "\nTell the coachman your hometown: ";

	//std::getline(std::cin, y);

	std::cin >> y;
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

	std::cout << std::endl;

	std::cout << "Ohhh! That is where I remember you from! I grew up in " << y << "!" << std::endl;

	//  <--- used as a pause here.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	
	return 0;
}


Andy
If there are an unknown number of words in the string, use getline.

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

int main()
{
   string name, hometown;
   cout << "Hello there traveler! Romeo the royal coachman at your service! erm... my bad... I was getting in ahead of myself. Please, introduce yourself!\n";
   cout << "Tell the coachman your name: ";
   getline( cin, name );
   cout << "Ah ... " << name << "... That sounds familiar... Where are you from?\n";
   cout << "Tell the coachman your hometown: ";
   getline( cin, hometown );
   cout << "Ohhh! That is where I remember you from! I grew up in " << hometown << "!\n";
}


Hello there traveler! Romeo the royal coachman at your service! erm... my bad... I was getting in ahead of myself. Please, introduce yourself!
Tell the coachman your name: Dick Turpin
Ah ... Dick Turpin... That sounds familiar... Where are you from?
Tell the coachman your hometown: York
Ohhh! That is where I remember you from! I grew up in York!
Topic archived. No new replies allowed.