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;
}
5
  • What are you using for the array? A std::vector would simplify things if you aren't using that. Commented Sep 18, 2013 at 20:03
  • " .. but at the same time what if the user enters copy " I didn't understand that part. Commented Sep 18, 2013 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. Commented Sep 18, 2013 at 20:06
  • Is this what you are looking for ? stackoverflow.com/questions/236129/splitting-a-string-in-c Commented Sep 18, 2013 at 20:12
  • You can split the input string at spaces and store in the corresponding arrays. Commented Sep 18, 2013 at 20:13

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.