Total price won't calculated

I developed a food menu program about selling sandwich. I don't have any error after compiling the code and the output is normal. But the problem is the total price and the price + gov tax always show 1.50 and 0.09.

These are the following functions I used in my code:
1. Function calPrice()receives the sandwich code and quantity as parameters. This function will return the price for each sandwich and calculate and return the total price.
2. Function addItem()receives the total price as a parameter and will ask the user whether he or she requires additional cheese in the sandwich. Then, the function returns the new price with an additional 6% government tax.
3. Function main() prompts for the sandwich code and quantity from the user.
4. Then, it calls function calPrice() to get the price.
5. Function addItem() calculates the new price after adding cheese and government tax.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std ;


double M=8.00;
double C=7.50;
double P=7.00;
double T=6.50;
int calPrice(int code,int quantity)
{
    if(code=='M')
	{
        return(8.00*quantity);
    }
    if(code=='C')
	{
        return(7.50*quantity);
    }
    if(code=='P')
	{
        return(7.00*quantity);
    }
    if(code=='T')
	{
    	return(6.50*quantity);
	}
}
int addItem (double total_price)
{
    int quantity;
    int calPrice;
	char answer ='y';'Y';'n';'N';
	double New_price;
	cout << "Additional Cheese? (Y/N) " ;
	cin >> answer ;
    if (answer == 'Y'|| answer == 'y') 
	{			
    cout <<"Add RM1.50 for each"<< endl;
    total_price = calPrice +(1.5*quantity);
	cout <<"Total price :" << total_price <<endl;
    New_price = total_price*0.06 ;
	cout <<"New price + Gov tax (6%) : "<< New_price << endl;
	return (New_price) ;
	} 
	else (answer == 'N'|| answer =='n') ;
	{
	total_price = calPrice ;
	cout <<"Total price :" << total_price <<endl;
    New_price = total_price*0.06 ;
	cout <<"New price + Gov tax (6%) : "<< New_price << endl;
	return (New_price) ;
	}
}
int main ()
{
    char choice = 'M';'C';'P';'T';
    int code , quantity;
    double total_price;
	cout << fixed << showpoint;
	cout << setprecision(2);
	cout <<setw(13)<<"SANDWICH"<<"\t"<<"CODE"<<"\t"<<"PRICE (RM)"<<"\t"<<"ADDITIONAL CHEESE (RM)"<< endl;
	cout <<"     ---------------------------------------------------------"<< endl;
	cout <<setw(13)<<"Meatball"<<setw(7)<<"M"<<setw(14)<< "8.00" << endl;
	cout <<setw(13)<<"Chicken slice"<<setw(7)<<"C"<<setw(14)<<"7.50"<<setw(28)<<"1.50 each"<<endl;
	cout <<setw(13)<<"Pepperoni"<<setw(7)<<"P"<<setw(14)<<"7.00"<<endl;
	cout <<setw(13)<<"Tuna"<<setw(7)<<"T"<<setw(14)<<"6.50"<<endl;
	
	cout << "Choose available sandwich code (M/C/P/T) :";
    cin >> choice;
    while (true)
    {
        if(choice == 'M' || choice == 'C'|| choice == 'P'|| choice == 'T') 
        {
        cout << "Quantity : ";
        cin >> quantity ;break;
		}
		else
		{
		cout << "Choose available sandwich code (M/C/P/T) :";
        cin >> choice;
		cout << "Quantity : ";
        cin >> quantity ;break ;
        }
    }
    addItem (total_price);
    return 0 ;
		
}

Last edited on
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) What is this supposed to be?

char choice = 'M';'C';'P';'T';


Do you understand that all this does is initialise choice to 'M', and that nothing after the first semicolon does anything at all?

3) In your while (true) loop in main(), every single path has a break statement, so the loop can only ever iterate once. Is this what you intended?

4) Nowhere in your code do you ever call the function calPrice() .

5) In your addItem() function, you declare local variables calPrice and quantity. You never assign any values to those variables, so when you attempt to use them, they contain unknown values.

6) Your addItem() function is defined as returning an int. However, the value you're returning is a double. Is that deliberate? I'm guessing not.
As formatted, it's somewhat easier to see some of the problems:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

double M = 8.00;
double C = 7.50;
double P = 7.00;
double T = 6.50;

int calPrice(int code, int quantity)
{
	if (code == 'M')
	{
		return (8.00 * quantity);
	}

	if (code == 'C')
	{
		return (7.50 * quantity);
	}

	if (code == 'P')
	{
		return (7.00 * quantity);
	}

	if (code == 'T')
	{
		return (6.50 * quantity);
	}
}

int addItem(double total_price)
{
	int quantity;
	int calPrice;
	char answer = 'y';
	'Y';
	'n';
	'N';
	double New_price;

	cout << "Additional Cheese? (Y/N) ";
	cin >> answer;

	if (answer == 'Y' || answer == 'y')
	{
		cout << "Add RM1.50 for each" << endl;
		total_price = calPrice + (1.5 * quantity);
		cout << "Total price :" << total_price << endl;
		New_price = total_price * 0.06;
		cout << "New price + Gov tax (6%) : " << New_price << endl;
		return (New_price);
	} else (answer == 'N' || answer == 'n');

	{
		total_price = calPrice;
		cout << "Total price :" << total_price << endl;
		New_price = total_price * 0.06;
		cout << "New price + Gov tax (6%) : " << New_price << endl;
		return (New_price);
	}
}

int main()
{
	char choice = 'M';
	'C';
	'P';
	'T';
	int code, quantity;
	double total_price;

	cout << fixed << showpoint;
	cout << setprecision(2);
	cout << setw(13) << "SANDWICH" << "\t" << "CODE" << "\t" << "PRICE (RM)" << "\t" << "ADDITIONAL CHEESE (RM)" << endl;
	cout << " ---------------------------------------------------------" << endl;
	cout << setw(13) << "Meatball" << setw(7) << "M" << setw(14) << "8.00" << endl;
	cout << setw(13) << "Chicken slice" << setw(7) << "C" << setw(14) << "7.50" << setw(28) << "1.50 each" << endl;
	cout << setw(13) << "Pepperoni" << setw(7) << "P" << setw(14) << "7.00" << endl;
	cout << setw(13) << "Tuna" << setw(7) << "T" << setw(14) << "6.50" << endl;

	cout << "Choose available sandwich code (M/C/P/T) :";
	cin >> choice;

	while (true)
	{
		if (choice == 'M' || choice == 'C' || choice == 'P' || choice == 'T')
		{
			cout << "Quantity : ";
			cin >> quantity;
			break;
		} else
		{
			cout << "Choose available sandwich code (M/C/P/T) :";
			cin >> choice;
			cout << "Quantity : ";
			cin >> quantity;
			break;
		}
	}

	addItem(total_price);
	return 0;
}

Last edited on
calPrice() needs to return a double, not an int.
Topic archived. No new replies allowed.