Stuck in infinite loop
I am stuck in an infinite loop.
How do I get the second and third line of this file to output?
BusInput file
111 100.00 200.00 50.00
222 200.00 300.00 100.00
333 400.00 500.00 75.00
I have other calculations to make and add later but I am stuck here right now.
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
|
#include <iostream>
#include <iomanip>
#include <fstream>
const int BUS_NUMS = 200;
const int NUM_COLS = 6;
int mult_table[BUS_NUMS][NUM_COLS];
int next, busNum;
double fuel, equip, misc, dFuel;
using namespace std;
int main()
{
ifstream inputFile;
ofstream outputFile;
cout << "Tour Bus Company \n\n";
cout << fixed << setprecision(2);
cout << "Bus No." << setw(10) << "Fuel" << setw(16);
cout << "Equipment" << setw(9) << "Misc" << setw(20);
cout << "Discount Fuel" << setw(15) << "Final Cost" << endl;
inputFile.open("BusInput.txt");
int count = 0;
//(count < BUS_NUMS, NUM_COLS); //keeps count inside array size.
inputFile >> busNum >> fuel >> equip >> misc; //reads everything in file into array
while (!inputFile.eof())
{
cout << endl << busNum << setw(10) << fuel << setw(16);
cout << equip << setw(9) << misc << setw(20) << endl;
count++;
}
system("pause");
return 0;
}
|
Last edited on
You need to read the file in a while loop, but first you need to check if your file is open.
1 2 3 4 5 6 7 8 9 10
|
ifstream inputFile("BusInput.txt");
if (!inputFile)
{
cerr << "\aError opening file";
return -1;
}
while(inputFile >> busNum >> fuel >> equip >> misc)
{
// do sth. with your input
}
|
Thank you. This fix worked for the most part.
I had to take out the intputFile >> before my while loop for it work properly.
Topic archived. No new replies allowed.