roger
roger

Reputation: 55

Is there a way to make C++ take in an undefined number of strings from cin?

I am trying to allow the user to input a decent number of words (around 10-20) and then parse the input, but using the code below will wait until the user has input a value for every string.

Is there a way to make C++ auto fill the remaining strings with the null character or something similar so the entry of a number of words less than the max won't cause a holdup?

Code:

#include <iostream>
#include <string>

int main()
{
  std::string test1;
  std::string test2;
  std::string test3;
  std::cout << "Enter values\n:";
  std::cin >> test1 >> test2 >> test3;
  std::cout << "test1: " << test1 << " test2: " << test2 << " test3: " << test3 << std::endl;
}

Upvotes: 0

Views: 409

Answers (3)

Nikhil Badyal
Nikhil Badyal

Reputation: 1697

You can use while loop .Something like this

string s;
while (cin >> s) {
    cout << s << endl;
}

or take a vector of strings and make a while , Take inputs in while loop and push them into vector.

as you see it doesn't store. if you want to store. do

vector<string>text;
while(cin>>s){
   text.push_back(s);}

Upvotes: 1

roger
roger

Reputation: 55

I figured it out using this code:

#include <iostream>
#include <string>

int main()
{
  std::string testTemp;
  std::string brokenUp[20];
  int lastOne = 0;

  std::cout << "Enter values\n:";
  std::getline(std::cin, testTemp);

  for(int current = 0; !testTemp.empty() && testTemp.find(' ') != -1; current++)
  {
    brokenUp[current] = testTemp.substr(0, testTemp.find(' '));
    testTemp.erase(0, testTemp.find(' ') + 1);
    lastOne = current;
  }
  lastOne++;
  brokenUp[lastOne] = testTemp;

  //this next part is just a test
  for(int i = 0; i <= lastOne; i++)
  {
    std::cout << brokenUp[i] << std::endl;
  }
}

but you could use anything as the storage for the broken up strings (i.e. a list or a dynamic array).

Upvotes: 0

David C. Rankin
David C. Rankin

Reputation: 84531

To read (and store) and unknown number of whitespace separate strings, you need storage for each string. The most basic way to provide the storage in a flexible way that can be added to in an unlimited (up to your usable memory limit) is with a vector of strings. The string provides storage for each string and the vector container provides an easy way to collect any number of strings together.

Your vector-of-strings (vs) can be declared as:

#include <iostream>
#include <string>
#include <vector>
...
    std::vector<std::string>vs {};

std::vector provides the .push_back() member function to add an element (a string in this case) to the vector, e.g.

    std::vector<std::string>vs {};
    std::string s;

    while (std::cin >> s)
        vs.push_back(s);

Which simply reads string s until EOF is encountered, and each string read is added to the vector-of-strings using vs.push_back(s);.

Putting it altogether you could do:

#include <iostream>
#include <string>
#include <vector>

int main (void) {

    std::vector<std::string>vs {};
    std::string s;

    while (std::cin >> s)  /* read each string into s */
        vs.push_back(s);   /* add s to vector of strings */

    for (auto& w : vs)     /* output each word using range-based loop */
        std::cout << w << "\n";

}

Example Use/Output

$ echo "my dog has fleas" | ./bin/readcintostrings
my
dog
has
fleas

Look things over and let me know if you have further questions.

Upvotes: 2

Related Questions