首页 > 解决方案 > 我的代码是完整的,至少我相信它是......为什么它不能按预期运行?

问题描述

我已经为我的家庭作业编写了这个代码,并且我拥有所有必要的输入文件(错误和餐饮输入),但是我得到了一个黑屏,没有到达我的 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放在它下面,它就不会通过。我无法弄清楚:/

标签: c++

解决方案


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

有效的输入文件内容:

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

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

我根本没有修改你的来源。


推荐阅读