Thread: Help with querying the data in memory

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    Help with querying the data in memory

    I have most of the program done but need some help with getting the program to:
    Get the price of an item by its name.
    Get the quantity of an item by its name.
    Get the average price of all items.


    Here's my code:
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    
    struct ItemDataType
    {
    	char name[30];
    	int price;			//Price in cents
    	int quantity;		//Number in inventory
    };
    
    
    ItemDataType ReadEntry();
    
    int main ()
    {
    	ItemDataType Item[100];
    	int TotalItem;
    	int index;
    
    
    	cout << "How many items are there? ";
    	cin >> TotalItem;
    	
    
    	for (index = 0; index < TotalItem; index++)
    	{
    		Item[index] = ReadEntry();
    	}
    
    
    bool done = false;
    char  userChoice;
    while (!done)
    	{
        cout << "Please select a choice\n";
        cout << "1: Get price by name\n";
        cout << "2: Get quantity by name\n";
        cout << "3: Get the average price\n";
        cout << "4: Exit\n";
        cin >> userChoice;
    
        switch (userChoice)
        {
            case '1' : GetPriceByName(Item, TotalItem);
     		break;
            case '2' : GetQuantityByName(Item, TotalItem);
     		break;
            case '3' : GetAveragePrice(Item, TotalItem);
      		break;
            case '4' : done = true;
         	break;
            default : 	cout << "Invalid selection, please try again\n\n";
                       	break;
        }
    	}
    
     cout << "Goodbye\n";
    
    
    	return 0;
    
    }
    
    
    ItemDataType ReadEntry()
    {
    	ItemDataType Data;
    
    	cout << "Enter item name: ";
    	cin >> Data.name;
    
    	cout << "Enter item price in cents: ";
    	cin >> Data.price;
    
    	cout << "Enter item quantity: ";
    	cin >> Data.quantity;
    
    	return Data;
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    A simple search for an item could include:

    a function that returns an integer (the price of the object);

    a for loop starting at 0 and going until it either finds the item, or runs out of items to check; and

    a call to strcmp() which takes two strings as parameters
    I.e. strcmp(Item.name, TargetString);
    and returns:
    an integer less than 0 if string1 is smaller than string2 (i.e. "Bob" and "Jack")
    an integer greater than 0 if string1 is greater than string2 (i.e. "Zack" and "Andy")
    and finally, it returns 0 if string1 equals string2 (I.e. "Mojo" and "Mojo").

    A similar method could be applied for the quantity.

    As for the average price, a for loop that goes through your Item array and keeps adding the prices to a variable, then dividing by the total number of Items, would do the trick.

    Hope that gets you off to a good start.
    Last edited by Epo; 04-19-2005 at 06:33 PM. Reason: God told me to
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Beyond any other issue you may have with your problem, which I am not going to address at the moment, you should at least make certain you don't go over the bounds of your array.

    Code:
    const int MaxItems = 100;
    ItemDataType Item[MaxItems];
    int TotalItem;
    int index;
    
    cout << "How many items are there? ";
    cin >> TotalItem;
    	
    for (index = 0; index < TotalItem && index < MaxItems; index++)
    {
        Item[index] = ReadEntry();
    }
    A better way would be to simply use an STL container of some sort to store as many objects as your heart could possibly desire.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    To expound upon hp_mp5kpdw's suggestions, I would first and foremost suggest using std::map within this project. It will make your life SO much easier. Dealing with data of varying lengths that you want to relate to one another (like a dynamic array, but it involves different types), is best dealt with using the many facets of STL containers. Very useful tool, and it cuts down on the assumptions you might make during design (i.e max / min value for a data set, since it's dynamic in nature and throws exceptions when anything goes wrong).

    EDIT:
    Just in case you don't like my suggestion, you at least will want to make MaxItems unsigned
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    At your request, here is some code to get you started. But the missing tidbits are for you to fill in:

    Code:
    /*ENTER RETURN TYPE HERE*/ return_price(/*ENTER NECESSARY PARAMETERS HERE*/)
    {
    	int x, compare_result;
    
    	char SearchForThisName[30];
    
    	//ADD SOME CODE WHICH READS IN THE SEARCH NAME FROM THE USER
    
    	for(x = 0; x < TotalItem; x++)
    	{
    		//This compares whether or not SearchForThisName and Item[x].name are equal.
    		//If "equal", then it means that every letter in SearchForThisName corresponds
    		//to every letter in Item[x].name, and vice versa. Any difference in capital
    		//letters or spacing will not result in an "equal" answer.
    		compare_result = strcmp(SearchForThisName, Item[x].name);
    
    		//These next If Statements decide how your program reacts based on
    		//the return value of strcmp()
    		if(compare_result > 0)
    		{
    			//YOUR JOB TO FIGURE OUT WHAT GOES HERE (IF ANYTHING)
    		}
    		else if (compare_result < 0)
    		{
    			//YOUR JOB TO FIGURE OUT WHAT GOES HERE (If ANYTHING)
    		}
    		else if (compare_result == 0)
    		{
    			//YOUR JOB TO FIGURE OUT WHAT GOES HERE (If ANYTHING)
    		}
    		
    		//Just a note for the If Statements. Once you find the proper item,
    		//calling:
    		//return Item[x].price;
    		//Will not only break you out of the For Loop, but exit the function
    		//properly, assuming your return type matches the type you are attempting
    		//to return
    	}
    
    	//If we get here, that means our Item was not located. Therefore
    	//you should return a specific value which you know will never be
    	//returned (or shouldn't under normal circumstances). For example,
    	//have you ever paid -1 dollars for an item? The choice is yours
    	//what to choose though. If you're returning a character, maybe make
    	//it "$". If you're returning a float or double, again, a negative
    	//number would be handy. I think you get the idea.
    }
    For more information on strcmp(), click HERE

    Hope this gets you on the right foot.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    2
    Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. problem with data in allocated memory
    By supi in forum C Programming
    Replies: 3
    Last Post: 06-09-2008, 02:06 AM
  4. How to Parallel reading flat file into C ?
    By anwar_pat in forum C Programming
    Replies: 11
    Last Post: 09-16-2006, 09:44 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM