Hi I'm trying to make a program that reads data from a text document and allows me to modify it. I am stuck with the display() function I can get the printf statement to display all my array values except the char AD value. When I include flight[i].AD it causes the program to crash. Please help. Thank you.
The text file looks like this:
Delta 195 A SanFrancisco 1427 1427
American 65 D KansasCity 1812 1830
Southwest 100 A Dallas 1600 1605
United 123 D LasVegas 1800 1800
Delta 157 A SanFrancisco 1730 1745
American 53 A SanDiego 1010 1010
Southwest 221 D Dallas 1400 1405
Southwest 125 A Austin 815 815
Southwest 63 A LasVegas 1000 1000
Southwest 200 D SanFrancisco 1200 1200
American 210 A KansasCity 1500 1500
American 211 D LasVegas 730 730
United 101 A LasVegas 900 910
United 102 A LasVegas 945 945
United 103 A LasVegas 1100 1100
But with more spaces.
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
|
#include <fstream>
using namespace std;
//named constants
const int MAX=100; //maximum flights
const int SIZE=20; //maximum characters
//struct definition
struct FlightType
{
char name[SIZE];
int number;
char AD;
char dest[SIZE];
int expected;
int actual;
};
//function prototypes
void readFile(FlightType flight[],int& count);
void display(const FlightType flight[],int count);
int main()
{
FlightType flight[MAX];
int track;
readFile(flight,track);
display(flight,track);
system("pause");
}
/************************readFILE********************/
void readFile(FlightType flight[],int& count)
{
//create file pointer
ifstream inFile;
int index=0;
//open file
inFile.open("Airport.txt");
//check if file was found
if(inFile.fail())
cout<<"File not found."<<endl;
else
{
while(!inFile.eof()&& index<MAX)
{
inFile.get(flight[index].name,SIZE-1);
inFile>>flight[index].number;
inFile>>flight[index].AD;
inFile>>flight[index].dest;
inFile>>flight[index].expected;
inFile>>flight[index].actual;
index++;
inFile.ignore();
}
count=index;
//close file
inFile.close();
}
}
void display(const FlightType flight[],int count)
{
for(int i=0; i<count; i++)
{
printf("%20s %3d %20s %4d %4d\n",flight[i].name,flight[i].number,
flight[i].dest,flight[i].expected,flight[i].actual);
}
}
|