Writing a function

Write a function, computePrice that receives two parameters: the first one is a character variable indicating the size of the pizza (S, M or L) and the second an integer variable indicating the number of toppings on the pizza. It then computes the cost of the pizza and returns the cost as a floating point number according to the rules:
• Small pizza = R50 + R5.50 per topping
• Medium pizza = R70 + R6.50 per topping
• Large pizza = R90 + R7.50 per topping


I must ONLY do the function , not whole program

1
2
3
4
5
6
7
8
9
10
11
12
  double Pizza::computePrice() 
{ 
double cost = 0.0; 
switch (size) 
{ 
case SMALL: 
  cost += 50; break; 
case MEDIUM: 
  cost += 70; break; 
case LARGE: 
  cost += 90; break; 
} 


How do I make a certain amount for each size for topping?

Do I write this? or how?
1
2
3
4
5
6
7
8
if (SMALL) 
  cost += 5.5; 
if (MEDIUM) 
  cost += 6.5; 
 if (LARGE)
    cost += 7.50;
 return cost; 
} 
Last edited on
Maybe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double computePrice(char pizaType, int noTop) {
	switch (pizaType) {
		case 'S':
		case 's':
			return 50.0 + noTop * 5.5;

		case 'M':
		case 'm':
			return 70.0 + noTop * 6.5;

		case 'L':
		case 'l':
			return 90.0 + noTop * 7.5;
	}

	return 0.0;
}

Last edited on
thanks , So I was kinda on the right track at the beginning
Topic archived. No new replies allowed.