vendredi 31 mars 2017

Function not writing to txt file

I have a program that uses various structs and functions to read information into a struct from an output file, do something with it, and write to an output file if a condition is met. Everything is working properly, except the function that's supposed to write to the output file isn't doing so. I'm required to have a function that writes to the output file, so doing it in main isn't an option. Here's my code:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <climits>

using namespace std;

struct subscriberName{
    string firstName;
    string lastName;
    int custId;
};

struct address{
    string street;
    string city;
    string state;
    int zip_code;
};

struct date{
    string month;
    int day;
    int year;
};

struct renewal_information{
    int monthsLeft;
    date lastNoticeSent;
};

struct subscriber{
    subscriberName custName;
    address custAddress;
    renewal_information custInfo;
};

void openInFile(ifstream&);
void openOutFile(ofstream&);
subscriber recordIn(ifstream&, subscriber&, address&, date&, int&,     int&);
void expired(subscriber&, ofstream&);

int main() {
    ifstream inFile;
    ofstream outFile;
    openInFile(inFile);
    openOutFile(outFile);
    subscriber customer;
    address custAddress;
    date custDate;
    int currentLine=0, numProcessed=0, numExpired=0;

    while (!inFile.eof()){
        recordIn(inFile, customer, custAddress, custDate, currentLine,     numProcessed);

        if (customer.custInfo.monthsLeft==0) {
            expired(customer, outFile);
            numExpired++;
        }
    }
    cout<<endl<<string(47, '-')<<endl<<"Number of subscribers     processed: "<<numProcessed
        <<endl<<"The number of expired subscriptions is: "    <<numExpired<<endl
        <<string(47, '-')<<endl<<endl;

    inFile.close();
    outFile.close();
    return 0;
}

void openInFile(ifstream& inFile){
    string inFileName;
    do{
        cout<<"Enter input file name: ";
        cin>>inFileName;
        cout<<inFileName<<endl;
        inFile.open(inFileName.c_str());
    }
    while (!inFile);
    if (inFile.fail()){
        cout<<"input file failed to open\n";
        inFile.clear();
    } else
        cout<<inFileName<<" opened successfully\n";
}

void openOutFile(ofstream&){
    string outFileName;
    ofstream outFile;
    do{
        cout<<"Enter output file name: ";
        cin>>outFileName;
        cout<<outFileName<<endl;
        outFile.open(outFileName.c_str());
    }
    while (!outFile);
    if (outFile.fail()){
        cout<<"output file failed to open\n";
        outFile.clear();
    } else
        cout<<outFileName<<" opened successfully\n";
}

subscriber recordIn(ifstream& inFile, subscriber& customer, address&     custAddress, date& custDate, int& currentLine, int& numProcessed){
    inFile.ignore(currentLine, '\n');

    getline(inFile, customer.custName.firstName, '\n');

    if (inFile.eof()){
        return customer;
    }
    else {
        getline(inFile, customer.custName.lastName, '\n');
        inFile >> customer.custName.custId;
        cout << "==> Processing Customer ID: " <<     customer.custName.custId << endl;
        numProcessed++;

        inFile.ignore(INT_MAX, '\n');
        getline(inFile, customer.custAddress.street, '\n');
        getline(inFile, customer.custAddress.city, '\n');
        getline(inFile, customer.custAddress.state, '\n');
        inFile >> customer.custAddress.zip_code;
        inFile >> customer.custInfo.monthsLeft;
        inFile >> customer.custInfo.lastNoticeSent.month;
        inFile >> customer.custInfo.lastNoticeSent.day;
        inFile >> customer.custInfo.lastNoticeSent.year;

        currentLine = currentLine + 11;
    }
    return customer;
}

void expired(subscriber& customer, ofstream& outFile){
    while (customer.custInfo.monthsLeft==0) {
        outFile << customer.custName.firstName;
        outFile << customer.custName.lastName;
        outFile << customer.custName.custId;
        outFile << customer.custAddress.street;
        outFile << customer.custAddress.city;
        outFile << customer.custAddress.state;
        outFile << customer.custAddress.zip_code;
        outFile << customer.custInfo.monthsLeft;
        outFile << customer.custInfo.lastNoticeSent.month;
        outFile << customer.custInfo.lastNoticeSent.day;
        outFile << customer.custInfo.lastNoticeSent.year;
        customer.custInfo.monthsLeft=-1;
        outFile.flush();
    }
}

Aucun commentaire:

Enregistrer un commentaire