0

I'm trying to design a program that receives a string from a user and parses it at ever ';' and places each token in a variable of its own. I'm having problems with this because there is no fixed number of variables for each token since my users input is subject to change each time. How would I configure my program to account for these changes and give each token its own unique variable for parsing?

  • 2
    Just use a container class - `std::vector` is usually a good choice to start with – UnholySheep Nov 05 '17 at 19:42
  • 1
    You can't give each token its own unique variable because you don't know how many tokens there will be. Even if this could somehow work you wouldn't be able to use the variables because you don't know how many and with which name exist. The usual solution is to make 1 variable that can hold any number of tokens, like a `std::vector`. – nwp Nov 05 '17 at 19:44
  • How would I then work with each token individually? For instance, if I wanted to make an object for each token? – CodingSolutions Nov 05 '17 at 19:50
  • See for example [this question](https://stackoverflow.com/a/5167799/3484570). The approach also works on `std::cin` or `std::ifstream`. Once you got the vector you can do `std::cout << strings.size();` or `for (const auto &token : strings){ std::cout << token << '\n' }`. – nwp Nov 05 '17 at 20:05
  • [This](http://coliru.stacked-crooked.com/a/d26593e5249edda3) should be close to what you want. – nwp Nov 05 '17 at 20:14

0 Answers0