Vector push_back resulting in doubled int

Hi everyone, I am having a problem. I am piping 10 ints into this program(from another program provided to me), and for some reason it is taking the 9th int and copying it twice so that the 9th int and the 10th int are the same at the output, even though they were different numbers in the input. I have no idea what's going on here, anyone a little more experienced with vectors who could pinpoint where I'm going wrong?

Example: input is 54 65 13, cout and the sum are reading and using 54 65 65.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>

using namespace std;

int main(){
  int input, n, sum=0, min, max;
  vector<int> V_num;

  V_num.clear(); //clear the vector

  cin.clear(); cin.ignore(1000, '\n'); //clear the cin

  while (!cin.eof()) { //while loop to get all cin and push into vector.
    cin >> input;
    V_num.push_back(input);
    cout << input << endl;
 }

  //math for needed information (number of int, sum of, min, and max)
  n   =  V_num.size();
 for(std::vector<int>::iterator j=V_num.begin();j!=V_num.end();j++)
    sum += *j;
  min =  *min_element(V_num.begin(), V_num.end());
  max =  *max_element(V_num.begin(), V_num.end());

   //print out info
 cout << "N   = " << n << endl;
 cout << "sum = " << sum << endl;
 cout << "Min = " << min << endl;
 cout << "Max = " << max << endl;

return 0;

}


Last edited on
I am piping 10 ints into this program(from another program provided to me), and for some reason it is taking the 9th int and copying it twice so that the 9th int and the 10th int are the same at the output, even though they were different numbers in the input.


I suspect that you're missing the first int, and since you're looping on eof and not checking to see if input extraction is successful, you actually get int 2-10, with the last one coming up twice because the input extraction fails (but you didn't check to see if it did.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>

int main()
{
    std::vector<int> nums;

    int num;
    while (std::cin >> num)
        nums.push_back(num);

    int min = *std::min_element(nums.begin(), nums.end());
    int max = *std::max_element(nums.begin(), nums.end());
    int sum = std::accumulate(nums.begin(), nums.end(), 0);


    std::cout << "N    = " << nums.size();
    std::cout << "\nsum = " << sum;
    std::cout << "\nMin = " << min;
    std::cout << "\nMax = " << max << std::endl;
}
line 12 is pointless you have nothing in the vector. You called it using the default parameter. It is also pointless to use the cin.clear() , cin.ignore() as you didn't use anything before that needs to be cleared.

Also why is your loop on line 16 !cin.eof() ?
What I would do would be...
1
2
3
4
5
while( cin >> input )
{
    V_num.push_back( input );
    std::cout << input << std::endl;
}


also how come you are using j as your iterator? Most people in the top level for loop use i for integer and it for iterator
for( std::vector<int>::iterator it = V_num.begin(); it != V_num.end(); ++it )

If you have c++11 enabled you can also use auto instead of std::vector<int>::iterator to clear things up
for( auto it = V_num.begin(); it != V_num.end(); ++it )
Or even a ranged-based loop
for( const auto &it : V_num )

Btw your error is probably due to line 16 being the end of file call.
Thank you cire, I just removed the clearing of the cin and clearing of the vector and that solved it. I tried using the cin >> num and it hadn't worked either, but after seeing you excluded those from your code I removed them also and that solved it, thanks.

Also why is your loop on line 16 !cin.eof() ?
What I would do would be...
advice of my TA, actually lol. After some searching after I posted this I cleared it up that it's not good to do so.

also how come you are using j as your iterator? Most people in the top level for loop use i for integer and it for iterator
Because I am just a CS student in the first year and still learning best practices, thanks.
Last edited on
Topic archived. No new replies allowed.