Hello I am attempting to code for a particular function to show the number of times a particular item appears. I have a class Item and class Bag. Items are generated and placed into my bag, this is what I have so far:
void get_frequency(Item item){
for (int i = 0; i < number_items; i++){
int found = 0;
for (int j = 0; j < i; j++) {
if (items[i].get_name() == items[j].get_name())
found++;
}
if (found == 0){
int count = 1;
for (int j = i + 1; j < number_items; j++) {
if (items[i].get_name == items[j].get_name())
count++;
}
cout << items[i].get_name() <<" appears: " << count << endl;
}
My problem is that when I call this function in main() it does not recognize any parameter I put in for the class item, but by leaving no parameters it works. This function is supposed to return the amount of times a particular item in my bag appears. i.e. if I insert 2 peaches in my bag I should call this function in my main and it should say Peach appears: 2.
myBag.get_frequency(Peach);
This should result in Peach appears: 2. But this doesn't work and I have to omit Peach and when I omit Peach what it does is it lists every single item in my bag. Can anyone please advise on what I'm doing wrong? Sorry if this is unclear.
Aucun commentaire:
Enregistrer un commentaire