I have a simple program that asks for the name of a film, and the amount of tickets sold for set1 and set2. Then it does some computation with those values and displays the results. The only issue I have is that I can't seem to line up the decimal values the way I want them to.
The last three lines of output in the console should look like this all the time (with the dollar signs AND decimal points lined up): image. But when I input 1 for set1 of tickets, and 0 for set2 of tickets and vice versa, it looks like this instead: image. Any ideas on how can I make the output ALWAYS line up nicely like in the first screenshot? Thanks in advance.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
string film = "";
int set1 = 0;
int set2 = 0;
// the cinema always takes this 20% cut.
const double CINEMA_FEE = 0.20;
double profit = 0;
double profitMinusFee = 0;
double paidToMovieMaker = 0;
cout << "What is the name of the film: ";
getline(cin, film);
cout << "How many tickets for set1 sold: ";
cin >> set1;
cout << "How many tickets for set2 sold: ";
cin >> set2;
cout << "Film Name:" << setw(20) << "\"" << film << "\"" << endl;
cout << "Set1 tickets sold:" << setw(16) << set1 << endl;
cout << "Set2 tickets sold:" << setw(16) << set2 << endl;
set1 *= 10;
set2 *= 6;
profit = set1 + set2;
profitMinusFee = profit * CINEMA_FEE;
paidToMovieMaker = profit - profitMinusFee;
// needs to always show two decimal points and fixed
cout << setprecision(2) << fixed;
cout << "The total monetary profit:" << setw(5) << "$ " << profit << endl;
cout << "The net monetary profit:" << setw(7) << "$ " << profitMinusFee << endl;
cout << "Total paid to movie maker:" << setw(5) << "$ " << paidToMovieMaker << endl;
return 0;
}
I think your problem is you are setting the width on the dollar signs rather than your numbers.
This seems to fix it for me:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
string film = "";
int set1 = 0;
int set2 = 0;
// the cinema always takes this 20% cut.
const double CINEMA_FEE = 0.20;
double profit = 0;
double profitMinusFee = 0;
double paidToMovieMaker = 0;
cout << "What is the name of the film: ";
getline(cin, film);
cout << "How many tickets for set1 sold: ";
cin >> set1;
cout << "How many tickets for set2 sold: ";
cin >> set2;
cout << "Film Name:" << setw(20) << "\"" << film << "\"" << endl;
cout << "Set1 tickets sold:" << setw(16) << set1 << endl;
cout << "Set2 tickets sold:" << setw(16) << set2 << endl;
set1 *= 10;
set2 *= 6;
profit = set1 + set2;
profitMinusFee = profit * CINEMA_FEE;
paidToMovieMaker = profit - profitMinusFee;
// needs to always show two decimal points and fixed
cout << setprecision(2) << fixed;
cout << "The total monetary profit: $ " << setw(10) << profit << endl;
cout << "The net monetary profit : $ " << setw(10) << profitMinusFee << endl;
cout << "Total paid to movie maker: $ " << setw(10) << paidToMovieMaker << endl;
return 0;
}
Remember setw()
affects the thing that comes after it.