0

I have coded this out for my homework assignment, and I have all the necessary input files (error and cateringInput), but I am getting a black screen that doesn't reach my cout message. I made a rookie mistake and did the functions without testing them, but I really thought I had it right.

I've tried to work around my functions, but it seems everything is in order. I know it's probably some silly mistake but I really can't seem to tackle it :/

#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;
}

Just need this compiled, I am getting no output file creation nor am I getting the cout << that I made to confirm the programs been executed

EDIT 1:

Seems like this part of the code is messing something up:

    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);
            }
        }
    }

I put a COUT above this while loop and it came through, but if I place the COUT right under it, it doesn't come through. I can't figure it out though :/

Jordan S
  • 45
  • 5
  • 1
    This is the perfect use-case for a debugger. Insert a breakpoint on the first line of your `main` function, and step through the code to *see* what is happening. – Cody Gray May 02 '19 at 01:11
  • [Why is `iostream::eof` inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – molbdnilo May 02 '19 at 04:54

1 Answers1

1

I gave it a valid input file and it worked. Then I gave it an invalid input file and in failed. Maybe you just didn't give it a valid input file:

Valid input file content:

1 1 1 S Y 1

Invalid input file content:

1.x 1 1 S Y 1

Output file content for the valid input file:

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

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

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

I didn't modify your source at all.

  • Ok thats strange. I just tried your exactly contents for the valid input file and got no response still – Jordan S May 02 '19 at 01:35
  • Yup, that's strange. Maybe the file you gave it is invalid; if it is, it means it'll always loop as caracters will stop being read from the file. Let me explain: if an `int a` is expected, `cin >> a` will ignore all whitespaces until it finds a digit, but if what you read is `" something"`, it will let it as `"something"` after reading the whitespaces and when you try to read an `int` again, it'll do nothing. I think you noticed it, as you edited your question, however your problem is with parsing the input file and it's no easy task if you expect errors. – Agustín Nieto García May 04 '19 at 15:53