Problem with reading .txt file

Hi, I'm trying to create a program to calculate the flight time and fuel burn of an airplane. However, the output didn't show the whole data from the text file, and when I repeated the whole calculation, it did not show any result. I can't figure out why.

The output
http://postimg.org/image/guhxrcyr5/

The txt file
http://postimg.org/image/6vsnuf9rh/

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

double computeFlightTime (double distance, double speed);
double computeFuelBurned (double flightTime, double burnRate);
double computeRemainingFuel (double fuelBurned, double initialFuel);

int main  (double distance, double speed, double burnRate, double initialFuel )
{
        ifstream inFile;
	string inFileName, name, note;
	char choice = 'y';
	double flightTime, fuelBurned, fuelRemain;
        double totalFlightTime;
        double flightRange;
        cout << fixed << showpoint << setprecision(1);   

	
  while(choice == 'y')
  {
       cout << "Welcome to Rob's airplane Flight Time and Fuel Burn calculator!" << endl;
       cout << "What is the name of the file containing the destination airports?" << endl;
       cin >> inFileName;
       inFile.open(inFileName.c_str());
       cout << "What is the speed of your airplane in miles per hour? ";
       cin >> speed;
       cout << "How many gallons of fuel does your airplane carry? ";
       cin >> initialFuel;
       cout << "How many gallons of fuel does your airplane burn per hour? ";
       cin >> burnRate;
	 
       cout << left  << setw(9) << "\nName";
       cout << right << setw(13) << "Distance";
       cout << right << setw(14) << "  Flight Hours";
       cout << right << setw(16) << "  Gallon Burned";
       cout << right << setw(16) << "  Fuel Remaining";
       cout << right << setw(10) << "Note";
       cout << "\n" << endl;
       cout << "--------------------------------------------------------------------------------";
       flightTime = computeFlightTime(distance, speed); 
       fuelBurned = computeFuelBurned(flightTime, burnRate);
       fuelRemain = computeRemainingFuel(fuelBurned, initialFuel);
       totalFlightTime = initialFuel / burnRate;
       flightRange = speed * totalFlightTime;
       inFile >> name >> distance;
       cout << "The total time the airplane can fly is " << totalFlightTime << 
		"hours " << ", and can fly " << flightRange << "miles" << endl;    
	if(inFile.is_open())
     {	
        while(!inFile.eof())
		{
			flightTime = computeFlightTime(distance, speed); 
	                fuelBurned = computeFuelBurned(flightTime, burnRate);
                        fuelRemain = computeRemainingFuel(fuelBurned, initialFuel);
			inFile >> name >> distance;
                        if( fuelRemain <= 0)
	                 {
	                       note = "Insufficient Fuel";
                         }
		        cout << left  << setw(14) << name;
                        cout << right << setw(8) << distance;
                        cout << right << setw(10) << flightTime;
                        cout << right << setw(14) << fuelBurned;
                        cout << right << setw(14) << fuelRemain;
                        cout << right << setw(20) << note;
                        cout << "\n" << endl;
                        inFile >> name >> distance;  
		}
     }
    else cout << "Unable to open file"; 
	 cout << "Would you like to do another calculation? :";
	 cin >> choice;
  }
		inFile.clear();
		inFile.seekg(0, ios::beg);
		system("\nPAUSE");
		return(0);
}
double computeFlightTime (double distance, double speed)
{
		
	return (distance / speed);   
}       
double computeFuelBurned (double flightTime, double burnRate)
{
	return (flightTime * burnRate);  
}     
double computeRemainingFuel (double fuelBurned, double initialFuel)
{
       
        return (initialFuel - fuelBurned);
Last edited on

1. main should only be one of the following two,
1
2
int main() {} 
int main(int, char**) {}


2. Don't use eof for reading the text file. The most natural way to read that file would be something like
1
2
3
4
5
std::ifstream is(filename);
std::string name;
double distance;
while(is >> name >> distance)
        //do something 


3. You should really indent your code, its hard to read.
I changed my code based on your advise, but the output still missed 1 data from the text file, and when I did another calculation, it didn't show the result. btw, sorry for the poor indentation in the first code.

Here are the outputs:

http://postimg.org/image/utr4xzmx3/
http://postimg.org/image/98r2k14mn/

Last edited on
Could you post the updated code?
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

double computeFlightTime (double distance, double speed);
double computeFuelBurned (double flightTime, double burnRate);
double computeRemainingFuel (double fuelBurned, double initialFuel);

int main()
{
    std::ifstream is("AirportInfoWin.txt");
    std::string name, note;
    double speed = 0;
    double distance = 0;
    double burnRate = 0;
    double initialFuel = 0;
    char choice = 'y';
    double flightTime, fuelBurned, fuelRemain;
    double totalFlightTime;
    double flightRange;
    cout << fixed << showpoint << setprecision(1);   

    while(choice == 'y')
     {
       cout << "Welcome to Rob's airplane Flight Time and Fuel Burn calculator!" << endl;
       cout << "What is the speed of your airplane in miles per hour? ";
       cin >> speed;
       cout << "How many gallons of fuel does your airplane carry? ";
       cin >> initialFuel;
       cout << "How many gallons of fuel does your airplane burn per hour? ";
       cin >> burnRate;
       flightTime = computeFlightTime(distance, speed); 
       fuelBurned = computeFuelBurned(flightTime, burnRate);
       fuelRemain = computeRemainingFuel(fuelBurned, initialFuel);
       totalFlightTime = initialFuel / burnRate;
       flightRange = speed * totalFlightTime;
       is >> name >> distance;
       cout << "The total time the airplane can fly is " << totalFlightTime << 
	       "hours " << ", and can fly " << flightRange << "miles" << endl;
       cout << left  << setw(9) << "\nName";
       cout << right << setw(13) << "Distance";
       cout << right << setw(14) << "  Flight Hours";
       cout << right << setw(16) << "  Gallon Burned";
       cout << right << setw(16) << "  Fuel Remaining";
       cout << right << setw(10) << "Note";
       cout << "\n" << endl;
       cout << "--------------------------------------------------------------------------------";
           
       if(is.is_open())
       {	
         while(is >> name >> distance)
		{
			flightTime = computeFlightTime(distance, speed); 
	                fuelBurned = computeFuelBurned(flightTime, burnRate);
                        fuelRemain = computeRemainingFuel(fuelBurned, initialFuel);
                        if( fuelRemain <= 0)
	                 {
	                    note = "Insufficient Fuel";
                         }
			cout << left  << setw(14) << name;
                        cout << right << setw(8) << distance;
                        cout << right << setw(10) << flightTime;
                        cout << right << setw(14) << fuelBurned;
                        cout << right << setw(14) << fuelRemain;
                        cout << right << setw(20) << note;
                        cout << "\n" << endl;
                }
      }
     else cout << "Unable to open file"; 
     cout << "Would you like to do another calculation? :";
     cin >> choice;
  }
		is.clear();
		is.seekg(0, ios::beg);
		system("\nPAUSE");
		return(0);
}
double computeFlightTime (double distance, double speed)
{
		
	return (distance / speed);   
}       
double computeFuelBurned (double flightTime, double burnRate)
{
	return (flightTime * burnRate);  
}     
double computeRemainingFuel (double fuelBurned, double initialFuel)
{
       
        return (initialFuel - fuelBurned);
}
The problem is here
1
2
3
4
5
6
       flightTime = computeFlightTime(distance, speed); //remove
       fuelBurned = computeFuelBurned(flightTime, burnRate); //remove
       fuelRemain = computeRemainingFuel(fuelBurned, initialFuel); //remove
       totalFlightTime = initialFuel / burnRate;
       flightRange = speed * totalFlightTime;
       is >> name >> distance;  //remove 


You are reading one {name distance} from the text file outside the while(is >> name >> distance) loop. After the instruction in line 41 is executed, the next time you read with is >> name >> distance you are reading from the second entry, not the first. Try removing lines 36, 37, 38 and 41.
Last edited on
I got it now, thanks a lot. I am really appreciate your help.
Topic archived. No new replies allowed.