Price Calculation Problem using Class

This is a discussion on Price Calculation Problem using Class within the C++ Programming forums, part of the General Programming Boards category; I've just learned about classes and objects and I'm having trouble trying to calculate an order for a price of ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    Price Calculation Problem using Class

    I've just learned about classes and objects and I'm having trouble trying to calculate an order for a price of cookies, which is the goal of the program.

    The program is supposed to calculate the price of an order of cookies offering the best deal possible.
    The program starts off with the user entering the price of cookies for the day.
    The price is entered for: single cookie, half-dozen cookies and a dozen. After prices are entered the program for an order which it takes as input from the customer. After taking the order its supposed to calculate the price of the order and offer the best deal. This is where my problem is because there are constraints for the prices and what can be accepted as order inputs. Constraints are listed below:
    1. All prices have to be greater than 0, (I'm ok with this)
    2.The price of a half-dozen, must be less than the price of six individual cookies.
    3. The price of a dozen must be less than the price of two half-dozen.
    This sets up the possibility that the price of 5 or fewer could be greater than the price of a half-dozen, and price of 11 or fewer could be greater than price of a dozen. If this price situation does happen program is supposed to advise the customer of the better deal.
    4. So the program can't accept the price of a halfdozen thats more than price of a dozen, or price of 6 single cookies thats more than a half dozen.

    I'm not sure if I need more set or get methods or if its just my calculations.
    I keep getting 0 for my totalPrice each run. I'm looking to understand how and what I've done wrong, so I can learn from this. Thank you all for you help.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstring>
    using namespace std;
    
    class CookieStore{
    
    public: 
    	void setPrice(double, double, double);
    	double calcPrice(int);
    	void displayPrice();
    	
    	
    	
    private:
    	double price1;
    	double price6;
    	double price12;
    	double totalPrice;
    	int amount;
    
    };
    
    
    
    void CookieStore::setPrice(double a, double b, double c)
    {
    	price1 = a;
    	price6 = b;
    	price12 = c;
    }
    
    double CookieStore::calcPrice(int amount)
    {	
    	double calcPrice1, calcPrice6, calcPrice12 = 0;
    	
    	calcPrice12 = (amount/12) * price12;
    	calcPrice6 = (amount/6) * price6;
    	calcPrice1 = amount * price1;
    	
    	totalPrice = calcPrice1;
    	
    	if((price1* 6) > calcPrice6)
    	totalPrice = calcPrice6;
    	else if((price6 * 2) > calcPrice12)
    	
    	return totalPrice;
    }
    
    void CookieStore::displayPrice()
    {
    	cout << "Amount due is: " << totalPrice;
    
    }
    
    
    	int main(){
    		CookieStore myCookieStore;
    		double single, half, dozen;
    		int order;
    
    		cout << " Enter the price of a single cookie, half-dozen, and dozen" <<
    		endl;
    		cin >> single >> half >> dozen;
    		if(!cin.eof() && cin.good() && order > 0){
    			myCookieStore.setPrice(single,half,dozen);
    		}
    		else{
    			cout << "Invalid Prices entered" << endl;
    		}
    
    
    	
    		cout << "How many cookies does the customer want to order?" << endl;
    		cin >> order;
    
    		while(!cin.eof() && cin.good() && order > 0){
    		myCookieStore.calcPrice(order);
    		myCookieStore.displayPrice();
    		order = 0;
    		}
    
    	return EXIT_SUCCESS;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have an else if at the end of calcprice that doesn't seem to have anything to do.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    Addition to the end of else if

    Sorry,

    I meant to have after the else if;

    totalPrice = calcPrice12;

    Not sure if that even makes a difference, but thats what I meant to have, thanks

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Okay. That should be setting things, but probably not the way you want. Ask yourself this: if the user asks for seven cookies, what price is (s)he going to be charged?

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    Ok, thanks I see what you saying now. So...if the user asks for seven cookies they would be charged for the price of a dozen plus the price of one single. I think what I need to do then in my calcPrice method is set up a series of if statements to determine this? I will rework that and see if it works and re-post. Thank You

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with friend class declaration in a namespace
    By Angus in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2008, 12:29 PM
  2. Replies: 2
    Last Post: 06-21-2006, 04:23 AM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. static class problem.
    By Sebastiani in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2002, 03:27 PM

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