игнорировать / пропускать неверные данные из файла? c ++ - PullRequest
0 голосов
/ 26 мая 2020

проблема для новичков, программа должна читать / вычислять информацию из текстового файла, записывать ошибки в файл и выводить полезную информацию. как мне пропустить строку с неверной информацией и напечатать сообщение об ошибке вместо того, чтобы рассчитывать и печатать? я могу распечатать сообщение об ошибке перед строкой с неверными данными, но данные все еще там

    int main(int argc, const char * argv[]) {
    ifstream readFile;
    ofstream writeFile;
    ofstream errorFile;
    int partyID, adults, kids;
    char mealType, weekend;
    double deposit;
    double mealTotal, totalParty, amtOwed, tipTax, surcharge;
    readFile.open("lab6.txt");

    if (!readFile) {
        cout << "Invalid file, program closing." << endl;
        exit (1);
    }

    writeFile.open("lab6output.txt");
    errorFile.open("lab6errors.txt");

    while (!readFile.eof()) {

    userInput(readFile, partyID, adults, kids, mealType, weekend, deposit, errorFile);
   mealTotal = mealCost(adults, kids, mealType);
    tipTaxSur(weekend, mealTotal, tipTax, surcharge);
    showTotal(writeFile, partyID, adults, kids, mealType, weekend, mealTotal, tipTax, surcharge, deposit, totalParty, amtOwed);

    }
    writeFile.close();
    errorFile.close();
    return 0;
}

void userInput(ifstream& readFile, int& partyID, int& adults, int& kids, char& mealType, char& weekend, double& deposit, ofstream& errorFile) {

    readFile >> partyID >> adults >> kids >> mealType >> weekend >> deposit;

    if (adults < 0) {
        errorFile << "Error: Adults cannot be less than zero:" << "  " << partyID << "  " << adults << "  " << kids << "  " << mealType << "  " << weekend << "  " << deposit;
        cout << "Error: Adults cannot be less than zero" << endl;
        readFile.ignore();
    }
    if (kids < 0) {
        errorFile << "Error: Kids cannot be less than zero:" << "  " << partyID << "  " << adults << "  " << kids << "  " << mealType << "  " << weekend << "  " << deposit;
        cout << "Error: Kids cannot be less than zero" << endl;
        readFile.ignore();
    }
    if (mealType != 'S' && mealType != 's' && mealType != 'D' && mealType != 'd') {
        errorFile << "Error: Meal type must be S or D:" << "  " << partyID << "  " << adults << "  " << kids << "  " << mealType << "  " << weekend << "  " << deposit;
        cout << "Error: Meal type must be S or D" << endl;
        readFile.ignore();
    }
    if (weekend != 'Y' && weekend != 'y' && weekend != 'N' && weekend != 'n') {
        errorFile << "Error: Weekend must be Y or N:" << "  " << partyID << "  " << adults << "  " << kids << "  " << mealType << "  " << weekend << "  " << deposit;
        cout << "Error: Weekend must be Y or N" << endl;
        readFile.ignore();
    }
    if (deposit < 0) {
        errorFile << "Error: Deposit cannot be less than zero:" << "  " << partyID << "  " << adults << "  " << kids << "  " << mealType << "  " << weekend << "  " << deposit;
        cout << "Error: Deposit cannot be less than zero" << endl;
        readFile.ignore();
    }

}

...

void showTotal(ostream& writeFile, int partyID, int adults, int kids, char mealType, char weekend, double& mealTotal, double& tipTax, double& surcharge, double deposit, double& totalParty, double& amtOwed){
    totalParty = (mealTotal + tipTax + surcharge);
    amtOwed = (mealTotal + tipTax + surcharge) - deposit;
    cout << fixed << showpoint << setprecision(2);
    cout << setw(5);

    writeFile << partyID << "  " << adults << "  " << kids << "  " << mealType << "  " << mealTotal << "  " << tipTax << "  " << surcharge << "  " << totalParty << endl;
    cout << "Party ID : " << partyID << " Meal Total:  " << mealTotal << " Tip/Tax: " << tipTax << " Surcharge: " << surcharge << " Total Cost: " << totalParty << " Amount Owed: " << amtOwed << endl;
}

...