gpt4 book ai didi

c++ - 我的代码是完整的,至少我相信它是......为什么它不能按预期运行?

转载 作者:行者123 更新时间:2023-11-28 04:18:48 24 4
gpt4 key购买 nike

我已经为我的家庭作业编写了代码,并且我有所有必要的输入文件(错误和 cateringInput),但是我得到一个黑屏,无法到达我的 cout 消息。我犯了一个菜鸟错误,没有测试就实现了功能,但我真的认为我做对了。

我已尝试解决我的功能问题,但似乎一切都井井有条。我知道这可能是一些愚蠢的错误,但我似乎真的无法解决它:/

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int getInput(ifstream& infile, ofstream& errorfile, int &partyID, int &numAdults, int &numChildren, char &mealType, char &isWeekend, double &deposit);
double calculateCost(int numAdults, int numChildren, char mealType);
void additionalCost(double &cost, double &tip, double &tax, double &surcharge, char isWeekend);
void toatlBill(ofstream& outfile, int partyID, int numAdults, int numChildren, double cost, double tip, double tax, double surcharge, double deposit);

int getInput(ifstream& infile, ofstream& errorfile, int &partyID, int &numAdults, int &numChildren, char &mealType, char &isWeekend, double &deposit)
{
infile >> partyID >> numAdults >> numChildren >> mealType >> isWeekend >> deposit;

int errorFlag = 1;

// Check for errors in input (validation)
if (numAdults < 0)
{
errorfile << "PartyID: " << partyID << endl;
errorfile << "Number of adults cannot be negative" << endl;
errorFlag = 0;
}

if (numChildren < 0)
{
errorfile << "PartyID: " << partyID << endl;
errorfile << "Number of children cannot be negative" << endl;
errorFlag = 0;
}

if (mealType != 'S' && mealType != 'D')
{
errorfile << "PartyID: " << partyID << endl;
errorfile << "Meal type should be either 'S' or 'D' " << endl;
errorFlag = 0;
}

if (isWeekend != 'Y' && isWeekend != 'N')
{
errorfile << "PartyID: " << partyID << endl;
errorfile << "Weekend should be either 'Y' or 'N' " << endl;
errorFlag = 0;
}

if (deposit < 0)
{
errorfile << "PartyID: " << partyID << endl;
errorfile << "Deposit amount cannot be negative" << endl;
errorFlag = 0;
}

return errorFlag;
}

double calculateCost(int numAdults, int numChildren, char mealType)
{
double adultMealCost, childrenMealCost, cost;

if (mealType == 'D')
{
adultMealCost = 25.80;
}
else
{
adultMealCost = 21.75;
}

childrenMealCost = adultMealCost * 0.6; // 60% of adult meal cost

cost = (numAdults * adultMealCost) + (numChildren * childrenMealCost);

return cost;

}

void additionalCost(double &cost, double &tip, double &tax, double &surcharge, char isWeekend)
{
tip = cost * 0.18; // 18%
tax = cost * 0.1; // 10%

double total = cost + tip + tax;

if (isWeekend == 'Y')
{
surcharge = total * 0.07; // 7%
}
else
{
surcharge = 0;
}
}

void toatlBill(ofstream& outfile, int partyID, int numAdults, int numChildren, double cost, double tip, double tax, double surcharge, double deposit)
{
double total = cost + tip + tax + surcharge;

outfile << setprecision(2) << fixed;
outfile << endl << "--------------------------------------" << endl;
outfile << endl << setw(20) << left << "PartyID: " << setw(5) << right << partyID << endl;
outfile << setw(20) << left << "Number of adults: " << setw(5) << right << numAdults << endl;
outfile << setw(20) << left << "Number of children: " << setw(5) << right << numChildren << endl;
outfile << endl << setw(20) << left << "Meal Cost: " << setw(6) << right << "$ " << cost << endl;
if (surcharge > 0)
{
outfile << setw(20) << left << "Surcharge: " << setw(6) << right << "$ " << surcharge << endl;
}
outfile << setw(20) << left << "Tax: " << setw(6) << right << "$ " << tax << endl;
outfile << setw(20) << left << "Tip: " << setw(6) << right << "$ " << tip << endl;
outfile << setw(20) << left << "Total party cost: " << setw(6) << right << "$ " << total << endl;
if (deposit > 0)
{
outfile << setw(20) << left << "Deposit: " << setw(6) << right << "$ " << deposit << endl;
}
outfile << setw(20) << left << "Total Balance due: " << setw(6) << right << "$ " << (total - deposit) << endl;
}

int main()
{
// Open the input file
ifstream infile;
infile.open("cateringInput.txt");

// Open the error file
ofstream errorfile;
errorfile.open("error.txt");

// Open the output file
ofstream outfile;
outfile.open("cateringOutput.txt");

int partyID, numAdults, numChildren;
char mealType, isWeekend;
double deposit, cost, tip, tax, surcharge;

while (!infile.eof()) // End of the file has not been reached yet
{
int errorFlag = getInput(infile, errorfile, partyID, numAdults, numChildren, mealType, isWeekend, deposit);

if (errorFlag) // Flag for errors in the data
{
if (numAdults > 0 || numChildren > 0)
{
cost = calculateCost(numAdults, numChildren, mealType);
additionalCost(cost, tip, tax, surcharge, isWeekend);
toatlBill(outfile, partyID, numAdults, numChildren, cost, tip, tax, surcharge, deposit);
}
}
}

outfile << endl << "--------------------------------------" << endl;

infile.close();
errorfile.close();
outfile.close();

cout << "Finished! Please verify by checking 'cateringOutput.txt'" << endl;

system("pause");
return 0;
}

只需要编译这个,我没有创建输出文件,也没有得到我为确认程序已执行而做的 cout <<

编辑 1:

似乎这部分代码搞砸了:

    while (!infile.eof()) // End of the file has not been reached yet
{
int errorFlag = getInput(infile, errorfile, partyID, numAdults, numChildren, mealType, isWeekend, deposit);

if (errorFlag) // Flag for errors in the data
{
if (numAdults > 0 || numChildren > 0)
{
cost = calculateCost(numAdults, numChildren, mealType);
additionalCost(cost, tip, tax, surcharge, isWeekend);
toatlBill(outfile, partyID, numAdults, numChildren, cost, tip, tax, surcharge, deposit);
}
}
}

我在这个 while 循环上面放了一个 COUT,它通过了,但是如果我把 COUT 放在它下面,它就不会通过。虽然我无法弄清楚:/

最佳答案

我给了它一个有效的输入文件,它工作了。然后我给了它一个无效的输入文件并且失败了。也许你只是没有给它一个有效的输入文件:

有效的输入文件内容:

1 1 1 S Y 1

无效的输入文件内容:

1.x 1 1 S Y 1

有效输入文件的输出文件内容:

--------------------------------------

PartyID: 1
Number of adults: 1
Number of children: 1

Meal Cost: $ 34.80
Surcharge: $ 3.12
Tax: $ 3.48
Tip: $ 6.26
Total party cost: $ 47.66
Deposit: $ 1.00
Total Balance due: $ 46.66

--------------------------------------

PartyID: 1
Number of adults: 1
Number of children: 1

Meal Cost: $ 34.80
Surcharge: $ 3.12
Tax: $ 3.48
Tip: $ 6.26
Total party cost: $ 47.66
Deposit: $ 1.00
Total Balance due: $ 46.66

--------------------------------------

我根本没有修改你的源。

关于c++ - 我的代码是完整的,至少我相信它是......为什么它不能按预期运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55944360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com