0

How can I get and then store a space delimited input in C++ from a user into an array?

Most important part is that length is unknown and input should be by user during runtime

#include<iostream>
using namespace std;
int main() {
    int n=0;
    string names[3];
    int i = 0;
    while ( getline( cin, names[i]))
    {
        cout<<"i = "<<i<<"\n";
        cout<<names[i]<<"\n";
        i++;
        cout<<"i = "<<i<<"\n";
    }
    for(i=0;i<3;i++)
    {
        cout<<names[i];
    }
    return 0;
}
jotik
  • 17,044
  • 13
  • 58
  • 123
INDER
  • 343
  • 1
  • 4
  • 15
  • The length is not *unknown*. It is just *not yet known*. Ask the user. –  Apr 24 '16 at 17:56
  • #include using namespace std; int main() { int n=0; string names[3]; int i = 0; while ( getline( cin, names[i])) { cout<<"i = "< – INDER Apr 24 '16 at 18:04
  • @NickyC There are various ways, including not to ask the user about the number of values they want to add. That's just silly and clumsy. Better prepare a contract that a certain input will end up asking for more values. – πάντα ῥεῖ Apr 24 '16 at 18:59
  • @πάνταῥεῖ Yes, there are various ways, including "a simple additional hit of the ENTER", which is far better than just asking. No, that is not the point of my comment. The point is, the use of the word *unknown* may have revealed a blind spot. The information can be collected at some point, even in a "silly and clumsy" way. When someone has a problem but shows no idea at all, that's a way to test whether the one really has no idea, and a way to help build up the idea. –  Apr 25 '16 at 00:08

1 Answers1

1

The first problem I can spot in your code is, that you provide a fixed sized array of string names[3]; to store an unknown number of inputs from the user. That certainly won't work.

To fix this use a container that can use an "arbitrary" number of std::strings;

std::vector<std::string> names;

Having this you can just use something like

std::vector<std::string> names;
std::string name;
while(cin >> name) {
   names.push_back(name);
}

The problem still left is how you could end this loop.

There are several options:

  1. Let the user use one of CTRL-D or CTRL-Z (<- subtle link)

  2. Ask them to enter a special value like quit, exit, -1 to end inputting

  3. Track it down to a second level to process ENTER. Use std::getline() parse all the words using a std::istringstream or break, when getline() reads an empty result.

Let's analyze these:

  1. Is a bad choice, because it's operating system dependent and asks the user to do relatively complicated actions.

  2. Restricts the possible inputs, and there's a method needed to escape inputs of quit, exit or alike.

  3. Empty input like retrieved from a simple additional hit of the ENTER key is probably the most intuitive you can offer to the user.


As a solution following my proposal in 3.:

std::string line;
std::vector<std::string> names;
while ( getline( cin, line)) {
    if(line.empty()) {
        // end input condition
        break;
    }
    std::istringstream iss(line);
    std::string name;
    while(iss >> name) {
        names.push_back(name);
    }
}
Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190