cout total element in array

Hi, i a beginner in C++ programming. I having a problem to show the output of all the element i store at a array called total_price. the output become unreadable.Your attention is appreciate.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main ()
{
	float price[1]={0};
	int qty;
	float total_price[1]={0};

	cin>>price[0];
	cin>>qty;
	total_price[0]=price[0]*qty;
	cout<<total_price;
}
price[0]==0 //by your declaration
price[0] x any integer == 0
cin>>price[0]//not acceptable

cin>>qty//OK
price[0]=qty//OK
total_price[0]=price[0]//OK
cout<<total_price//OK same as total_price[0]
@kelvin516

[Here's your program, with a bit of explanations, in it. I let it with the arrays, since I figured you're going to enhance it with a loop, and ask for more than one cost, item, etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Total Price.cpp : main project file.

#include <iostream>

using namespace std;

int main ()
{
	float price[1]={0}; // This is the same as float price; 
	int qty;
	float total_price[1]={0};  // This is the same as float total_price;
	// It's good to have something printed on screen
	// so the user knows what is expected
	cout << "What is the cost of the item ?" << endl;
	cin>>price[0];
	cout << "How many are being purchased?" << endl;
	cin>>qty;
	total_price[0]=price[0]*qty;
	// And then give an explanation of what is being shown, as a result
	cout << "The total cost for " << qty << " of those items, at a cost of $" << price[0] << " each, equals" << endl;
	cout << "$" << total_price[0] << endl;
}
Thank you for the reply, what if i have total_price[20], i wan to display the total price for it, i try using the for loop and it become worse. this is my whole code for better understanding (not complete)

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
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <stdlib.h>
using namespace std;

int main ()
{
	
	cout<<"***********************FOOD ORDERING SYSTEM**************************\n";
	cout<<"***********************MARVELLOUS RESTAURANT*************************\n";
	cout<<"********************************************************JTMK-DIS2A***\n"<<endl;

	char cust_name[50];
	cout<<"Please Enter Your Name:\n";
	cin.getline (cust_name,50);
	system("cls");
	menu:
	cout<<"Welcome "<<cust_name<<" to Marvellous Restaurant"<<endl;
	cout<<endl;
	cout<<"Here is the menu for your restaurant. Please make selection (1-5)."<<endl<<endl;

	int slt_num;
	char y_n;
	int food_num;
	int food;
	int qty;
	int sum=0;
	float price[30]={0};
	float total_price[30]={0};
	float grand_total;
	
	cout<<"(1) foods menu"<<endl<<endl;
	cout<<"(2) drinks menu"<<endl<<endl;
	cout<<"(3) pay your bill"<<endl<<endl;
	cout<<"(4) book you table"<<endl<<endl;
	cout<<"(5) exit the program"<<endl<<endl;

	cin>>slt_num;

	switch (slt_num)
	{
		case (1):	system("cls");
						cout<<"Please Choose food categories:"<<endl<<endl;
						cout<<"(1) chinese food"<<endl<<endl;
						cout<<"(2) malay food"<<endl<<endl;
						cout<<"(3) indian food"<<endl<<endl;
						cin>>food_num;
						switch (food_num)
						{
						case (1):		food_1:
										system("cls");
										cout<<"Please make selection:"<<endl<<endl;
										cout<<"(1) Char Kuih Tiaw"<<endl<<endl;
										cout<<"(2) Chee Cheong Fan"<<endl<<endl;
										cout<<"(3) Bak Kuk Teh "<<endl<<endl;
										cout<<"(4) Main Menu"<<endl<<endl;
										cin>>food;
											switch (food)
											{
												case 1:	system("cls");
														cout<<"Price for Char Kuih Tiaw is RM 3.50\n";
														cout<<"Please Enter Quantity:";
														cin>>qty;
														price[0]=3.50;
														total_price[0]= price[0]*qty;
														goto food_1;
														break;										

						
														

												case 2:break;
												case 3: break;
												case 4: system("cls");goto menu;
														
											}
											
						
							case (2) :	system("cls");
							case (3) :	system("cls");

							default:	system("cls");
										cout<<"Wrong input, Please Try Again!..PRESS 'Y' TO CONTINUE";
										cin>>y_n;
										if (y_n=='Y'||y_n=='y')
										system("cls");
										goto menu;
							}
									
					

		case (2):	system("cls");
					break;

		case (3):	system("cls");
					for(int i=0;i<30;i++)
					{total_price[30] += total_price[i];}
					cout<<"TotalPrice="<<total_price;
					break;

		case (4):	system("cls");
					break;

		case (5):	system("cls");
					cout<<"Thankyou for comming..... GOOD BYE!";
					return 0;

		default:	system("cls");
					cout<<"Wrong input, Please Try Again!..PRESS 'Y' TO CONTINUE";
					cin>>y_n;
					if (y_n=='Y'||y_n=='y')
						system("cls");
						goto menu;

	}
	

	return 0;
}
You have a problem with line 96:

total_price[30] += total_price[i];

You're trying to store a value in the 31st element of the total_price array. But you've declared it to only have 30 elements, so you're trying to write past the end of the array.

Also, in lines 27 and 28, you're only initializing the first element of each array.
@kelvin516

You have quite a few things that are wrong in your program. It seems you're not sure how arrays operate, so I added a little to the original program, tou better show how they are used. After studying it, you may be able to better program your second attempt.

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
// Total Price.cpp : main project file.

#include <iostream>
#include <string>

using namespace std;

int main ()
{
	float price[5]={0.0};
	int quantity[5];
	int i;
	float total_price[5]={0.0};
	float grand_total = 0.0;
	for (i=0;i<5;i++)
	{
		
	// It's good to have something printed on screen
	// so the user knows what is expected
	cout << "What is the cost of the item " << i+1 << " ?" << endl;
                        // Added a 1 to print out of i, so values show as 1 to 5, not 0 to 4
	cin>>price[i];
	cout << "How many are being purchased?" << endl;
	cin>>quantity[i];
	total_price[i]=price[i]*quantity[i];
	grand_total+=total_price[i];
	}
	
	// Now print out what was inputted
	// And then give an explanation of what is being shown, as a result
		for (i=0;i<5;i++)
	{
		cout << "Cost per item : $" << price[i] << endl;
		cout << "Amount purchased : " << quantity[i] << endl;
		cout << "Total Cost : $" << total_price[i] << endl << endl;

	}
		// Print out the grand total here
		cout << "For a grand total of $" << grand_total << endl << endl;
}
Thx for all the reply. I very appreciate this.
Topic archived. No new replies allowed.