cin question

I have a prety easy question, my mind is just drawing a blank though..

say I'm getting user input, but don't know how many strings they are putting in, how can I read until the end of the input..printing each element out seperately.

Example:
Enter Words: hello cat bird dog
cfansAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DrAskeCommented:
Is It a homework??
you can declare an array of characters
char input[SIZE];
then read the users' input
cin.getline(input,SIZE,'\n');
then use *strtok* function to tokenize it ..

Is that clear??
0
DrAskeCommented:
the easy way to tokenize *input* is:
while(char*token= strtok(input," ");token!=NULL; token = strtok(NULL," "))
                cout<<token<<endl; // printing each element seperately

regards, Ahmad;
0
pcgabeCommented:
Ahmad, should that be a for-loop instead?  ^_^
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

pcgabeCommented:
If the input is on multiple lines, encapsulate the thing in a while-loop:

      cin.getline(input,SIZE);      //primes the pump, so to speak.
      while (strlen(input))            //while the length of 'input' is not zero...
      {
            //tokenizer
            for(char*token= strtok(input," ");token!=NULL; token = strtok(NULL," "))
                  cout<<token<<endl; // printing each element seperately
            //reads the next line (IMPORTANT)
            cin.getline(input,SIZE);
      }
0
rstaveleyCommented:
Arrays and pointers are evil ;-)

Consider using std::string and std::istringstream for this.

Consider using the returned reference from getline as your while condition. It returns a reference to the cin istream, which has a bool operator which is false, when you hit EOF. Likewise, istream operator>> returns a reference to the istream, which you can test in a similar manner.
 
     string line;
     while (getline(cin,line))      
     {
          istringstream istr(line);  
          string token;
          while (istr >> token)
               cout << token << endl;
     }
0
itsmeandnobodyelseCommented:
>>>> but don't know how many strings they are putting in

rstaveley showed you how to read a text line that was terminated by a linefeed character (means the user hit the ENTER key to finish their input). If the words could be separated by different seperator characters you might tokenize the input string like the following:

#include <string>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<string> inputs;
    string input;
   
    cout << "==> ";
    if (getline(cin, input))
    {
        string seperators = " ,.-/|;!";
        int pos = 0;
        int lpos = 0;
        input += seperators[0];  // add a space
        while ((pos = input.find_first_of(seperators, lpos)) != string::npos)
        {
            if (pos > lpos)
            {
               inputs.push_back(input.substr(lpos, pos-lpos));
            }
            lpos = pos+1;
        }
    }
   
    for (int i = 0; i < inputs.size(); ++i)
        cout << inputs[i] << endl;
   
    return 0;
}

Regards, Alex
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itsmeandnobodyelseCommented:
Thanks for the points, but rstaveley should have got some points as well ...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.