Having trouble formatting and inserting $ sign

Hey everybody! Im doing a project for my fundamentals of programming class and I'm almost done but Im having trouble inserting a dollar sign ($) next to the values of
totalRevenue, revenue2Theater, and revenue2Distrib. I tried multiple ways to insert the dollar sign ($) but the whole formatting gets screwed up in the output. Thanks.

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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
 //define a list of vars & input
 	string movieName;
 	int aTicketsSold,
 		cTicketsSold;
 	double aTicketPrice = 6.00,
 		   cTicketPrice = 4.00,
 		   aRevenue,
 		   cRevenue,
 		   totalRevenue,
 		   revenue2Theater,
 		   revenue2Distrib;
 	
 	cout << "What is the title of the movie?"<<endl;
 	getline(cin, movieName);
 	
 	cout << "How many adult tickets were sold?" << endl;
 	cin >> aTicketsSold;
 	
 	cout << "How many child tickets were sold?" << endl;
 	cin >> cTicketsSold;


 //processing
 aRevenue = aTicketPrice * aTicketsSold;
 cRevenue = cTicketPrice * cTicketsSold;
 totalRevenue = aRevenue + cRevenue;
 revenue2Theater = totalRevenue * 0.20;
 revenue2Distrib = totalRevenue * 0.80;
 
 //output
 cout<<"Movie name:"<<right<<setw(15)<<movieName<<endl;
 cout<<"Adult tickets sold:"<<right<<setw(15)<<aTicketsSold<<endl;
 cout<<"Child tickets sold:"<<right<<setw(15)<<cTicketsSold<<endl;
 cout<< setprecision(2)<< fixed << showpoint;
 cout<<"Gross box office profit:"<<right<<"$"<<setw(15)<<totalRevenue<<endl;
 cout<<"Net box office profit:"<<right<<"$"<<setw(15)<<revenue2Theater<<endl;
 cout<<"Amount paid to distributor:"<<right<<"$"<<setw(15)<<revenue2Distrib<<endl;
 return 0;  
}
Last edited on
If you absolutely want to be sure that the "$" comes out right next to something, you could make them one string.

1
2
string totalRevenueOutputString = string("$") + std::to_string(totalRevenue);
cout<<"Gross box office profit:"<<right<<setw(15)<<totalRevenueOutputString <<endl;

If the standard library supports the US locale (ie. any reasonable library other than the GNU library on non-linux platforms), std::put_money can be used.
http://en.cppreference.com/w/cpp/io/manip/put_money

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

int main()
{
    const double amount_in_dollars = 123.456789 ;

    const unsigned long long cents = amount_in_dollars * 100.0 + 0.5 ;
    
    // imbue the locale for US. note: supported locales and locale names other than 
    // standard C locale names ("C" and "POSIX") are implementation defined 
    std::cout.imbue( std::locale("en_US.utf8") );
    
    std::cout << std::showbase 
              << std::setw(12) << std::put_money(cents) << '\n' 
              << std::setw(12) << std::put_money( cents, true ) << '\n' ;
}

http://coliru.stacked-crooked.com/a/f60e584bc1e29880
http://rextester.com/ANFZ80553
I got a compilation error for both of these suggestions. It said that to_string and put_money was not a member of std.
Use a compiler/library that is not ancient, and enable support for C++11 or later. eg.
g++ -std=c++11 -O3 -Wall -Wextra -pedantic-errors ....
Topic archived. No new replies allowed.