Reading data from a file wit While loop

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.

[code]
Put the code you need help with here.
[#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;
]
1
2
3
4
5
while (inputFile >> hours || inputFile >> pay)
{
    inputFile >> hours >> pay; // Read data from file
    cout << "Hours: " << hours << " and " "Pay: " << pay << endl;
}

The way the (non-overloaded) logical OR operator works, if one operand evaluates to true, then anything after that never even gets touched.
So if inputFile >> hours succeeds, inputFile >> pay will never happen.

The next problem is that after you get input in the while loop condition, you try to get input again right after that (so you end up "skipping" the previous input).

So to fix both of those, change that code above to
1
2
3
4
5
while (inputFile >> hours >> pay)
{
    // We already have our input; no need to get it again
    cout << "Hours: " << hours << " and " "Pay: " << pay << endl;
}
Okay, I fixed that part and it works like a charm, but not when I try to write my Net Pay data to file it only prints the last row.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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;
}
Move this to right before the while loop:
30
31
// Write data to output file.
outputFile.open("Lab12Output.txt");

Then move this into the while loop:
32
33
// Output Answer into File.
outputFile << "Net Pay: " << total << endl;
Perfect, Thank You So Much!! :)
Topic archived. No new replies allowed.