output formatting

I am currently writing a coffee shop program, and I want my output for monetary values to be lined up and always in the same spot, even if the characters prior to them are large, but I am not sure how to do this. I know of setw() and setfill(), and those are the only modifiers I have played around with. Any help would be appreciated. This an excerpt from the code. It takes the items purchased, and then returns the totals. Setw looks great, when it is all 0's... and the items before do not change...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
double pointOfSale (int & totalSmallCups, int & totalLargeCups, int& totalSlices, int & totalOuncesOfCoffee)
{
	
	cout << setfill (' ');
	double coffeeSalesProfits = (totalSmallCups * SMALL_CUP_PRICE) + (totalLargeCups * LARGE_CUP_PRICE);
	double cheeseCakeSaleProfits = (totalSlices * CHEESE_CAKE_SLICE_PRICE);
	double coffeeBeanSaleProfits = (totalOuncesOfCoffee * BULK_COFFEE_PRICE);
	
	cout << "Small cups of coffee = " << totalSmallCups << fixed << showpoint << setw(14) << " ($"<< setprecision (2) << (totalSmallCups * SMALL_CUP_PRICE)<< ")\n"<< endl;
	cout << "Large cups of coffee = " << totalLargeCups << fixed << showpoint << setw(14) << " ($"<< setprecision (2)<< (totalSmallCups * LARGE_CUP_PRICE)<< ")\n"<< endl;
	cout << "Slices of cheese cake sold = " << totalSlices << fixed << showpoint << setw(8) << " ($" << setprecision (2) << (totalSlices * CHEESE_CAKE_SLICE_PRICE)<< ")\n"<< endl;
	cout << "Ounces of coffee beans sold = " << totalOuncesOfCoffee << fixed << showpoint <<" ($"<< setprecision (2) << (totalOuncesOfCoffee * BULK_COFFEE_PRICE)<< ")\n"<< endl;
	double subTotal = coffeeSalesProfits + cheeseCakeSaleProfits + coffeeBeanSaleProfits;
	cout << "Your subtotal is:" <<setw(23) << "$" << subTotal << "\n" << endl;
	double grandTotal = subTotal + (subTotal* SALES_TAX_RATE);
	cout << "Taxes:" << setw(32) << "$" << (subTotal * SALES_TAX_RATE) << "\n" << endl;
	cout << "Grand Total:" <<setw(26) << "$" << grandTotal << "\n" << endl;
	cout << "Number of items sold = " << (totalSmallCups+totalLargeCups+totalSlices+totalOuncesOfCoffee) << "\n" << endl;
	system ("pause");
	cout << endl;
}
Last edited on
You could change your code in this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Example program
#include <iomanip>
#include <iostream>

int main()
{
    int totalSmallCups = 2;
    double SMALL_CUP_PRICE = 2.50;
    
 std::cout << std::fixed << std::setprecision(2) << std::showpoint;
 std::cout << std::setw(30) << std::left << "Small cups of coffe = " 
              << std::setw(14) << std::right << "$ " << std::setw(5) 
              << (totalSmallCups * SMALL_CUP_PRICE + 24) << std::endl;
    
    std::cout << std::setw(30) << std::left << "Ounces of coffee beans sold = "
                  << std::setw(14) << std::right << "$ " << std::setw(5)<< std::right 
                 << (totalSmallCups * SMALL_CUP_PRICE) << std::endl;
}


To make it easier for you to determine how the lengths should be, copy and paste Ounces of coffee beans sold = to this website http://string-functions.com/length.aspx to get the string length of the longest string you are going to output.
Last edited on
Will the place of the dollar amounts change if the quantity sold value increases in size, because that was the problem I was having. I also placed all the fixed showpoint and set precision stuff in the lines itself, because I do not want the quantity in .00, otherwise I would have placed it at the beginning.
> I know of setw() and setfill(), and those are the only modifiers I have played around with.

To print out monetary amounts, we can use the manipulatorstd::put_money
http://en.cppreference.com/w/cpp/io/manip/put_money

Note that this relies on the standard library implementation having more than mere token support for locales.
(In other words, with the GNU library, this won't work on any platform other than Linux.)

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
#include <iostream>
#include <iomanip>

const double SMALL_CUP_PRICE = 1.23 ;
const double LARGE_CUP_PRICE = 1.87 ;
const double CHEESE_CAKE_SLICE_PRICE = 2.05 ;
const double BULK_COFFEE_PRICE = 3.42 ;
const double SALES_TAX_RATE = 0.05 ;

// double pointOfSale (int & totalSmallCups, int & totalLargeCups, int& totalSlices, int & totalOuncesOfCoffee)
void pointOfSale( int totalSmallCups, int totalLargeCups, int totalSlices, int totalOuncesOfCoffee )
{
	const double small_cups_sales = totalSmallCups * SMALL_CUP_PRICE ;
	const double large_cups_sales = totalLargeCups * LARGE_CUP_PRICE ;
	const double coffeeSales = small_cups_sales + large_cups_sales ;
	const double cheeseCakeSales = (totalSlices * CHEESE_CAKE_SLICE_PRICE);
	const double coffeeBeanSales = (totalOuncesOfCoffee * BULK_COFFEE_PRICE);
	const double nett_sales = coffeeSales + cheeseCakeSales + coffeeBeanSales ;
	const double taxes = nett_sales * SALES_TAX_RATE ;
	const double gross_sales = nett_sales + taxes ;
    
    // http://en.cppreference.com/w/cpp/io/basic_ios/imbue
    std::cout.imbue( std::locale( "en_US.utf8" ) ); // note: locale name is implementation specific
    
    // http://en.cppreference.com/w/cpp/io/manip/put_money 
    std::cout << std::showbase
	          << "  Small cups of coffee = " << std::setw(4) << totalSmallCups << std::setw(10) << std::put_money(small_cups_sales*100) << '\n'
	          << "  Large cups of coffee = " << std::setw(4) << totalLargeCups << std::setw(10) << std::put_money(large_cups_sales*100) << '\n'
	          << " Slices of cheese cake = " << std::setw(4) << totalSlices << std::setw(10) << std::put_money(cheeseCakeSales*100) << '\n'
	          << "Ounces of coffee beans = " << std::setw(4) << totalOuncesOfCoffee << std::setw(10) << std::put_money(coffeeBeanSales*100) << '\n'
	          << "------------------------------------------\n"
	          << "            Nett sales = " << std::setw(14) << std::put_money(nett_sales*100) << '\n'
	          << "                 Taxes = " << std::setw(14) << std::put_money(taxes*100) << '\n'
	          << "------------------------------------------\n"
	          << "           Gross sales = " << std::setw(14) << std::put_money(gross_sales*100) << '\n' 
	          << "\n\n\n" ;
}

int main()
{
    pointOfSale( 2, 3, 4, 5 ) ;
    pointOfSale( 33, 4, 155, 66 ) ;
}

http://coliru.stacked-crooked.com/a/63df20a30ebe53ff
http://rextester.com/ATFX35870
I ended up just breaking the two apart, since the values are unpredictable, and something is always out of format. Even though if everything isn't the same because of an extra digit, they are all relatively inline, and I can live with that.
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
void exitProtocol (int & totalSmallCups, int & totalLargeCups, int& totalSlices, int & totalOuncesOfCoffee)
{
	ofstream outFile;
	cout << setfill (' ');
	outFile << setfill (' ');
	outFile.open("Sales Reciept.txt");
	double coffeeSalesProfits = (totalSmallCups * SMALL_CUP_PRICE) + (totalLargeCups * LARGE_CUP_PRICE);
	double cheeseCakeSaleProfits = (totalSlices * CHEESE_CAKE_SLICE_PRICE);
	double coffeeBeanSaleProfits = (totalOuncesOfCoffee * BULK_COFFEE_PRICE);
	
	cout << setprecision (2);
	cout << "Small coffee sales = " <<  fixed << showpoint << setw(14) << right << "$" << (totalSmallCups * SMALL_CUP_PRICE)<< "\n"<< endl;
	cout << "Large coffee sales = " <<  fixed << showpoint << setw(14) << right << "$" << (totalSmallCups * LARGE_CUP_PRICE)<< "\n"<< endl;
	cout << "Cheese cake sales  = " <<  fixed << showpoint << setw(14) << right << "$" << (totalSlices * CHEESE_CAKE_SLICE_PRICE)<< "\n"<< endl;
	cout << "Coffee beans sold  = " <<  fixed << showpoint << setw(14) << right << "$" << (totalOuncesOfCoffee * BULK_COFFEE_PRICE)<< "\n"<< endl;
	double subTotal = coffeeSalesProfits + cheeseCakeSaleProfits + coffeeBeanSaleProfits;
	cout << "Your subtotal is:" <<setw(18) << right << "$" << subTotal << "\n" << endl;
	double grandTotal = subTotal + (subTotal* SALES_TAX_RATE);
	cout << "Taxes:" << setw(29) << right << "$" << (subTotal * SALES_TAX_RATE) << "\n" << endl;
	cout << "Grand Total:" <<setw(23) << right << "$" << grandTotal << "\n" << endl;
	
	cout << setprecision (0);
	cout << "Total number of items sold = " << setw(10) << right << (totalSmallCups+totalLargeCups+totalSlices+totalOuncesOfCoffee) << "\n" << endl;
	cout << "Total small cups of coffee = " << setw(10) << right << totalSmallCups << "\n" << endl;
	cout << "Total large cups of coffee = " << setw(10) << right << totalLargeCups << "\n" << endl;
	cout << "Total slices of cheese cake sold = " << setw(4) << right << totalSlices << "\n" << endl;
	cout << "Ounces of coffee beans sold = " << setw(9) << right << totalOuncesOfCoffee << "\n" << endl;
	
	outFile << setprecision (2);
	outFile << "Small coffee sales = " <<  fixed << showpoint << setw(14) << right << "$" << (totalSmallCups * SMALL_CUP_PRICE)<< "\n"<< endl;
	outFile << "Large coffee sales = " <<  fixed << showpoint << setw(14) << right << "$" << (totalSmallCups * LARGE_CUP_PRICE)<< "\n"<< endl;
	outFile << "Cheese cake sales  = " <<  fixed << showpoint << setw(14) << right << "$" << (totalSlices * CHEESE_CAKE_SLICE_PRICE)<< "\n"<< endl;
	outFile << "Coffee beans sold  = " <<  fixed << showpoint << setw(14) << right << "$" << (totalOuncesOfCoffee * BULK_COFFEE_PRICE)<< "\n"<< endl;
	
	outFile << "Your subtotal is:" <<setw(18) << right << "$" << subTotal << "\n" << endl;
	outFile << "Taxes:" << setw(29) << right << "$" << (subTotal * SALES_TAX_RATE) << "\n" << endl;
	outFile << "Grand Total:" <<setw(23) << right << "$" << grandTotal << "\n" << endl;
	
	outFile << setprecision (0);
	outFile << "Total number of items sold = " << setw(10) << right << (totalSmallCups+totalLargeCups+totalSlices+totalOuncesOfCoffee) << "\n" << endl;
	outFile << "Total small cups of coffee = " << setw(10) << right << totalSmallCups << "\n" << endl;
	outFile << "Total large cups of coffee = " << setw(10) << right << totalLargeCups << "\n" << endl;
	outFile << "Total slices of cheese cake sold = " << setw(4) << right << totalSlices << "\n" << endl;
	outFile << "Ounces of coffee beans sold = " << setw(9) << right << totalOuncesOfCoffee << "\n" << endl;
	 
	system ("notepad.exe Sales Reciept.txt");
	system ("pause");
	cout << endl;
}
Topic archived. No new replies allowed.