Shopping program

This is a discussion on Shopping program within the C Programming forums, part of the General Programming Boards category; Hello! I would like to make me a program about shopping expences. The best idea in C is to use ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    26

    Shopping program

    Hello!

    I would like to make me a program about shopping expences. The best idea in C is to use structures for that. Correct me if I am wrong.

    I did this little code here:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_NUM 50
    
    typedef struct{
            char item[15];
            float price;
            }Shopping;
           
    Shopping Stores[MAX_NUM];
    I have this structure named Shopping which will pick up all entries for different items and prices. I made me array of structures that I called Stores which contains 50 structures. I am stucked here because I don't know how to identify now each store. For example if it is Walgreen, Walmart, Kroger and so on. Is it better idea that I create separate structures and call them by the name of the store? How could I make my data to go where I want to be?

    I hope that you understand my problem. If somebody have some better ideas how to do this I would appreciate that.

    Thanks.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    maybe try having a store structure that contains item structures? If you think about it, a store contains items, so the logical thing would be to have a store struct that has the name of the store and stuff, along with an array of the items inside the store...get what i mean?

  3. #3
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Quote Originally Posted by barim
    If somebody have some better ideas how to do this I would appreciate that.
    Hi! I'd have another approach for this problem, starting from up above . I defined an Item and a Store class, and I use STL data structures and the string class for simplicity. Here's the code:
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    using namespace std;
    
    class Item
    {
    private:
    	string name;	
    	double price;
    
    public:
    	// default constructor
    	Item( const string& name_in = "" ) : name( name_in )
    	{
    		price = 0;
    		//id++; // automatically increment ID upon construction
    	}
    
    	// copy constructor
    	Item( const Item& item_in )
    	{
    		name = item_in.name;
    		price = item_in.price;
    		//id++;
    	}
    
    	// oerator =
    	Item& operator = ( const Item& item_in )
    	{
    		if( this == &item_in )
    		{
    			return *this;
    		}
    		
    		name = item_in.name;
    		price = item_in.price;		
    		return *this;
    	}
    
    	// copy construct given a string
    	Item& operator = ( const string& name_in )
    	{
    		name = name_in;
    		return *this;		
    	}
    
    	void setPrice( const double price_in )
    	{
    		price = price_in;
    	}
    
    	const double& getPrice()
    	{
    		return price;
    	}
    
    	const string& getName()
    	{
    		return name;
    	}	
    };
    
    
    //typedefs
    typedef pair<string, Item*> ItemsPairType; // pair used in our map
    typedef map<string, Item*> ItemsMap;
    
    class Store
    {
    
    private:
    	string storeName;		// store name	
    	ItemsMap storeItems;	// items from given store	
    
    public:
    	Store( const string& storeName_in ) : storeName( storeName_in ) {}
    	
    	// clean up the heap
    	~Store()
    	{
    		if ( storeItems.size() )
    		{
    			ItemsMap::iterator anIt = storeItems.begin();
    			do
    			{
    				delete anIt->second;						
    			} while( ++anIt != storeItems.end() );
    		}
    	}
    
    	// return store name
    	const string& getStoreName()
    	{
    		return storeName;
    	}
    
    	// add new item to store
    	void addItemToStore( Item& items_in )
    	{
    
    		Item* anItemPtr = new Item(items_in); // copy passed parameter
    		ItemsPairType anItPair;
    		anItPair.first = anItemPtr->getName();
    		anItPair.second = anItemPtr;
    		storeItems.insert( anItPair );
    	}
    
    	// store is free to change prices
    	bool setItemPrice( const string& name_in, const double price_in )
    	{
    		ItemsMap::iterator anIt = storeItems.find( name_in );
    		if( anIt == storeItems.end() )
    		{
    			cout << "No item with name " << name_in << " found in the store " << storeName << endl;
    			return false;
    		}
    		else
    		{
    			//ItemsPairType anItemPair = *anIt;
    			Item* anItem = anIt->second;
    			anItem->setPrice( price_in );
    		}
    		return true;
    	}
    	// search whether item with given nr. exists
    	bool getItem( const string& name_in, Item& item_out )
    	{
    		ItemsMap::iterator anIt = storeItems.find( name_in );
    		if( anIt != storeItems.end() )
    		{
    			cout << "No item with name " << name_in << " found in the store " << storeName << endl;			
    			return false;
    		}
    		else
    		{
    			ItemsPairType anItemPair = *anIt;
    			Item* anItem = anItemPair.second;
    			item_out = *anItem;
    		}
    		return true;
    	}
    
    	void listItems()
    	{
    		if ( !storeItems.size() )
    		{
    			cout << "Store is empty!" << endl;
    			return;
    		}
    		cout << "You can buy at " << storeName << " following items" << endl;
    		ItemsMap::iterator anIt = storeItems.begin();
    		do
    		{
    			Item* anItem = anIt->second;
    			cout << "Item name " << anItem->getName() << " Price " << anItem->getPrice() << endl;			
    		} while( ++anIt != storeItems.end() );
    	}
    
    
    private:
    	Store( const Store& store_in );			// copy constructor and operator delete hidden (just being lazy ;))
    	Store& operator = ( const Store& store_in );
    
    };
    
    int main()
    {
    	Store store1( "Tesco" ); // create store Tesco
    	Store store2( "Auchan" );// create store Auchan
    	Item anItem( "cheese" );
    	anItem.setPrice( 2.25 );
    
    	store1.addItemToStore( anItem );
    
    	anItem.setPrice( 2.35 ); // they are expensiver ;)
    	store2.addItemToStore( anItem );
    	
    
    	anItem = "apple";
    	anItem.setPrice( 0.30 );
    
    	store1.addItemToStore( anItem );
    	store2.addItemToStore( anItem );
    
    	anItem = "bike";
    	store1.addItemToStore( anItem );
    	// store is free to change prices
    	store1.setItemPrice( "bike", 250 );
    	store1.listItems();
    	store2.listItems();
    
    	return 0;
    }
    As I said, this is just another point of view.
    Have a nice code!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I defined an Item and a Store class, and I use STL data structures and the string class for simplicity.
    That's all well and good, but it won't compile as C.

    >If somebody have some better ideas how to do this I would appreciate that.
    Code:
    #define MAX_NUM 50
    
    typedef struct {
      char  item[15];
      float price;
    } Shopping;
    
    typedef struct {
      char     name[15];
      Shopping list;
    } Store;
           
    Store expenses[MAX_NUM];
    Though this limits you to only one Shopping instance for each store. You didn't specify the purpose of these "shopping expenses".
    My best code is written with the delete key.

  5. #5
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Quote Originally Posted by Prelude
    >
    That's all well and good, but it won't compile as C.
    Oh, man, I just forgot this is the C board!!!
    Anyway, forget C...
    ... do it in Assembly!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >... do it in Assembly!
    C++ programmers always want you to do things in a high level and abstract way...work directly with machine code!
    My best code is written with the delete key.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    How about a relational database:
    Code:
    #define MAX_NUM 50
    
    typedef struct {
      char  item[15];
      float price;
      int storeID; /* Which store does this item belong to */
    } Item;
    
    typedef struct {
      char     name[15];
    } Store;
    
    Item items[MAX_NUM];       
    Store stores[MAX_NUM];
    Code:
    /* Add item belonging to store 1. */
    items[0].storeID = 1;
    
    /* Print out store that owns item. */
    puts(stores[items[0].storeID].name);
    
    /* List items belonging to store */
    - loop through items
    if (items[i].storeID == target) puts(items[i].item);
    Last edited by anonytmouse; 05-26-2004 at 08:37 AM.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    26
    I've got this code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_NUM 50
    typedef struct{
            char desc[15];
            float price;
            }Item;
           
    typedef struct{
            char name[15];
            }Store;
            
    Item Items[MAX_NUM];
    Store Stores[MAX_NUM];
    
    
            
    int main()
    {
      int i,j,k;
      
      for(i = 0; i < 25; i++){
      printf("Enter the name of the store: \n");
      scanf("%s", Stores[i].name);
            if(Stores[i].name == '\n')
               break;
               }
               
               for(j = 0; j < 25; j++){
               printf("Enter the description of the item: \n");
               scanf("%s", Items[j].desc);
                    if(Items[j].desc == '\n')
                    break;
                    }
                    for(k = 0; k < 25; k++){
                    printf("Enter the price: \n");
                    scanf("%f", Items[k].price);
                         if(Items[k].price == '\n')
                         break;
                           }
      system("PAUSE");	
      return 0;
    }
    Error message is:

    [Warning] comparison between pointer and integer

    It points to this part of the code:

    Code:
    if(Stores[i].name == '\n')
    I don't understand why it telling me this message because i didn't use any pointers in my program?
    I used three dimensional arrays to access the elements of the structures. I wanted program keeps looping so I can just enter data until I hit new line character.
    Is this OK?

    Thanks.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The name of an array is treated as a pointer to the first element. You should instead be doing:
    Code:
    if( Stores[i].name[0] == '\n' )
    Which checks the first element of the array to see if it is a newline.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    26
    Thanks, quzah this error is fixed, I have display of the first question. That is OK but I would like after response to first question the other question that follows to be displayed. My loop is not good for that. According to that I have 25 possibilities to type and loop is over. That is not what I need. I tried this:

    Code:
    for(i = 0; Stores[i].name[0]; i++)
    My first question is skipped. Anybody has any idea?

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by barim
    My first question is skipped. Anybody has any idea?
    Eventually, the problems with scanf lead you to accept other methods.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21