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();
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;
}