How to cin to a vector

后端 未结 20 2512
萌比男神i
萌比男神i 2020-11-27 14:44

I\'m trying to ask the user to enter numbers that are put into a vector, then using a function call to count the numbers, why is this not working? I am only able to count t

20条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 15:34

    You probably want to read in more numbers, not only one. For this, you need a loop

    int main()
    {
      int input = 0;
      while(input != -1){
        vector V;
        cout << "Enter your numbers to be evaluated: " << endl;
        cin >> input;
        V.push_back(input);
        write_vector(V);
      }
      return 0;
    }
    

    Note, with this version, it is not possible to add the number -1 as it is the "end signal". Type numbers as long as you like, it will be aborted when you type -1.

提交回复
热议问题