1

I was wondering if you could help me and figure this out without using something like strtok. This assignment is meant for me to build something that will accept input and direct the user to the right area. I want to get something like....

Help Copy

and it stores it as

array[1] = Help
array[2] = Copy.

I tried to do something like cin>>arr[1]; and cin>>arr[2] but at the same time what if the user enters copy then I am not sure how to do it cause if I put just one cin then what if the user puts help copy.

Basically I am not sure how to accept any size input and store anything they put in as elements in an array.

I would try something like cin.get or getline but they don't seem to really help me and my cin idea was not helpful at all.

This is what I have so far.

int main()
{
    string user;

    cout<<"Hello there, what is your desired username?"<<endl;

    cin >> user;

    system("cls");

    cout<<"Hello, " << user << "! How are you doing?"<<endl<<endl;

    cout<< user << ": ";



    return 0;
}
Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • What are you using for the array? A `std::vector` would simplify things if you aren't using that. – GWW Sep 18 '13 at 20:03
  • " .. but at the same time what if the user enters copy " I didn't understand that part. – Mahesh Sep 18 '13 at 20:05
  • Oh sorry, I uploaded an older version of this. I haven't added the array in here yet but I have heard of the vector thing but what exactly does that do. I know a short trip to google will help but I would like your input. – Marlin Hankin Sep 18 '13 at 20:06
  • Is this what you are looking for ? http://stackoverflow.com/questions/236129/splitting-a-string-in-c – heretolearn Sep 18 '13 at 20:12
  • You can split the input string at spaces and store in the corresponding arrays. – Sunny Sep 18 '13 at 20:13

2 Answers2

2
std::vector<std::string> myInputs;

std::cout << "Enter parameters:  ";
std::copy(std::istream_iterator<std::string>(std::cin), std::isteram_iterator<std::string>(), std::back_inserter(myInputs));

// do something with the values in myInputs

If the user presses Enter in between each input, this will go until they cease the input (Crtl-D on Windows). If you want them to put all the parameters on a single line, you can read the input into a single string and then split the string up by spaces (or whatever delimiter you wish to use).

Zac Howland
  • 15,777
  • 1
  • 26
  • 42
2

You can do it like this:

  • Read the entire line using getline
  • Make an input string stream from that line
  • Read the content of that string stream into a vector<string>. It will grow automatically to accommodate as many inputs as the user enters
  • Examine the size of the resultant vector to see how many entries the end-user made

Here is how you can do it in code:

// Prepare the buffer for the line the user enters
string buf;
// This buffer will grow automatically to accommodate the entire line
getline(cin, buf);
// Make a string-based stream from the line entered by the user
istringstream iss(buf);
// Prepare a vector of strings to split the input
vector<string> vs;
// We could use a loop, but using an iterator is more idiomatic to C++
istream_iterator<string> iit(iss);
// back_inserter will add the items one by one to the vector vs
copy(iit, istream_iterator<string>(), back_inserter(vs));

Here is a demo on ideone.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523