This compiles with no problem, but my calcCost function returns jibberish. I’ve tried many different combinations and I know it’s something simple. If I change my calcCost function to void and cout totalCost, it works fine, But I need the return value for another class (Order) that I need to add as soon as I (we) figure this problem out.

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

class Pizza
{
public:

Pizza();
char getCrust();
char getSize();
int getPepperoni();
int getCheese();

const void output();
const void orderOutput();
double calcCost(Pizza& custPizza);

private:

char custCrust;
char custSize;
int pepperoni;
int cheese;
double totalCost;
};

/*class Order
{
public:

Order();
const double calcCost(const Order& custPizza);

private:

Pizza order1;
double totalCost;
};*/

int main()
{
   Pizza custPizza;
   //Order order1;
	
   //char newOrder;
	
	custPizza.getCrust();
	custPizza.getSize();
	custPizza.getPepperoni();
	custPizza.getCheese();

	custPizza.calcCost(custPizza);


	/*cout << "the total cost of your pizza is \n";
	custPizza.calcCost();*/
custPizza.output();
custPizza.orderOutput();
cout << endl;
cout << endl;

return 0;
}

Pizza::Pizza(){}

//Order::Order() : order1(Pizza()){}

char Pizza::getCrust()
{
	cout << endl;
	cout << "Welcome to Nick's Pizza.\n";
	cout << "What type of crust would you like?\n"
		 << "'P' pan, 'D' deep dish, 'H' hand tossed.\n";
	cin >> custCrust;
	return custCrust;
}

char Pizza::getSize()
{
	cout << endl;
	cout << "What size would you like?\n"
		 << "'S' small, 'M' medium, 'L' large.\n";
	cin >> custSize;
	return custSize;
}

int Pizza::getPepperoni()
{
	cout << endl;
	char custPep;
	cout << "Would you like pepperoni? (y/n) \n";
	cin >> custPep;
	if (custPep == 'y' || custPep == 'Y')
		cout << "How many orders of pepperoni would you like?\n";
		cin >> pepperoni;
		return pepperoni;
}

int Pizza::getCheese()
{
	cout << endl;
	char custCh;
	cout << "Would you like cheese? (y/n) \n";
	cin >> custCh;
	if (custCh == 'y' || custCh == 'Y')
		cout << "How many orders of cheese would you like?\n";
		cin >> cheese;
		return cheese;
}

const void Pizza::output()
{
	cout << endl;
	cout << custCrust;
	cout << endl << endl;
	cout << custSize;
	cout << endl << endl;
	cout << pepperoni;
	cout << endl << endl;
	cout << cheese;
	cout << endl << endl;
	cout << totalCost;
	cout << endl << endl;
}

const void Pizza::orderOutput()
{
	cout << totalCost;
}

double Pizza::calcCost(Pizza& custPizza)
{
int toppings = 2;
int costOfTop;
int totalCost = 0;
int costOfPizza = 0;
	if (custCrust == 'P' && custSize == 'S')
		costOfPizza = 10;
	/*else if (custCrust == 'P' && custSize == 'M')
		costOfPizza = 14;
	else if (custCrust == 'P' && custSize == 'L')
		costOfPizza = 17;
	else if (custCrust == 'D' && custSize == 'S')
		costOfPizza = 10;
	else if (custCrust == 'D' && custSize == 'M')
		costOfPizza = 14;
	else if (custCrust == 'D' && custSize == 'L')
		costOfPizza = 17;
	else if (custCrust == 'H' && custSize == 'S')
		costOfPizza = 10;
	else if (custCrust == 'H' && custSize== 'M')
		costOfPizza = 14;
	else if(custCrust == 'H' && custSize == 'L')
		costOfPizza = 17;*/ 
	
	costOfTop = (custPizza.pepperoni + custPizza.cheese) * toppings; // the cost of all toppings
	totalCost = costOfPizza + costOfTop;
	
	return totalCost;
}

Output:

Welcome to Nick's Pizza.
What type of crust would you like?
'P' pan, 'D' deep dish, 'H' hand tossed.
P

What size would you like?
'S' small, 'M' medium, 'L' large.
S

Would you like pepperoni? (y/n)
y
How many orders of pepperoni would you like?
2

Would you like cheese? (y/n)
y
How many orders of cheese would you like?
2

P

S

2

2 //I put the preceding output just to see what’s going on.

-9.25596e+061

-9.25596e+061

Press any key to continue . . .
_____________________________________________________________________
I commented out most of the function, but I get the same results with the whole function too. I guess I’m getting leftover memory, but why?

Initialize the variables using the default constructor

OK, I'm on it.

Line 59 to 62, you ask to get user input, but where do you store them? Also, you don't have pepperoni and cheese initialized but use them in your total cost calculation?

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.