DJ Tonedeaf
DJ Tonedeaf

Reputation: 5

C++ Incorrect Looping Algorithm (or file processing issue)

I'm trying to make a program that reads a text file, does some math, displays answers, then writes a text file. The program is currently only writing one line of text instead of the 5 I want.

The input text file is formated as such:

    string double double
    string double double
    string double double
    string double double
    string double double

The output text file becomes this: string, double, double, double,

It writes what I want, but only once. I want it to process all 5 lines. My program only processes the last line of the input text file.

This is what the input text file looks like (only without the file name)

    //payinfile.txt
    2198514 17.20 11.25
    6698252 59.25 21.00
    5896541 50.00 10.00
    8863214 45.00 18.20
    8465555 25.75 14.80

and here is the code for my program

    // outputprogram.cpp
    #include <iostream>
    #include <fstream> // file stream
    #include <iomanip>
    #include <string>
    #include <cstdlib> // exit function prototype
    using namespace std;

    void outputLine(const string, double, double); // prototype
    // void writeToFile();

    int main()
    {
        // ifstream constructor opens the file          
        ifstream inputFile("payinfile.txt", ios::in);

        // exit program if ifstream could not open file
        if (!inputFile)
        {
            cerr << "File could not be opened" << endl;
            exit(EXIT_FAILURE);
        } // end if

        string ID; // the account number
        double hours; // the account owner's name
        double rate; // the account balance

        cout << left << setw(10) << "ID" << setw(15)
            << "Hours" << setw(10) << "Rate" << right << setw(10) << "Gross" << endl << fixed << showpoint;

        // display each record in file
        while (inputFile >> ID >> hours >> rate)
        {
            outputLine(ID, hours, rate);
        }

    } // end main

    // display single record from file
    void outputLine(const string ID, double hours, double rate)
    {
        double gross;
        gross = 0.0;
        gross = (hours * rate);

        cout << left << setw(10) << ID 
             << setw(15) << hours
             << setw(15) << setprecision(2) << rate
             << setw(10) << setprecision(2) << gross << endl;

        // ofstream constructor opens file                
        ofstream writeToFile("FinalOutput.txt", ios::out);

        // exit program if unable to create file
        if (!writeToFile) // overloaded ! operator
        {
            cerr << "File could not be opened" << endl;
            exit(EXIT_FAILURE);
        } // end if

        writeToFile << ID << ", " << hours << ", " << rate << ", " << gross << ", ";


    } // end function outputLine

After execution this is what the output file looks like:

    //FinalOutput.txt
    8465555, 25.75, 14.8, 381.1,

So it writes what I want, I just also want it to also write the other 4 lines to FinalOutput.txt

Upvotes: 0

Views: 40

Answers (1)

The Dark
The Dark

Reputation: 8514

In this line:

 ofstream writeToFile("FinalOutput.txt", ios::out);

you are opening the output file each time you want to write a line. This will truncate the file (i.e. remove the contents).

You can either open the file in append mode each time, or better still, open the file once outside of the function and pass the stream object by reference into the outputLine function.

Upvotes: 3

Related Questions