Dynamic Arrays?

I have created a program allows you to input the amount of items purchased, and then input the price of each item and the sales tax. It then creates a receipt which totals all the prices and sales tax.

Now I want to be able to list the items and their prices in the sales receipt as well. What would I have to do so lets say you pick 5 items and type in 5 prices and it give you the sales receipt, before it tells you the total, I want it to list the 5 items so looks like this:

Sales Receipt

Item 1: Price
Item 2: Price
Item 3: Price
Item 4: Price
Item 5: Price

**********

(rest of sales receipt)

Here is what I have so far:
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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char rerun;

		do //The do-while for a program rerun option
		{

// Declare variables: counter, items, sales items, sales total, sales tax, and grand total
int items, count;
int * price;
double total, percent, g_t, tax;

// Input information
	cout<<"How many sales items do you have? :";
	cin>>items;

	price= new (nothrow) int[items];
	if (price == 0)
		cout << "error";
	else
	{
			for (count = 0; count < items; count++)
			{
				cout<<"Enter the value of the sales item. : $";
				cin>>price[count];
				total = total + price;
			}
	}
		
	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 << "entered";
	for (count = 0; count < items; count++)
		cout << price[count] << endl;
	cout << "**                                        **" << endl;
	cout << "**                                        **" << endl;
	cout << setiosflags(ios::fixed) << setprecision(2);
	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');
	
	system ("PAUSE");
	return 0;
} //End Main Function 


I receive the following error:
salerec.cpp(33) : error C2111: pointer addition requires integral operand
Error executing cl.exe.

salerec.exe - 1 error(s), 0 warning(s)
closed account (DSLq5Di1)
1
2
int * price;
double total, percent, g_t, tax;

1
2
3
4
5
6
			for (count = 0; count < items; count++)
			{
				cout<<"Enter the value of the sales item. : $";
				cin>>price[count];
				total = total + price;
			}

You don't see anything wrong here?
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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char rerun;

		do //The do-while for a program rerun option
		{

// Declare variables: counter, items, sales items, sales total, sales tax, and grand total
int items, count;
int * price;
double total, percent, g_t, tax;

// Input information
	cout<<"How many sales items do you have? :";
	cin>>items;

	price= new (nothrow) int[items];
	if (price == 0)
		cout << "error";
	else
	{
			for (count = 0; count < items; count++)
			{
				cout<<"Enter the value of the sales item. : $";
				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;
	for (count = 0; count < items; count++)
		cout << price[count] << endl;
	cout << "**                                        **" << endl;
	cout << "**                                        **" << endl;
	cout << setiosflags(ios::fixed) << setprecision(2);
	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');
	
	system ("PAUSE");
	return 0;
} //End Main Function 


Fixed it. The prices are now being displayed except it screwed up everything else in the display.

http://i44.servimg.com/u/f44/16/74/15/46/output10.jpg

It should be like this:
http://i44.servimg.com/u/f44/16/74/15/46/output11.jpg
Last edited on
closed account (DSLq5Di1)
http://en.wikipedia.org/wiki/Uninitialized_variable
total = 0 DUH!!!!!
Thanks sloppy!
I want to number my items. I want it to ask "Input sales item 1" "Input sales item 2" "Input sales item 3" and then on the receipt I want it to do the same thing:

Sale item 1: $(price)
Sale item 2: $(price)
Sale item 3: $(price)

I want those number to show up. I can't find any tutorials or anything to read that teaches how to number items either.

Here is my current code:
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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char rerun;

		do //The do-while for a program rerun option
		{

// Declare variables: counter, items, sales items, sales total, sales tax, and grand total
int items, count;
int * price;
double total = 0, percent, g_t, tax;

// Input information
	cout<<"How many sales items do you have? :";
	cin>>items;

	price= new (nothrow) int[items];
	if (price == 0)
		cout << "error";
	else
	{
			for (count = 0; count < items; count++)
			{
				cout<<"Enter the value of the sales item: $";
				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;
	for (count = 0; count < items; count++)
		cout << "**  Sales Item #:    $" << setw(9) << setprecision(2) << price[count] << "           **" << endl;
	cout << "**                                        **" << endl;
	cout << "**                                        **" << endl;
	cout << "********************************************" << endl;
	cout << "**                                        **" << endl;
	cout << "**                                        **" << endl;
	cout << setiosflags(ios::fixed) << setprecision(2);
	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');
	
	system ("PAUSE");
	return 0;
} //End Main Function 
Last edited on
cout << "** Sales Item " << count + 1 << ": $" << setw(9) << setprecision(2) << price[count] << " **" << endl;
Ended up doing it that way and it works like a charm!

I found one big issue! I cannot input decimal value for the price of an item. I know this is because price is set to integer. I need to be able to use decimal values for the price. Previously I used double for price originally but to create the dynamic array, I changed it to integer.

What options are there? Or what can I do? The entire program just stops working as soon as I input decimals into the sales item price such 10.50.
closed account (DSLq5Di1)
1
2
double * price;
price= new (nothrow) double[items];

Don't forget to delete[] price; after your output is done. Currently, each subsequent run is allocating new memory without freeing previously allocated memory, which will continually grow over time until the user runs out of system memory.

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.11
yeah i got the incrementing working. I need to learn to completely follow through on my code when making edit. I had changed it to double in the variable but failed to recognize to change it to double[items].

Here's the code:

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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char rerun;

		do //The do-while for a program rerun option
		{

// Declare variables: counter, items, sales items, sales total, sales tax, and grand total
int items, count;
double * price;
double total = 0, percent, g_t, tax;

// Input information
	cout<<"How many sales items do you have? :";
	cin>>items;

	price= new (nothrow) double[items];
	if (price == 0)
		cout << "error";
	else
	{
			for (count = 0; count < items; 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;
	for (count = 0; count < items; count++)
		cout << "**  Sales Item " << count + 1 << ":   $" << setw(9) << setprecision(2) << price[count] << "            **" << endl;
	delete[] price;
	cout << "**                                        **" << endl;
	cout << "**                                        **" << endl;
	cout << "********************************************" << endl;
	cout << "**                                        **" << endl;
	cout << "**                                        **" << endl;
	cout << setiosflags(ios::fixed) << setprecision(2);
	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');
	
	system ("PAUSE");
	return 0;
} //End Main Function 


On the first run of the program, entering decimal values such as 1.50 will get rounded to 2 on sales receipt. However, if you select Y and run the program again, it properly displays the decimal place.

Here's a screenshot:
http://i44.servimg.com/u/f44/16/74/15/46/output12.jpg
closed account (DSLq5Di1)
cout << setiosflags(ios::fixed) << setprecision(2);

Move this above your price output. ^^
THANK YOU!!!
I have one last component of this program now. I want to be able to name the sales item such as book, pen, car, etc. and then in the sales receipt, instead of it listing the items as Sales Item 1, Sales Item 2, etc. I want it to be listed (name of item): Price so like Book: Price, Pen: Price, and etc.

I know this goes back into memory and storing it all but it seems a little more complex.
I went with the way I already had but it is producing errors

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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char rerun;

		do //The do-while for a program rerun option
		{

// 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 * names = new string[items];

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

	// Input and storage of items prices
	price = new (nothrow) double[items];
	if (price == 0)
		cout << "error";
	else
	{
			for (count = 0; count < items; 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 << "**  Sales Item " << count + 1 << ":   $" << 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');
	
	system ("PAUSE");
	return 0;
} //End Main Function 


Errors I receive
:\Users\Neel P\Documents\Classes\Summer 2011\CMPSC 101\HW6\salesname.cpp(26) : warning C4291: 'void *__cdecl operator new(unsigned int,const struct std::nothrow_t &)' : no matching operator delete found; memory will not be freed if initialization throws an exception

c:\program files (x86)\microsoft visual studio\vc98\include\new(36) : see declaration of 'new' salesname.cpp(34) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<
char> >' (or there is no acceptable conversion)
closed account (DSLq5Di1)
The warning is self explanatory and regards my previous post about memory, your error is a result of not including the string header #include <string> .

Also,
1
2
3
4
5
6
int items, count;
string * names = new string[items]; // items = ???

// Input information
	cout<<"How many sales items do you have? :";
	cin>>items;
The program keeps crashing for me and there isn't any errors or warnings...

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
char rerun;

		do //The do-while for a program rerun option
		{

// 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 * names = new string[items];

// Input information
	cout<<"How many sales items do you have? :";
	cin>>items;
	
	// Input and storage of items names and prices
	names = new string;
	price = new (nothrow) double[items];
	if (names, price == 0)
		cout << "error names";
	else
	{
		for (count = 0; count < items; count++)
		{
			cout << "Enter the name of the sales item " << count + 1 << ": ";
			cin >> names[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 << "**  " << names[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');
	
	system ("PAUSE");
	return 0;
} //End Main Function 
closed account (DSLq5Di1)
Other than its type, your names variable should be handled in same way as your price variable, dont you think..?

1
2
string * names = new string[items];
string * names;

1
2
3
	// Input and storage of items names and prices
	names = new string;
        names = new (nothrow) string[items];

1
2
3
	...
	delete[] price; // clears the memory
	delete[] names;


Topic archived. No new replies allowed.