Hey all, I got a quick question on a piece of code I am working on. It's a large program so i'll only post relevant snippets(which will be functions).
First I prompt the user to declare and array size and fill it with numbers(0-40). I use two array to keep track of their input, one holds the actual values they input and the other is a counter for the number of occurrences of each number
I think list the numbers they they input by comparing the number of occurances to 0Code:void add(int x,int i) { numberToAdd = x; sizeOfBag = i; int j; int addIt; for(x=0; x<i; x++) { cin >> addIt; if(addIt >= 0 && addIt <= 40) { bag[addIt]++; store[j] = addIt; j++; } else { cout << "Number must be between 0-40" << endl; } } };
3rd i let them remove a certain number of elementCode:void list() { int i=0; for (i=0; bag[store[i]]!=0; i++) { cout << store[i] << endl; } };
I then repeat the list function. Here's where the problem lies. if they input 1,2,3,4,5 into the array; then remove 3; my output shows only 1,2 opposed to 1,2,4,5.Code:void remove(int x, int i) { numberToRemove = x; sizeOfRemove = i; int removeIt; for(x=0; x<i; x++) { cin >> removeIt; if(removeIt >= 0 && removeIt <= 40) { bag[removeIt]--; } else { cout << "Number must be between 0-40" << endl; } } }; };
Any advice?



LinkBack URL
About LinkBacks


