• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

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
0
cfans
Asked:
cfans
  • 2
  • 2
  • 2
  • +1
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
itsmeandnobodyelseCommented:
Thanks for the points, but rstaveley should have got some points as well ...
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now