I'm just starting to learn C++ and ran into a little bug in my program:
#include <iostream>
using namespace std;
int main() {
string name;
int number;
cout << "Hello!\n";
cout << "Please enter your name: " << flush;
cin >> name;
cout << "Please enter a whole number: " << flush;
cin >> number;
cout << "Thank you for your cooperation, " + name + ". We will be contacting you again soon in regards to your order of " << number << " puppies.\n";
}
When attempting to enter in multiple words (say, No One
) the first time it asks for user input, the program will output the following:
Please enter a whole number: Thank you for your cooperation, No. We will be contacting you again soon in regards to your order of 0 puppies.
I read elsewhere that cin
treats all whitespace the same (so a space would be treated the same way as a return), how could I avoid this problem?