Thread: Classes problem

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    45

    Question Classes problem

    i need help with this code...i know whats wrong, but i just need to know what i need to do to fix it. i dont want the answer, just what i need to do.

    Code:
    #include<iostream.h>
    
    class GroceryItem
    {
    	private:
    		int stockNum;
    		double priceEach;
    		int quantity;
    		double totalValue;
    		void calculateTotalValue();
    
    	public:
    		void setStockNum();
    		void setPriceEach();
    		void setQuantity();
    		void displayGroceryItem();
    };
    
    void GroceryItem::setStockNum()
    {	
    	cout<<"Enter the Stock Number: ";
    	cin>>stockNum;
    
    };
    void GroceryItem::setPriceEach()
    {
    	cout<<"Enter the price: ";
    	cin>>priceEach;
    };
    void GroceryItem::setQuantity()
    {
    	cout<<"Enter the quantity in stock: ";
    	cin>>quantity;
    };
    void GroceryItem::calculateTotalValue()
    {
    	totalValue=priceEach*quantity;
    };
    void GroceryItem::displayGroceryItem()
    {
    	cout<<"Item #"<<stockNum<<" Price: "<<priceEach<<endl;
    	cout<<"Quantity in stock: "<<quantity<<" Value: "<<totalValue;
    };
    
    void main()
    {
    
    
    
    	GroceryItem anItem;
    	void setStockNum();
    	void setPriceEach();
    	void setQuantity();
    	void calculateTotalValue();
    	void displayGroceryItem();
    
    
    
    
    
    	anItem.setStockNum();
    	anItem.setPriceEach();
    	anItem.setQuantity();
    	anItem.calculateTotalValue();
    	anItem.displayGroceryItem(stockNum, priceEach, quantity, totalValue);
    
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>.i know whats wrong
    What makes you think that? Be more descriptive with your questions, and people will be able to help you better. Simply expecting someone to read a program and guess at what they think you're complaining about isn't the right way to ask for help.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    the last 2 lines have errors saying that the the variables are unidentifiable and that the calculatetotalvalue cannot access private variables

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    don't read this if you don't want the answer

    you need to watch the scope:
    Code:
    	GroceryItem anItem;
    	void setStockNum();
    	void setPriceEach();
    	void setQuantity();
    	void calculateTotalValue();
    	void displayGroceryItem();
    you should keep the first line, but get rid of the rest...

    and in the scope your using, you never set up a prototype for:
    Code:
    displayGroceryItem(int,double,int,double);
    just fix the first codebit i gave you and your problems may go away...
    Last edited by major_small; 10-27-2003 at 08:32 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    What to do? Some thoughts:
    1. Bummer that you haven't been able to get a newer book (I assume). You are getting in to a couple of bad habits which may not be the end of the world, but make many people cringe every time you ask for help here (void main() and <iostream.h>).
    2. Look at the errors you get when compiling. You should be able to figure out how to fix them by what they say and what line they refer to. The first one is probably something like:

      'calculateTotalValue' : cannot access private member declared in class 'GroceryItem'

      That means that you are trying to access a private member called "calculateTotalValue". To fix it, either make it public so you can call it from outside the class, or don't call it from outside the class at all (the first option seems more appropriate for you).

      The other errors are probably like:

      'stockNum' : undeclared identifier

      That means that the compiler doesn't recognize the variable stockNum. You have a variable called stockNum, but it is inside your class and it is private. In this case, if you look at the line the error refers to, the question is whether you even need that variable there. If you look at the declaration for the function you are calling, it doesn't take any parameters. Maybe you changed it but forgot to remove the arguments when you called it in main?
    Seriously, you should work on your problem solving skills so that you can fix these errors. Maybe you tried many things, but if you don't make it look like you made an effort when asking for help, you will normally get less sympathy from people here.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    im not so great at this stuff, but since i did the whole GroceryItem::DisplayGroceryItem, and anItem.DisplayGroceryItem, shouldn't they work with the variables in that class?
    Last edited by ForlornOdium; 10-27-2003 at 09:30 PM.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah, but like i said before, you need to pay attention to scope:
    Code:
    	void setStockNum();
    	void setPriceEach();
    	void setQuantity();
    	void calculateTotalValue();
    	void displayGroceryItem();
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with classes and pointers
    By Akkernight in forum C++ Programming
    Replies: 18
    Last Post: 02-21-2009, 06:21 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Having a problem with Classes
    By FoxTrot in forum C++ Programming
    Replies: 10
    Last Post: 09-06-2007, 07:40 PM
  4. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  5. problem w/ nested templatized classes
    By *ClownPimp* in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2002, 07:58 AM