我正在尝试编写一个程序,要求获得电影标题,成人票已售出,以及售完的儿童票,第一个问题暂停,但第二个和第三个问题放在一起
// This program displays a theaters profits
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const double ADULT_TICKET = 6.00;
const double CHILD_TICKET = 3.00;
const double PERCENT_GROSS_BOX_PROFIT = 0.20;
int main()
{
double adult_Ticketsold, child_Ticketsold, Grossboxprofit, distributorPrice,
Netboxprofit, AdultTotal, ChildTotal;
string movieTitle;
cout << "Which movie had the highest ticket sales tonight? ";
cin >> movieTitle;
cout << "How many adult tickets did we sell tonight? ";
cin >> adult_Ticketsold;
cout << "How many tickets for children? ";
cin >> child_Ticketsold;
AdultTotal = ADULT_TICKET * adult_Ticketsold;
ChildTotal = CHILD_TICKET * child_Ticketsold;
Grossboxprofit = AdultTotal + ChildTotal;
Netboxprofit = (AdultTotal + ChildTotal) * PERCENT_GROSS_BOX_PROFIT;
distributorPrice = Grossboxprofit - Netboxprofit;
cout << left << setw(15) << "Movie Name: " << right << setw(12) << movieTitle << endl;
cout << left << setw(25) << "Adult Tickets Sold: " << right << setw(4) << adult_Ticketsold << endl;
cout << left << setw(25) << "Child Tickets Sold: " << right << setw(4) << child_Ticketsold << endl;
cout << left << setw(40) << "Gross Box Office Profit: " << " S " << setprecision(2) <<
right << fixed << setw(12) << Grossboxprofit << endl;
cout << left << setw(45) << "Amount Paid to Distributor: " << " $ " << setprecision(2) <<
right << fixed << setw(12) << distributorPrice << endl;
cout << left << setw(18) << "Net Box Office Profit: " << " $ " << setprecision(2) <<
right << fixed << setw(12) << Netboxprofit << endl;
getchar();
return 0;
}
输出显示
哪部电影票价最高?复仇者 我们卖了多少张成人票?我们为儿童出售了多少张门票。
答案 0 :(得分:1)
默认情况下,cin >> moveTitle
只会将第一个空白分隔符词读入movieTitle
:如果您输入“Life of Brian”,那么您将Life
movieTitle
1}}和cin >> adult_Ticketsold
会在" of Brian"
上窒息而失败。 (如果您使用了调试器,添加了std::cout << "movieTitle " << movieTitle << '\n';
,或者在输入操作后检查了std::cin
以查看失败状态,您已经注意到了;-P)。
要读取到换行符的一行:
if (!std::getline(std::cin, movieTitle))
{
std::cerr << "error reading movieTitle\n";
exit(-1);
}