C++ - Storing user input string separated by commas into vector

前端 未结 3 1368
野趣味
野趣味 2021-01-07 09:23

I have a code written that performs this task to a certain extent. But, I would like to how to alter my code so that I can store as many string inputs the user wants to ente

3条回答
  •  自闭症患者
    2021-01-07 10:02

    You need to change your code in order to work for any number of user input. The logic is to push every sub string between the commas into vector.

    vector objects;
    
    for(int i = 0,j=0; i

    In order to print the vector first you have to find the size of the vector,then simply iterate over to print it.

    //display
    
    int l=objects.size();
    for (int k = 0; k < l; k++) {
        cout << objects[k] << endl;
    }
    

    Note: If you want your code to work for strings with spaces in between , for example: a ,b ,c ,d then use getline(cin,input); to take input from user.

提交回复
热议问题