output align problems

I'm having trouble aligning a price column when showing the menu and when the customer's check is being calculated. I know I'm just missing something little but I've been banging my head against my keyboard for the past hour or so. A little help would be appreciated.

Here is my 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
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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

// struct definition and declaration
struct menuItemType
{
	string menuItem;
	double itemPrice;
} plainEgg, baconEgg, muffin, frenchToast, fruitBasket, cereal, coffee, tea;

// function prototype
void getData(menuItemType menuList[]);
void showMenu(menuItemType menuList[], int x);
void printCheck(menuItemType menuList[],int userOrder[], int x);

// constants
const int MENU_ITEMS = 8;
const double SALES_TAX = 0.05;

int main()
{
	menuItemType menuList[MENU_ITEMS];
	int userOrder[MENU_ITEMS] = {0};
	int orderChoice = 0;
	bool ordering = true;
	int count = 0;

	getData(menuList);

	showMenu(menuList, MENU_ITEMS);

	while (ordering)
	{
		cout << "Please enter the number of the item that corresponds to the item you would like to purchase. Enter 0 to finalize your order." << endl;
		cin >> orderChoice;
		if (orderChoice > 0 && orderChoice <= MENU_ITEMS)
		{
			userOrder[orderChoice - 1] += 1;
		}
		else
			ordering = false;
	}

	printCheck(menuList, userOrder, MENU_ITEMS);

	return 0;
}
void getData(menuItemType menuList[])
{
	// initizlize menuItemType variables
	plainEgg.menuItem = "Plain Egg";
	plainEgg.itemPrice = 1.45;
	baconEgg.menuItem = "Bacon and Egg";
	baconEgg.itemPrice = 2.45;
	muffin.menuItem = "Muffin";
	muffin.itemPrice = 0.99;
	frenchToast.menuItem = "French Toast";
	frenchToast.itemPrice = 1.99;
	fruitBasket.menuItem = "Fruit Basket";
	fruitBasket.itemPrice = 2.49;
	cereal.menuItem = "Cereal";
	cereal.itemPrice = 0.69;
	coffee.menuItem = "Coffee";
	coffee.itemPrice = 0.50;
	tea.menuItem = "Tea";
	tea.itemPrice = 0.75;

	menuList[0] = plainEgg;
	menuList[1] = baconEgg;
	menuList[2] = muffin;
	menuList[3] = frenchToast;
	menuList[4] = fruitBasket;
	menuList[5] = cereal;
	menuList[6] = coffee;
	menuList[7] = tea;
}
void showMenu(menuItemType menuList[], int x)
{
	int count;

	cout << "----------Welcome to Gloria's Glorious Breakfast Nook----------" << endl;
	for (count = 0; count < x; count++)
	{
		cout << fixed << setprecision(2) << "[" << count + 1 << "]" << menuList[count].menuItem << right << setw(20) << "$" << menuList[count].itemPrice << endl;
	}
}
void printCheck(menuItemType menuList[], int userOrder[], int MENU_ITEMS)
{
	double amountDue = 0;
	double tax;

	cout << "---------------Customer's Check:---------------" << endl;
	for (int i = 0; i < MENU_ITEMS; i++)
	{
		if (userOrder[i] > 0)
		{
			cout << fixed << setprecision(2) << menuList[i].menuItem << right << setw(20) << "$" << (menuList[i].itemPrice * userOrder[i]) << endl;

			amountDue += (menuList[i].itemPrice * userOrder[i]);
		}
	}

	tax = amountDue * SALES_TAX;

	cout << "Tax:" << right << setw(14) << "$" << tax << endl;
	cout << "Amount Due:" << right << setw(14) << "$" << amountDue << endl;
}
And the output:

----------Welcome to Gloria's Glorious Breakfast Nook----------
[1]Plain Egg                   $1.45
[2]Bacon and Egg                   $2.45
[3]Muffin                   $0.99
[4]French Toast                   $1.99
[5]Fruit Basket                   $2.49
[6]Cereal                   $0.69
[7]Coffee                   $0.50
[8]Tea                   $0.75
Please enter the number of the item that corresponds to the item you would like to purchase. Enter 0 to finalize your order.
2
Please enter the number of the item that corresponds to the item you would like to purchase. Enter 0 to finalize your order.
3
Please enter the number of the item that corresponds to the item you would like to purchase. Enter 0 to finalize your order.
7
Please enter the number of the item that corresponds to the item you would like to purchase. Enter 0 to finalize your order.0
---------------Customer's Check:---------------
Bacon and Egg                   $2.45
Muffin                   $0.99
Coffee                   $0.50
Tax:             $0.20
Amount Due:             $3.94
I don't know if there is a proper formating solution for this problem. But you could ensure that your menuItem strings are all have the same length.
E.g.:
1
2
3
4
5
6
7
8
for (int i = 0; i < 8; ++ i)
{
    int appendedSpaces = 20 - menuList[i].menuItem.length();  // detects how many spaces should append
    for (int k = 0; k < appendedSpaces; ++ k)
    {
        menuList[i} += ' ';
    }
}

Have you tried putting a setw() in front of the items as well as before the price?

And don't forget that the "left" and "right" manipulators are sticky, meaning that they stay in effect until changed again.

1
2
3
4
5
6
7
8
    cout << "----------Welcome to Gloria's Glorious Breakfast Nook----------" << endl;

    for(count = 0; count < x; count++)
    {
        cout << fixed << setprecision(2) << "[" << count + 1 << "]" << setw(20)
             << left << menuList[count].menuItem << right << setw(20) << "$"
             << menuList[count].itemPrice << endl;
    }


Got it working with jlb's suggestion although I can see how yours would work nuderob. I already had most of the code set up for the second option I just couldn't work my left and rights aligns correctly.
Topic archived. No new replies allowed.