我的代码已经完成,至少我相信它是...为什么它不能按预期运行?

时间:2019-05-02 00:45:08

标签: c++

我已经为家庭作业分配了此代码,并且拥有所有必需的输入文件(错误和cateringInput),但是却收到黑屏,但未收到提示消息。我犯了一个菜鸟错误,并且在没有测试它们的情况下执行了这些功能,但是我真的以为我做对了。

我尝试解决我的功能,但似乎一切正常。我知道这可能是一个愚蠢的错误,但是我似乎真的无法解决它:/

#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 个答案:

答案 0 :(得分:1)

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

有效的输入文件内容:

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

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

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