How to read string with whitespace in C++?

string s;
getline(cin,s);

2 Likes

If you want to input many strings in one line with white spaces then you can try the following code:-

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,out;
    stringstream ss;
    getline(cin,s);
    ss<<s;
    while(ss>>out)
    {
        cout<<out<<endl;
    }
    return 0;
}

But if the input consists of many integers (the number of integers being unspecified) then try the following code :-

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,out;
    stringstream ss;
    getline(cin,s);
    ss<<s;
    int x;
    while(ss>>out)
    {
        stringstream convert(out);
        convert>>x;
        cout<<x<<endl;
    }
    return 0;
}
6 Likes

you can use gets() or getline function to input instead of scanf()

You can input string with white spaces by simply using ***scanf(" %[^\n]s",s);***It will read string up to the newline character. Note the space before the ‘%’ because it will flush all the whitespaces before the input string.If you will not put that then you will not get your desired result.

1 Like

Since strings are not built-in data type in C++, So we use #include<string> use string class in c++ program . We declare string variable as string s; and take input as ‘cin>>s’ but this will only take string upto first white-space. In order to get whole string with white-spaces we use string input function getline as string s; getline(cin,s);

3 Likes

Use getline(cin,s).
If s contains integers use atoi() function in

Use below

std::cin >> noskipws;

Now reading from standard input will keep all whitespace characters as well.

You have to use getline() as stated by all others. But if you have used cin before getline() then don’t forget to use cin.ignore() and then use getline() because we have to remove white space or \n character encountered previously.Between consecutive getline(), do not use cin.ignore().

21 Likes

scanf ("%[^\n]%*c", str);
Works unless you have overflown the limit like there is a question in HackerRank’s 30 Day Challenge, where taking Double before string creats problem in getting a string with spaces

in the first scanf, I usually write it as scanf("%d\n", …) to trash the newline character, there’s no need for extra getchar and also safer just in case there’s some white space next to t :slight_smile:

1 Like

Your reply is awesome… It simply corrected my problem… actually my loop for test cases was taking it’s first string to be blank or something and accordingly performing wrong output. also didn’t give output for the last string entered. but after using your way it’s perfect!
Thanx mate…

perfect explaination for how to use getline.thanx

will not work for CodeChef: Practical coding for everyone

Thanks a lot for sharing this. I was stuck on it since hours finally found a precise explanation!
Have a nice day!

You can use getline() but BEWARE! not to use cin>> before that as it >> does not clear the buffer so getline() will consider the ‘\n’ (the enter key that you pressed after using cin>>) as the input string and will immediately be executed as an empty string.
So use getc(stdin) or getchar() before getline() to clear the buffer.

int n; string str;
cin>>n;
getchar(); //OR getc(stdin)
getline(cin,str);

7 Likes

Thanks a lot. Your ans is perfect and complete.

Thanks alot!

Thank you so much!

It not work if we take “12:20 AM” string

It not work if take “12:09 AM” String