Table alignment

I'm a bit stuck.. the dollar signs and amount wont align properly. Can someone let me know where I went wrong? I'm thinking the length of the item name is pushing it but im not sure how to fix it. I got the total sales, commission and earnings portion to work though.


Item Name Item Price
--------------------------------------------------------------
dksfjk $ 1
sdjhdsjf $ 2

--------------------------------------------------------------
TOTAL SALES: $ 3
--------------------------------------------------------------
TOTAL COMMISSION: $ 0
--------------------------------------------------------------
TOTAL EARNINGS: $ 3
--------------------------------------------------------------


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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  #include <iostream>
#include <iomanip>

using namespace std;

/*****************Prototypes****************/

void displayMainMenu();

double askSellerForMarketPlace();

void promptForInteger(string, int &, int, int);

void createProducts(string [], int []);

void promptForDouble(string, double &, double, double);

//bool isNum(int);

//double calculateCommission(double, double);
//double sellerEarnings(double, double);
//void displayResults(double, double, double);


/*****************Main Function****************/
int main() {

//    double commissionRate = askSellerForMarketPlace();
//    if (commissionRate > -1) { //sentinel value
//        double price;
//        promptForDouble("Please enter a price", price, 0.01, DBL_MAX);
////        double commission = calculateCommission(price, commissionRate);
////        double earnings = sellerEarnings(price, commission);
////        displayResults(price, commission, earnings);
//    }

    string product_name[1]; // product_name array initialization, initializing array with 1 element
    int product_price[1]; // product_price array initialization, initializing array with 1 element
    createProducts(product_name, product_price);

    return 0;
}


/*****************Headers****************/
// Function to display menu options
void displayMainMenu() {
    cout << endl;
    cout << "............................ MENU ............................" << "\n";
    cout << "." << setw(61) << "." << "\n";
    cout << "." << setw(20) << "1. Mercari" << setw(25) << "3. Tradesy" << setw(16) << "." << "\n";
    cout << "." << setw(61) << "." << "\n";
    cout << "." << setw(21) << "2. Poshmark" << right << setw(29) << "4. Quit Program" << right << setw(11) << "." << "\n";
    cout << "." << setw(61) << "." << "\n";
    cout << ".............................................................." << "\n";
    cout << endl;
}

// Function to ask seller to choose marketplace
double askSellerForMarketPlace() {
//    displayMainMenu();
    int marketplaceNum;
    promptForInteger("Please choose a marketplace", marketplaceNum, 1, 4);

    switch (marketplaceNum) {
        case 1:
            return 0.10; // mercari
        case 2:
            return 0.20; // poshmark
        case 3:
            return 0.149; // tradesy
        case 4:
            return -1; // quit program
    }
    return -1;
}

// Function to prompt data type in integer
void promptForInteger(string prompt, int &choice, int low, int high) {
    cout << prompt << ": ";
    cin >> choice;

    if (choice < low) {
        cout << "Value is too low" << endl;
        promptForInteger(prompt, choice, low, high); // using recursion here, calling itself
    } else if (choice > high) {
        cout << "Value is too high" << endl;
        promptForInteger(prompt, choice, low, high); // using recursion here, calling itself
    }
}

//// Function to prompt data type in double
//void promptForDouble(string prompt, double &val, double low, double high) {
//    cout << prompt << ": ";
//    cin >> val;
//
//    if (val < low) {
//        cout << "Value is too low" << endl;
//        promptForDouble(prompt, val, low, high); // using recursion here, calling itself
//    } else if (val > high) {
//        cout << "Value is too high" << endl;
//        promptForDouble(prompt, val, low, high); // using recursion here, calling itself
//    }
//}


/*
// Function to calculate marketplace commission
double calculateCommission(double price, double commissionRate) {
    return price * commissionRate;
}


// Function to calculate seller earnings
double sellerEarnings(double price, double totalCommission) {
    return price - totalCommission;
}
*/

// Function to create two arrays: productName and productPrice
// This function takes in two empty arrays which will be filled at the end of the function
void createProducts(string productName[], int productPrice[]) {
    int productCount = 0, itemPrice = 0, totalSalesPrice = 0, totalCommissionAmount = 0, totalSalesEarnings = 0;
    double commissionRate = 0;
    string itemName;

    displayMainMenu();
    commissionRate = askSellerForMarketPlace();
    while (commissionRate > -1) {
        cout << "Please enter the number of products: ";
        cin >> productCount;

        while (productCount <= 0 && (!(cin >> productCount))) {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Please enter a numeric value greater than 0" << endl;
            cout << "Number of products: ";
        }

        productName = new string[productCount]; // building new productName array
        productPrice = new int[productCount]; // building new productPrice array


        for (int i = 0; i < (productCount); i++) {
            cout << "Please enter the product name: ";
            cin >> itemName;
//            getline(cin, itemName);
//            cin.ignore();
            productName[i] = itemName; // stores new product name element
            cout << "Please enter the product price: $";
            cin >> itemPrice;


            while (itemPrice <= 0) {
//                cin.clear();
//                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout << "Please enter a numeric value greater than 0" << endl;
                cout << "Product Price: $";
                cin >> itemPrice;
            }

            productPrice[i] = itemPrice; // stores new product price element
        }

        cout << endl;

        cout << "Item Name" << right << setw(30) << "Item Price" << endl;
        cout << "--------------------------------------------------------------" << endl;
        for (int i = 0; i < (productCount); i++) {
            cout << productName[i] << "\t\t\t" << right << setw(9) << "$" << right << setw(9) << productPrice[i] << endl; // prints out parallel array
            totalSalesPrice = totalSalesPrice + productPrice[i];
        }

        totalCommissionAmount = totalSalesPrice * commissionRate;
        totalSalesEarnings = totalSalesPrice - totalCommissionAmount;

        cout << endl;
        cout << "--------------------------------------------------------------" << endl;
        cout << "TOTAL SALES:  " << "\t\t\t" << left << setw(3) << "$" << right << setw(9) << totalSalesPrice << endl;
        cout << "--------------------------------------------------------------" << endl;
        cout << "TOTAL COMMISSION: " << "\t\t" << left << setw(3) << "$" << right << setw(9) << totalCommissionAmount
             << endl;
        cout << "--------------------------------------------------------------" << endl;
        cout << "TOTAL EARNINGS: " << "\t\t" << left << setw(3) << "$" << right << setw(9) << totalSalesEarnings
             << endl;
        cout << "--------------------------------------------------------------" << endl;

        displayMainMenu();
        commissionRate = askSellerForMarketPlace();

    }
}
Last edited on
closed account (48T7M4Gy)
Instead of using nearly 200 lines of code that is mostly irrelevant you will be better off writing a short 5 or 6 line program with hard-coded values to develop the layout you require.

As a start, you are using multiple \t tabs which is an indicator that you are not using iomanip functionality properly - get rid of them :)
closed account (48T7M4Gy)
Like this, keeping in mind there are other ways using local currency formatting conventions.
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
#include <iostream>
#include <iomanip>

#include <string>

using namespace std;

int main()
{
    string line = "\n------------------------------\n";
    
    double totalSalesPrice = 101.78;
    double totalCommissionAmount = 2.6;
    double totalSalesEarnings = totalSalesPrice + totalCommissionAmount;
    
    cout.precision(2);
    cout << fixed;
    
    cout
    << line
    << "     TOTAL SALES: $ " << right << setw(9) << totalSalesPrice
    << line
    << "TOTAL COMMISSION: $ " << right << setw(9) << totalCommissionAmount
    << line
    << "  TOTAL EARNINGS: $ " << right << setw(9) << totalSalesEarnings
    << line;
    
    return 0;
}


------------------------------
     TOTAL SALES: $    101.78
------------------------------
TOTAL COMMISSION: $      2.60
------------------------------
  TOTAL EARNINGS: $    104.38
------------------------------
Program ended with exit code: 0
Topic archived. No new replies allowed.