1
void budgetCheck() {
    for (int i = 0; i != 100; i++) {

        if (arrayTotalCost[i][2] > arrayTotalCost[i][0]) {
            outputValidPackage(i);
        }
        else outputInvalidPackage(i);
    }
}

void outputValidPackage(int i) {
    fstream validPackage;
    if (validFirst = false) {

        validPackage.open("requestOutValid.txt");
        validFirst == true;
    }
    validPackage << "Total cost is : " << arrayTotalCost[i][0] << " , the budget is : " << arrayTotalCost[i][2] << endl;
    validPackage << "The Sydney to Tokyo flight is on day " << flightTicketArray[i][1] << " and costs " << flightTicketArray[i][3] << endl;
    validPackage << "The Tokyo to Sydney flight is on day " << flightTicketArray[i][2] << " and costs " << flightTicketArray[i][4] << endl;
    validPackage << "A " << hotelArray[i][3] << " star hotel, from day " << hotelArray[i][1] << " to " << hotelArray[i][2] << " will cost " << hotelArray[i][4] << endl;




}

void outputInvalidPackage(int i) {
    fstream invalidPackage;
    if (invalidFirst == false) {
        invalidPackage.open("requestOutInvalid.txt");
        invalidFirst = true;
    }
    invalidPackage << "Package is invalid" << endl << endl;

}

The goal of the code is to take in requests from a text file, and then output the valid and invalid requests into separate text files.

Everything else in the code is working.

I would expect 100 cases between the two text files. But only one is outputting. I am not sure why, the outputted case is seemingly random but the same one every time. The 92nd case. It is not the last valid package or the first invalid. Though it is a valid case.

EDIT : changed = to == . Now one case is printed to both files.

  • 1
    `if (invalidFirst = false)` shouldnt be `if (invalidFirst == false) {`? – Naveen Oct 29 '18 at 11:28
  • 1
    You should enable compiler warnings. Compiler would immidiately emit a warning when it sees `if(something = false)` – Yksisarvinen Oct 29 '18 at 11:29
  • 1
    It would be helpful if there were a main function which would allow me to paste your code into my compiler. That is what is meant by a [Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve). – Gardener Oct 29 '18 at 11:35
  • 1
    Why do you assume your file stays open after function finishes? `std::fstream` will clean up after itself and close the file that it had opened. These conditions are making your code invalid when function is called for second (or more) time. It seems you should learn about [RAII](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) - it's one of the most important design patterns in C++. You can open file in class constructor and safely use it, and it will be cleaned up when the object is destroyed. – Yksisarvinen Oct 29 '18 at 11:45

0 Answers0