I need to read these numbers from a file:

2   10.5
5   20
10  30.2

It is not working. It skips over numbers. The first row (2,5,10) is supposed to be Hours and the second row (10.5, 20, 30.2) is supposed to be Pay.Any help is appreciated.Thank you.
Note: This section is supposed to be apart of a bigger program that calculates base pay using a Function.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream inputFile;
string hours, pay;

// Test File.
inputFile.open("Lab12aInput.txt");
if (!inputFile)
{
cout <<"Error file does not exist.\n";
}
while (inputFile >> hours || inputFile >> pay)
{
inputFile >> hours >> pay; // Read data from file
cout << "Hours: " << hours << " and " "Pay: " << pay << endl;
}
// Close the file.
inputFile.close();

return 0;

Recommended Answers

All 6 Replies

Line 17 AND line 19 are reading from file.

Change line 17 to this:
while (inputFile >> hours && inputFile >> pay)

Remove line 19.

Awesome. :) I am almost done but i have another question. My code is not printing all 3 net pay totals to my output file. Why?

#include <iostream>
#include <fstream>
using namespace std;

// Function Prototype.
double CalNetPay(double,double);

int main()
{
    ifstream inputFile; // Input File.
    ofstream outputFile; // Output File.
    double hoursWorked, hourlyPayRate; // Hold Hours and Rate.
    double total;

    // Test File.
    inputFile.open("Lab12aInput.txt");
    if (!inputFile)
    {
        cout <<"Error file does not exist.\n";
    }
    while (inputFile >> hoursWorked && inputFile >> hourlyPayRate)
        {
            // Read data from file
            cout << "Hours: " << hoursWorked << " and " "Pay: " << hourlyPayRate << endl;
            // Call Function.
            total = CalNetPay(hoursWorked,hourlyPayRate);
            //Display Answer.
            cout << "Your net Pay is: " << total <<endl;
        }
        // Write data to output file.
        outputFile.open("Lab12Output.txt");
        // Output Answer into File.
        outputFile << "Net Pay: " << total << endl;

        // Close Input and Output files.
        inputFile.close();
        outputFile.close();
    return 0;
}
// Value-Returning Function.
double CalNetPay(double hoursWorked, double hourlyPayRate)
{
    return hoursWorked * hourlyPayRate;
}

You are processing all the inputs in the while loop.
Then you one time write a value to output. Only the last data from the input reading gets stored.

I'd restructure to open the output file right after you've opened and tested the input file. Then after each input line is processed in the loop, write its result, also inside the loop.

Additionally, you test for success of the file opening attempt, but in the event of error you still go on to do the reading and processings - of nothing! You should either exit the program in the error handling block, or put the whole proccessing section in an else block. You should also test for success of the output file opening attempt.

Alright, I fixed it. I put line 30/31 directly above the while statement and put line 32/33 below the last cout statement inside the while loop. Works like a charm now. Thank you for your help.

In regards to the test file, I forgot to mention that I created a file with my values to go along with this file. It works properly so I didn't think to mention it. When I delete the input file it does show an error message. Is an else loop nessecary?

Is an else loop nessecary?

First of all, else is not a loop - it's a one time action.

Yes, you should have an else that encloses your action, if you don't just exit the program at that point. So one of the two models below should be used.

f.open( "somefile.txt" )
if( !f )
{
    cout << "Error opening file. Exiting now.";
    return -1;
}
//otherwise, main body of program continues


//   or   

f.open( "somefile.txt" )
if( !f )
{
    cout << "Error opening file. Skipping main work";
}
else
{
    //otherwise, main body in the else block
}

//program only exits at its normal end point

Okay, I see what you mean. I'll enclose the bottom half in an else. Thank you for explaining, I really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.