Hi. I am having a problem with one of my class functions. It compiles fine and works "when it wants to". I am using the search_inventory function in my add_product function of the same class. sometimes it works perfectly fine however sometimes the search_inventory function causes an infinite loop when i call my add_product function. Since the problem does not occur every time I have no clue where the problem is. the StringUpper function converts a string to uppercase. Any help would be GREATLY appreciated. many thanks

void shopping_cart::search_inventory(vector<product> &list)
{string upper_prod, temp_name,search_prod;
product temp;
bool found=false;
unsigned int size=list.size();

cout<<endl<<"Enter product name: ";
getline(cin,search_prod);
do
{

upper_prod=StringUpper(search_prod);
cout<<"test";
if(upper_prod=="XXX")
{inventory_position=-1;
found=true;}
for(unsigned int i=0; i<size;i++)
{temp=list[i];
cout<<"test1";
temp_name=temp.get_product_name();
temp_name=StringUpper(temp_name);
if(temp_name==upper_prod)
{found=true;
inventory_position=i;}}
cout<<"test2";
if (found!=true)
{cout<<"\nProduct not found. \nPlease re-enter product name or enter xxx to return to main menu: ";
getline(cin,search_prod);}
}while(!found);
}




void shopping_cart::add_product(vector<product> &inventory1)
{product temp,prod1;
int counter=0,exist,amount, inventory_amount;
bool prod_exist=false, correct_amount=false;
search_inventory(inventory1);

...

1. move line 12 up outside the do loop, between lines 8 and 9. Once the string is converted to upper case there is no point in repeating that process.

You need to reformat the code to make it easier for us to read and understand. It's great that you used code tags, but they don't help badly formatted code.

I'm sorry but after browsing the FAQ and "read me first" thread I could not figure out how to add line numbers to the code. I am still experiencing the same problem. I have been trying to figure this out for the past 8 hours... many thanks in advance for any ideas!!!!

1. void shopping_cart::search_inventory(vector<product> list)
2. {string temp_name,search_prod;
3. product temp;
4. bool found=false;
5. 
6. cout<<endl<<"Enter product name: ";
7. getline(cin,search_prod);
8. search_prod=StringUpper(search_prod);
9. 
10. while(!found)
11. {
12 for(unsigned int i=0; i<list.size();i++)
13.        {temp=list[i];
14.           temp_name=temp.get_product_name();
15.           temp_name=StringUpper(temp_name);
16.                    if(temp_name==search_prod)
17.                               {found=true;
18.                                inventory_position=i;}}
19. if (!found)
20. {cout<<"\nProduct not found. \nPlease re-enter product name or enter xxx to return to main menu: ";
21. getline(cin,search_prod);}
22. 
23. if(search_prod=="XXX")
24.         {inventory_position=-1;
25.          found=true;}

}
}

>>I'm sorry but after browsing the FAQ and "read me first" thread I could not figure out how to add line numbers to the code
very simple: [code=c++]

Formatting is still very bad and hard to read. Here is one way to do it right. Notice indentation and placement of braces. You can immediately see the beginning and ending of blocks without hunting all over the code for them or manually counting braces.

void shopping_cart::search_inventory(vector<product> list)
{
    string temp_name,search_prod;
    product temp;
    bool found=false;
 
    cout<<endl<<"Enter product name: ";
    getline(cin,search_prod);
    search_prod=StringUpper(search_prod);

    while(!found)
    {
        for(unsigned int i=0; i<list.size();i++)
        {
            temp=list[i];
            temp_name=temp.get_product_name();
            temp_name=StringUpper(temp_name);
            if(temp_name==search_prod)
            {
                found=true;
                inventory_position=i;
            }
        }
        if (!found)
        {
            cout<<"\nProduct not found. \nPlease re-enter product name or enter xxx to return to main menu: ";
            getline(cin,search_prod);
        }

        if(search_prod=="XXX")
        {
            inventory_position=-1;
            found=true;
        }
    }
}

put a break; after line 21 to stop that loop.

I would also move lines 30-34 up outside that while loop because there is no point searching and comparing when search_prod == "XXX".

many thanks Ancient dragon. I apologize for the bad formatting, I am currently struggling through my first cs class and wasn't sure what good formatting is.... i don't understand why i need to move lines 30-34 out of the loop? because doesn't this terminate the while loop if the user enters xxx?also, the break doesn't resolve the problem. Is there any reason why you may think the infinite loop only occurs during certain runs? the output of my infinite loop is " Product not found. Please re-enter product name or enter xxx to return to main menu: Product not found". many thanks!!!!

Yes, I agree you should leave those lines where they are because of line 27.

I'm stil experiencing the same problem. Does anyone else have any suggestions? many thanks!!!

try using your compile's debugger so that you can execute the program one line at a time and see what the variable values are. When in that code snippet is the infinite loop ? what value of search_prod causes the infinite loop? Possibly the problem isn't even in that function but somewhere else in your program, for example trashing that vector.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.