我已经用C ++编写了一个程序,用于我的C ++ Intro类中的赋值,但我在程序中有多个错误,但我似乎无法弄清楚如何解决所有错误。
该节目应该询问用户电影的名称,成人和儿童票的销售数量,并计算总票房利润,净票房利润和支付给经销商的金额。
我似乎无法弄清楚如何初始化变量adultTicketPrice和 childTicketPrice和我以为我声明了他们并试图弄清楚如果我已经宣布他们是否需要初始化?
childTicket价格如何超出范围?
为什么我会收到其他错误,我该如何解决?
// Michael VanZant
// Flix for Fun Profit Report
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
// Create all double variables
double adultTicketsSold, childTicketsSold, grossBoxProfit, netBoxProfit,
amtPaidDist, adultTicketPrice, childTicketPrice
adultTicketPrice = 12;
childTicketPrice = 7;
cout << fixed << setprecision(2);
// Create the string variable
string movieName;
// Get the name of the movie and store it in the movieName string
cout << "What is the name of the movie?";
getline(cin, movieName);
cout << "\n";
// Cin.ignore to ignore the string variable type
cin.ignore();
// Get the amount of adult and child tickets sold
cout << "How many adult tickets do you want to buy?";
cin >> adultTicketsSold;
cout << "\n";
cout >> "How many child tickets did you want to buy?";
cin >> childTicketsSold;
cout << "\n";
// Calculate the amount of gross box office profit and display it
grossBoxProfit = (childTicketsSold * childTicketPrice) + (adultTicketsSold * adultTicketPrice);
cout << "Gross Box Office Profit: $" << grossBoxProfit;
cout << "\n";
// Calculate the net box profit amount and display it
netBoxProfit = grossBoxProfit * .20;
cout << "Net Box Profit Amount: $" << netBoxProfit;
cout << "\n";
// Calculate the amount paid to distributor and display it
amtPaidDist = grossBoxProfit - netBoxProfit;
cout << "Amount Paid to Distributor is: $" << amtPaidDist;
return 0;
}
答案 0 :(得分:1)
当编译器说&#34;期望初始化&#34;时,它与这些行没有任何关系:
adultTicketPrice = 12;
childTicketPrice = 7;
实际上是赋值,而不是初始化(尽管一些旧的C术语会将第一个赋值称为初始化)。
不,这是因为它认为你还在这条线上,提供声明和(可选)初始化者:
double adultTicketsSold, childTicketsSold, grossBoxProfit, netBoxProfit,
amtPaidDist, adultTicketPrice, childTicketPrice
那是因为你没有在;
结尾处。
此外:
cout >> "How many child tickets did you want to buy?";
您的意思是<<
。
修复这两个小错字,代码编译。