Taking multiple inputs in a single line

So most of the programming problems given in sites like code jam or code chef ask for taking multiple inputs (that are integers in a single line). So I did I bit of research on it (googled it, actually).

I got recommendations like, take the imputs as a single string and then parse the strinf into respective integer inputs. So I spent my hole morning doing that, obviosuly there were errors, I tried to sownload missing C libraries, learnt some new functinos in C++. A pretty neat code on parsing the string into words that I cam across was:

#include
#include
#include
using namespace std;

int main()
{
string s(“Somewhere down the road”);
istringstream iss(s);

do
{
string sub;
iss >> sub;
cout << “Substring: ” << sub << endl;
} while (iss);

return 0;
}

But after much head scratching I found another solution to the problem:

cin>>a>>b and it worked!

Advertisements

One Comment on “Taking multiple inputs in a single line”

  1. anantzoid says:

    The header files above are, iostream, string and sstream


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s