How to cin to a vector

后端 未结 20 2524
萌比男神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:24

    cin is delimited on space, so if you try to cin "1 2 3 4 5" into a single integer, your only going to be assigning 1 to the integer, a better option is to wrap your input and push_back in a loop, and have it test for a sentinel value, and on that sentinel value, call your write function. such as

    int input;
    cout << "Enter your numbers to be evaluated, and 10000 to quit: " << endl;
    while(input != 10000) {
        cin >> input;
       V.push_back(input);
    }
    write_vector(V);
    

提交回复
热议问题