How read read random numbor of strings in c++?

kerbchik 1 Reputation point
2020-12-09T11:28:59.257+00:00

Hello,
As an input, I have a random number of strings (from 10 to 500). Every string is separated from the previous one by "enter". How to read them all and close the program as the user stops adding new strings?
Firstly, I was trying to use

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

int main() {
    string s;
   while (getline(cin, s)) {

    }
    return 0;
}

But in this case, the program never stops and always waiting for new input.

Thank you.

Developer technologies C++
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. RLWA32 49,461 Reputation points
    2020-12-09T12:49:39.34+00:00

    How about this --

    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    
    int main()
    {
        string str;
        vector<string> vStrings;
        const size_t min{ 10 }, max{ 20 };
    
        vStrings.reserve(max);
        cout << "Enter lines, an empty line ends input\n";
        while (getline(cin, str))
        {
            if (str.empty())
            {
                if (vStrings.size() < min)
                {
                    cout << "Must enter at least " << min << " lines\n";
                    continue;
                }
                break;
            }
            else
            {
                vStrings.emplace_back(move(str));
    
                if (vStrings.size() == max)
                    break;
            }
        }
    
        cout << "vStrings contains " << vStrings.size() << " strings\n";
        for (auto& s : vStrings)
            cout << s << endl;
    
        return 0;
    
    }
    
    1 person found this answer helpful.

  2. Guido Franzke 2,191 Reputation points
    2020-12-09T11:37:52.47+00:00

    Hello,
    if the user is supposed to enter the random number of strings before the programme exits, do something like this:

    #include<iostream>
    #include<string>
    using namespace std;
    
    int main() {
      string s;
      int n;
      cout << "random number n=" <<;
      cin >> n;
      if (n < 20 || n > 500) 
      {
        cout << "wrong n" << endl;
        return 1;
      }
    
      while (--n > 0 && getline(cin, s))
      {
                 // do something with s
      }
    
      return 0;
    }
    

    Regards, Guido


  3. Guido Franzke 2,191 Reputation points
    2020-12-09T13:40:17.457+00:00

    Sorry, I don't know what you mean.

    • The while-loop is in one function.
    • What do you mean with each symbol as an individual? You use getline, and it will read a string. If you need the characters in the string, use a for loop: for (int i=0; i < s.length(); i++) { c =s[i]; ... }
    • The getline will discard the \n at the end of the string. If you want it at the end of the string, then you must add it by yourself, e.g. s += "\n"; char c;
      int n=0;
      while (getline(cin, s))
      {
      n++;
      if (s.length() == 0)
      {
      if (n < 10) ... error routine, or if at least 10 lines needed: continue;
      break; // empty string will break the while loop
      }
      if (n > 500) break; // not more than 500 lines allowed
      // do whatever you want to do with s,
      // e.g. add the \n again
      s += "\n";
      // if you need the single characters in the string, use the for-loop:
      for (int i=0; i<s.length(); i++)
      {
      c = s[i];
      // do whatever you want to do with c
      }
      }

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.