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?
#include <iostream>
#include <string>
usingnamespace 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;
}
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>.
#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;
}
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>
usingnamespace 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!