2
我有一個簡單的程序,要求輸入電影的名稱以及爲set1和set2出售的門票數量。然後用這些值進行一些計算並顯示結果。我唯一的問題是,我似乎無法按照我希望的方式排列小數值。似乎無法準確排列十進制數字
控制檯中的最後三行輸出應該始終如此(帶有美元符號和小數點排列):image。但是,當我爲門票的set1輸入1,並且爲門票的set2輸入0時,反之亦然,它看起來像這樣:image。關於如何使輸出始終與第一個屏幕截圖一致?提前致謝。
#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;
}
你釘了它。這麼簡單,但我錯過了!謝謝你,先生。 – Blueshift