views:

45

answers:

1

First consider this sample C++ code:

std::string input1, input2, input3;
std::cout << "Enter Input 1: ";
std::cin >> input1;
std::cout << std::endl << "Enter Input 2: ";
std::cin >> input2;
std::cout << std::endl << "Enter Input 3: ";
std::cin >> input3;

If for input1 I enter something like "Good day neighbors" then input1 is set to "Good", input2 is set to "day" and input 3 is set to "neighbors". Im not even given the opportunity to set values for input2 and input3.

So my question is: How can I input a string of text that include spaces into a single string without it (for lack of better terminology) breaking up and overflowing into subsequent calls to the input stream?

Thanks in advance to any and all answers received.

+7  A: 

You can use std::getline:

std::getline(std::cin, input1);
...
std::getline(std::cin, input2);
...
std::getline(std::cin, input3);
AraK
Works like a charm. Thanks.
Rob S.