two variable arrays

For this program, when I have only one variable array (i.e. receipt listing the price only without the names), the program runs fine. However, when I try to add a second variable array (include the name of the item and the price), the program produces no errors but doesn't run correctly. I can't seem to figure out how to get two variable arrays set up the right way.

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

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
char rerun;

		do
		{

// Declare variables: counter, items, sales items, sales total, sales tax, and grand total
int items, count;
double * price; // creates the array to store the price
double total = 0, percent, g_t, tax;
string name = " ";

// Input information
	cout<<"How many sales items do you have? :";
	cin>>items;
	
	// Input and storage of items names and prices
	price = new (nothrow) double[items];
	if (name, price == 0)
		cout << "error names";
	else
	{
		for (count = 0; count < items; count++)
		{
			cout << "Enter the name of the sales item " << count + 1 << ": ";
			cin >> name[count];

			cout<<"Enter the value of the sales item " << count + 1 << ": $";
			cin>>price[count];

			total = total + price[count];
		}
	}
		
	cout << endl << endl;

	cout<<"Enter in the sales tax percentage: ";
	cin>>percent;
		
		tax = total * (percent/100);
		g_t = tax + total;
		
	cout << endl << endl;

	cout << "********************************************" << endl;
	cout << "********  S A L E S  R E C E I P T  ********" << endl;
	cout << "********************************************" << endl;
	cout << "**                                        **" << endl;
	cout << "**                                        **" << endl;
	cout << setiosflags(ios::fixed) << setprecision(2);
	for (count = 0; count < items; count++)
		cout << "**  " << name[count] << setw(12) << "$" << setw(9) << price[count] << "            **" << endl;
	delete[] price; // clears the memory
	cout << "**                                        **" << endl;
	cout << "**                                        **" << endl;
	cout << "********************************************" << endl;
	cout << "**                                        **" << endl;
	cout << "**                                        **" << endl;
	cout << "**  Total Sales:    $" << setw(9) << total << "            **" << endl;
	cout << "**  Sales Tax:      $" << setw(9) << tax << "            **" << endl;
	cout << "**                  -----------           **" << endl;
	cout << "**  Grand Total:    $" << setw(9) << g_t << "            **" << endl;
	cout << "**                                        **" << endl;
	cout << "**                                        **" << endl;
	cout << "********************************************" << endl;
		
	cout << endl << endl;

		cout<<"Do you want to run this program again? (y/n)";
		cin>>rerun;
			
		}

	while (rerun == 'y' || rerun == 'Y');
	
	return 0;
} //End Main Function  
For example, what do these statements mean?

if (name, price == 0)
cout << "error names";
Here in the condition expression of if you are using the comma operator. Why?!
Last edited on
I took out a part of the program to try to get the array to work and left that in, but still taking that out I can't get it to work
Your string name has only one element. So you can access the string only by index equal to 0. That is name[0]

string name = " ";

So your code in this loop where you enter characters in name are invalid for all indexes (count) greater than 0.
.

for (count = 0; count < items; count++)
{
cout << "Enter the name of the sales item " << count + 1 << ": ";
cin >> name[count];


You should declare name as

string name( items );


In this case the number of names of sales would correspond to the number of values of sales and you could use your loop.
Last edited on
I don't quite understand what you mean
Topic archived. No new replies allowed.