0

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
GarfGlider
  • 13
  • 3
  • You could take a look at [`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline). By default, that reads input a line at a time. – Paul Sanders May 26 '20 at 00:39
  • https://stackoverflow.com/questions/5882872/reading-a-full-line-of-input – Alan May 26 '20 at 00:42
  • 1
    "*I read elsewhere that `cin` treats all whitespace the same*" - it is not `cin` that is treating whitespace this way, it is `operator>>` specifically that is, as it is designed to read **whitespace-delimited** tokens. So, in your example, use `std::getline()` to read multi-word input into `name`, and then `>>` can read the `number` afterwards. – Remy Lebeau May 26 '20 at 00:52

1 Answers1

0

Try using getline http://www.cplusplus.com/reference/string/string/getline/ Is this of any help?

#include <iostream>
#include <string>

int main() {
std::string name;
int number;
std::cout << "Hello!\n";
std::cout << "Please enter your name: " << std::flush;
getline(std::cin, name);
std::cout << "Please enter a whole number: " << std::flush;
std::cin >> number;
std::cout << "Thank you for your cooperation, " + name + ". We will be contacting 
you again soon in regards to your order of " << number << " puppies.\n";

return 0; 
}
  • Getline definitely seems to be the way to go! I was trying to figure out why `getline()` wasn't registering as a function, but noticed #include was added and it fixed that lil' issue. The program also actually works correctly now as well! I will ask: Why put `return 0;` at the end of the program? – GarfGlider May 26 '20 at 20:14
  • Hey, glad I could help. We return 0 because the main() function expects an int to be returned. You can leave it out and the compiler will put it in for you. As a convention 0 is chosen because it tells the caller the program worked successfully, if something goes wrong you might see the program exit with a different value which is useful for debugging. I'm pretty much a beginner myself so here's some people speaking much more lucidly about it https://stackoverflow.com/questions/14928520/so-what-does-return-0-actually-mean – Samuel Campbell May 26 '20 at 23:12