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?

aandis
  • 4,084
  • 4
  • 29
  • 40
  • 1
    [Why is “using namespace std” considered bad practice?](http://stackoverflow.com/q/1452721/995714) – phuclv Apr 23 '17 at 15:47

3 Answers3

60

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.

Community
  • 1
  • 1
jrok
  • 54,456
  • 9
  • 109
  • 141
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;
    }
}
goelakash
  • 2,502
  • 4
  • 40
  • 56
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.

Community
  • 1
  • 1
Maddy Byahoo
  • 151
  • 1
  • 9
  • Number of words in the string are unknown. Any method to read the words with cin then? – aandis Aug 19 '13 at 17:03
  • http://stackoverflow.com/questions/8683302/how-to-split-a-space-separated-string-into-multiple-strings-in-c – Maddy Byahoo Aug 19 '13 at 17:06
  • you can do something with getline and deliminating characters too http://www.minich.com/education/wyo/cplusplus/cplusplusch10/getfunction.htm – Maddy Byahoo Aug 19 '13 at 17:15