Cant get rid of remaining zero

My code is meant to get rid of duplicates found in list. The only problem is that there was three zeroes at the end of the list after it was fixed. I made a for loop to check for zeroes, but it only got rid of two and not the last one.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <vector>

using namespace std;




int main() {


	
	vector<int>number;
	number.push_back(1);
	number.push_back(4);
	number.push_back(9);
	number.push_back(16);
	number.push_back(9);
    number.push_back(7);
    number.push_back(4);
    number.push_back(9);
    number.push_back(11);
    
   
        
	
	
	for (unsigned int x = 0; x < number.size() - 1; x++) {
        
		
        for(unsigned int i=0; i <x ; i++){
            if(number[i] ==number[x]){

			number.size()-1;
            for(unsigned int k=x;k<number.size();k++){
                number[k]=number[k+1];
            }
            }

        }
         
        }
    	
		
	
	for (unsigned int m = 0; m < number.size()+1; m++) {
         if(number[m]==0){
            number.pop_back();
        }
       
		
	}
    
    for(unsigned int t=0;t<number.size();t++){
        cout<<number[t]<<endl;
    }


	


	
	
	
	return 0;
}
Line 34: What do you think this line does? Hint: Nothing.

You should consider why you're seeing zeroes. You don't push any zeroes onto your vector (14-22). Therefore, lines 46-52 should be unnecessary. BTW, these lines are wrong. If number[m] is 0, you're removing the last entry in the vector which has no relation to number[m].

Lines 35-37: Why not use std::vector::erase to remove the duplicate entry rather than trying to move the vector down yourself, which you're not doing correctly.

Topic archived. No new replies allowed.