search case printing out unwanted things.

Hi guys. Can someone help debug my code? My case is working perfectly fine but there is some weird results.

For example.
in the list there would be.

1
2
3
4
5
Name SubCategory
A      Bat
B      Cat
C      Bat
D      Cat


The output of result I would get when i search for Cat is.

1
2
3
4
5
6
7
8
9
10
11
1. No such sub category found!

2.ItemID:..............
Item Description: .....
....

3. No such sub category found!

4.ItemID:..............
Item Description: .....
....


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
            case 2: {
                    cout << "<<Search by Sub Category>>" << endl << endl;
                    string search;
                    cout << "Please enter sub category:";
                    cin >> search;
                    
                    list<WHMgmtDetails>::iterator i;
                    
                    while (1) {
                        for (i = transaction.begin(); i != transaction.end(); ++i) {
                            if (i->itemCat == search) {
                                cout << "Item ID:    " << i->itemID << endl
                                << "Item Description:   " << i->itemDesc << endl
                                << "Item Category:      " << i->itemCat << endl
                                << "Item Sub Category:  " << i->itemSubCat <<endl
                                << "Item amount per unit:       " << i->unitPrice <<endl
                                << "Item quantity:      " << i->qty <<endl
                                << "Date Purchased:     " << i->date << endl << endl;
                            } else {         
                                cout << "No such sub category found!" << endl;
                            }
                        }
                        return;
                    }
            }
            break;


How do I solve this? I do not want to print the No such sub category found everytime it iterates through
Use a debugger and watch the values of the locals (the variables in the current scope). It is also INCREDIBLY hard for someone to debug your code without a debugger or comments for that matter. Because we don't know the intention nor do we have all the code to figure out the intention. This is a good exercise in learning how to use a debugger. It'll also help you to understand how your code works and why it works the way it does. I suggest you go get a debugger and run your code through it then come to us with questions that aren't "Do my homework for me."

Sorry if I sounded rude, but you gotta learn sometime right?
Last edited on
Topic archived. No new replies allowed.