How to cin to a vector

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

    Just add another variable.

    int temp;
    while (cin >> temp && V.size() < n){
        V.push_back(temp);
    }
    
    0 讨论(0)
  • 2020-11-27 15:48

    You need a second integer.

    int i,n;
    vector<int> V;
    cout << "Enter the amount of numbers you want to evaluate: ";
    cin >> i;
    cout << "Enter your numbers to be evaluated: " << endl;
    while (V.size() < i && cin >> n){
      V.push_back(n);
    }
    write_vector(V);
    return 0;
    
    0 讨论(0)
提交回复
热议问题