26

I just started learning C++. I was just playing around with it and came across a problem which involved taking input of a string word by word, each word separated by a whitespace. What I mean is, suppose I have

   name  place animal 

as the input. I want to read the first word, do some operations on it. Then read the second word, do some operations on that, and then read the next word, so on.

I tried storing the entire string at first with getline like this

    #include<iostream>
    using namespace std;
    int main()
    {
     string t;
     getline(cin,t);
     cout << t; //just to confirm the input is read correctly
    }

But then how do I perform operation on each word and move on to the next word?

Also, while googling around about C++ I saw at many places, instead of using "using namespace std" people prefer to write "std::" with everything. Why's that? I think they do the same thing. Then why take the trouble of writing it again and again?

1

3 Answers 3

62

Put the line in a stringstream and extract word by word back:

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
    string t;
    getline(cin,t);

    istringstream iss(t);
    string word;
    while(iss >> word) {
        /* do stuff with word */
    }
}

Of course, you can just skip the getline part and read word by word from cin directly.

And here you can read why is using namespace std considered bad practice.

7
  • I tried reading with cin. But since the words are separated by a whitespace, cin seemed to read only the first word. A simple cin t; cout t; shows that. Hence I switched to getline.
    – aandis
    Commented Aug 19, 2013 at 16:55
  • @jrok getting compilation error on the code you gave. error: variable ‘std::istringstream iss’ has initializer but incomplete type
    – aandis
    Commented Aug 19, 2013 at 17:13
  • @zack Did you include <sstream>?
    – jrok
    Commented Aug 19, 2013 at 17:20
  • @jrok what's the "iss" for ?
    – aandis
    Commented Aug 19, 2013 at 17:27
  • what exactly is istringstream ? A modified string type? Commented Jun 1, 2015 at 3:47
6

(This is for the benefit of others who may refer)

You can simply use cin and a char array. The cin input is delimited by the first whitespace it encounters.

#include<iostream>
using namespace std;

main()
{
    char word[50];
    cin>>word;
    while(word){
        //Do stuff with word[]
        cin>>word;
    }
}
1

getline is storing the entire line at once, which is not what you want. A simple fix is to have three variables and use cin to get them all. C++ will parse automatically at the spaces.

#include <iostream>
using namespace std;

int main() {
    string a, b, c;
    cin >> a >> b >> c;
    //now you have your three words
    return 0;
}

I don't know what particular "operation" you're talking about, so I can't help you there, but if it's changing characters, read up on string and indices. The C++ documentation is great. As for using namespace std; versus std:: and other libraries, there's already been a lot said. Try these questions on StackOverflow to start.

3

Not the answer you're looking for? Browse other questions tagged or ask your own question.