How to get string input from user with spaces?

I am making a program and for that I want to take input from user. User may input a string with spaces. Also, I am not sure how many characters will user enter.

So how to take input from user in string format?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

int main()
{
    std::cout << "Enter any string at all, even if it includes spaces or is very long: " << std::flush;
    std::string text;
    if(std::getline(std::cin, text))
    {
        std::cout << "Thanks! You said \"" << text << "\"." << std::endl;
    }
    else
    {
        std::cout << "Something went wrong, I didn't quite catch that." << std::endl;
    }
}
"Thanks!": http://ideone.com/w6C4OB
"Something went wrong,": http://ideone.com/Aiyfsw (no input)

std::string:
http://www.cplusplus.com/reference/string/string/
http://en.cppreference.com/w/cpp/string/basic_string

std::getline:
http://www.cplusplus.com/reference/string/string/getline/
http://en.cppreference.com/w/cpp/string/basic_string/getline
Last edited on
thanks
Topic archived. No new replies allowed.