String Array Formatting Help

I'm very new to programming and I've been trying to format the output for several hours now.
I'm trying to format the output of the prices coming from the string array, but because the words in the array all differ in length using something like setw(10) leaves me with several different length lines, and I need everything to line up nicely like it does with Sales Total Sales Tax and Grand Total.

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
119
120
121
122
123
124
125
126
127
128
129
130
131

//Bring in files
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

//Begin main function
int main()
{

	
//Declare variables

int counter;
int items = 0;
float salesItem = 0;
float salesTotal = 0;
float salesTax = 0;
float salesTaxPercentage = 0;
float grandTotal = 0;
char runAgain;
float items_array[999];
int sum = 0;
string textItems_Array[999];


do
{//Begin Runagain Loop

//Introduction Text
cout << "Welcome to the new and approved Sales Receipt system." << endl;
cout << "This new system will not only calculate multiple sales items, " << endl;
cout << "but it will also store the name of the sales item. " << endl;
cout << setw(15) <<" "<<"Enjoy the new system!!!!" << endl;


//Formatting Lines
cout << endl;
cout << endl;


//Get input for salesItem
cout << "Enter in the number of sales items to be calculated : ";
cin >> items;

//Start counter
counter = 1;

//Reset variable values after program repeats.
salesTotal = 0;
sum = 0;

//Store sales items in arrays
	for (int ct= 0; ct < items; ct++)
	{
	cout << "Enter in the name of sales item number " << ct + 1 << " : ";
	cin >>textItems_Array[ct];	


	cout << "Enter in the price of " << textItems_Array[ct] << " : $ ";
	cin >>items_array[ct];	


	}

//Calculate SalesTotal
	for (int i=0; i<items; i++)
	{
		sum+=items_array[i];
		salesTotal = sum;
	}

//Formatting Lines
cout << endl;
cout << endl;

//Get sales tax 
cout << "Enter in the sales tax percentage" << endl;
cout << "(Enter 10 for 10%): ";
cin >> salesTaxPercentage;

//Formatting Lines
cout << endl;
cout << endl;

//Calculate sales tax and grand total
salesTax = salesTotal * (salesTaxPercentage / 100);
grandTotal = salesTax + salesTotal;


//Display output
cout << "********************************************" << endl;
cout << "********  S A L E S  R E C E I P T  ********" << endl;
cout << "********************************************" << endl;
cout << "**               **        **             **" << endl;
cout << "**   Item Names  **        **    Price    **" << endl;
cout <<    setiosflags(ios::fixed) << setprecision(2);
cout << "********************************************" << endl;


for (int cnt = 0; cnt <items; cnt++)

{ 
cout << "**   "<< left << setw(4) << textItems_Array[cnt] <<"                " ; 
cout << right <<  "  $"<< setw(10) << items_array[cnt] <<"  **"<<endl;
}


cout << "********************************************" << endl;
cout << "**   Sales Total             $" << setw(10) << salesTotal << "  **" << endl;
cout << "**   Sales Tax               $" << setw(10)  << salesTax << "  **"<< endl;
cout << "**                            ----------- **" << endl;
cout << "**   Grand Total             $" << setw(10) << grandTotal << "  **" << endl;
cout << "**                                        **" << endl;
cout << "**                                        **" << endl;
cout << "********************************************" << endl;


//Run again input
cout << "Do you want to run this program again? (Y/N):" << endl;
cin >> runAgain;
runAgain = tolower(runAgain);

}// End loop
while (runAgain == 'y');

//System pause
return 0;

}//End main Function 
try to take the length of the words and then take the relative setw value like

setw(wordLen-20)//it can be any integer which formats well :D
Last edited on
Topic archived. No new replies allowed.