Question about reading in flights from file

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 2181 Views - Last Post: 07 September 2011 - 05:56 PM Rate Topic: -----

#1 skatingrocker17   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 138
  • Joined: 01-September 11

Question about reading in flights from file

Posted 05 September 2011 - 03:54 PM

I'm working on a pretty large program that is mostly written for us but we're supposed to write some of the functions.

I'm stuck on the first function, which requires quite a bit of information to be read in from a text file and I'm totally stuck on it. Our teacher said he knows we haven't learned most of this stuff but he's going to throw us "in the water to see if we can swim."

ANY information can help me, I am incredibly lost here.



This is the function that handles an array of objects from the 'Flight' class.
////////////////////////////////////////
// 'readFlights' reads flight info from file 'flightFile'
//     into array 'flights' of objects of class Flight
// SENTINEL in airline position terminates the info
// returns number of read flights
// exits program if 'flightFile' not opened

int readFlights(Flight flights[], ifstream & flightFile)
{
    string      airline, orig, dest;
    int         nFlights, flightNum, seats;
    float       price;

    // TODO
	
}


I actually had some code but I think it's completely the wrong idea.

This is the class:
class Flight
{
private:
    string  airline;    // airline code
    int     flightNum;  // flight number
    string  orig;       // origin airport code
    string  dest;       // destination airport code
    float   price;      // total price in $s & cents
    int     seats;      // number of seats remaining

public:
    Flight();
    ~Flight();

    // mutator functions

    void set_airline(const string & a);
    void set_flightNum(int n);
    void set_orig(const string & o);
    void set_dest(const string & d);
    void set_price(float p);
    void set_seats(int s);
    void set(const string & a, int n, 
        const string & o, const string & d, float p, int s);

    // accessor functions

    string get_airline() const;
    int get_flightNum() const;
    string get_orig() const;
    string get_dest() const;
    float get_price() const;
    int get_seats() const;
};

////////////////////////////////////////

Flight::Flight()
: airline(""), flightNum(-1), orig(""), dest(""),
  price(0.0), seats(0)
{
}

Flight::~Flight()
{
}

////////////////////////////////////////
// mutator functions

void Flight::set_airline(const string & a)
{
    airline = a;
}

void Flight::set_flightNum(int n)
{
    flightNum = n;
}

void Flight::set_orig(const string & o)
{
    orig = o;
}

void Flight::set_dest(const string & d)
{
    dest = d;
}

void Flight::set_price(float p)
{
    price = p;
}

void Flight::set_seats(int s)
{
    seats = s;
}

void Flight::set(const string & a, int n, 
    const string & o, const string & d, float p, int s)
{
    airline = a;
    flightNum = n;
    orig = o;
    dest = d;
    price = p;
    seats = s;
}

////////////////////////////////////////
// accessor functions

string Flight::get_airline() const
{
    return airline;
}

int Flight::get_flightNum() const
{
    return flightNum;
}

string Flight::get_orig() const
{
    return orig;
}

string Flight::get_dest() const
{
    return dest;
}

float Flight::get_price() const
{
    return price;
}

int Flight::get_seats() const
{
    return seats;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Question about reading in flights from file

#2 jimblumberg   User is offline

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,111
  • Joined: 25-December 09

Re: Question about reading in flights from file

Posted 05 September 2011 - 04:30 PM

In order to read data from a file you must know how the information is laid out in the file. How is the data separated? Until you understand how the information is organized in the file you will not be able to read the file.

For the actual reading of the file you may want to start by reading a few tutorials on file input and output. The following link is a good starting tutorial, and there are several tutorials available in the tutorial section.
Input and output with files. This link should give you an idea of how to start your project.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 skatingrocker17   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 138
  • Joined: 01-September 11

Re: Question about reading in flights from file

Posted 05 September 2011 - 04:40 PM

View Postjimblumberg, on 05 September 2011 - 04:30 PM, said:

In order to read data from a file you must know how the information is laid out in the file. How is the data separated? Until you understand how the information is organized in the file you will not be able to read the file.

Thanks Jim, something like this?
ifstream indata;
nFlights = 0;
indata.open("flights.txt");
while (indata >> airline >> flightNum >> orig >> dest >> price >> seats)
	{
 		nFlights++;
	}

indata.close();



Was This Post Helpful? 0
  • +
  • -

#4 Hadean Fall   User is offline

  • D.I.C Head


Reputation: -5
  • View blog
  • Posts: 241
  • Joined: 05-September 11

Re: Question about reading in flights from file

Posted 05 September 2011 - 04:50 PM

you could always try and look to see if there is a different approach to take, sometimes you just need to step back and look at it and ask if this way is the most efficient, plus it helps to not have a bad teacher and actually know the stuff, just saying
Was This Post Helpful? 0
  • +
  • -

#5 skatingrocker17   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 138
  • Joined: 01-September 11

Re: Question about reading in flights from file

Posted 05 September 2011 - 04:53 PM

View PostHadean Fall, on 05 September 2011 - 04:50 PM, said:

you could always try and look to see if there is a different approach to take, sometimes you just need to step back and look at it and ask if this way is the most efficient, plus it helps to not have a bad teacher and actually know the stuff, just saying

It seems I'm somewhat locked into one way of writing this function because most of the code is already written. Our job is to fill in the blanks and make it work with the rest of the code.

And yes, I agree about the teacher. The program is a little over my head honestly and I'm sure many other students will be struggling with it too. If I can't figure it out from the help on here I'll have to go visit him, which I'm sure I'll have to many times throughout the semester.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,111
  • Joined: 25-December 09

Re: Question about reading in flights from file

Posted 05 September 2011 - 05:30 PM

In the following snippet:

ifstream indata;
nFlights = 0;
indata.open("flights.txt");
while (indata >> airline >> flightNum >> orig >> dest >> price >> seats)
	{
 		nFlights++;
	}


What is the purpose of the parameters in your function:
int readFlights(Flight flights[], ifstream & flightFile)


Why do you think you are passing the array of Flight into this function? Don't you need to fill in the class with the values you read from the file? Shouldn't you do this in the loop? You also should be passing the size of the array into the function so you can insure you don't try to write data into more array elements than you have allocated. Also don't forget to test for the Sentinel value.

Looking at your comments in your first post it looks like you should be opening the file in this function. Therefore you shouldn't need the ifstream& parameter, a string holding the file name would probably be more appropriate.



Jim
Was This Post Helpful? 1
  • +
  • -

#7 skatingrocker17   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 138
  • Joined: 01-September 11

Re: Question about reading in flights from file

Posted 05 September 2011 - 05:45 PM

View Postjimblumberg, on 05 September 2011 - 05:30 PM, said:

Why do you think you are passing the array of Flight into this function? Don't you need to fill in the class with the values you read from the file? Shouldn't you do this in the loop? You also should be passing the size of the array into the function so you can insure you don't try to write data into more array elements than you have allocated. Also don't forget to test for the Sentinel value.

Looking at your comments in your first post it looks like you should be opening the file in this function. Therefore you shouldn't need the ifstream& parameter, a string holding the file name would probably be more appropriate.



Jim


I think I'm passing the array to the function to fill it with the data from the text file.

I don't think I can pass the size of the array it's not yet determined because I haven't yet went through the text file. nFlights should be incremented until the end of the file, then I'll know how many values are stored in the array.

Also, what do you mean by "a string holding the file name"? So I don't need ifstream but keep indata.open("flights.txt");?

Thanks

This post has been edited by skatingrocker17: 05 September 2011 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,111
  • Joined: 25-December 09

Re: Question about reading in flights from file

Posted 05 September 2011 - 05:56 PM

Quote

I don't think I can pass the size of the array it's not yet determined because I haven't yet went through the text file. nFlights should be incremented until the end of the file, then I'll know how many values are stored in the array.


How is flights[] defined in the calling function?

Quote

Also, what do you mean by "a string holding the file name"? So I don't need ifstream but keep indata.open("flights.txt");?


If you are going to open and close the file inside this function then you do not need to pass the ifstream&, you can create the ifstream inside this function. If I was going to open a file in a function I would pass the file name into the function, instead of using a constant inside the function.

Jim
Was This Post Helpful? 1
  • +
  • -

#9 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Question about reading in flights from file

Posted 05 September 2011 - 06:34 PM

It was implied in the skatingrocker17's last topic about this Flight program, that he was given the function to complete.

The array of objects and the file stream are passed to the function. The file should be open, although you can check if you wish.

Using a loop, the data can read from the file stream and inserted into the array. There are different ways of doing this.

A count of the number of records inserted can be returned by the function.


Here you try and store data in variables but you haven't created them.
You have a file stream called indata when you already have the stream flightFile passed to the function.
while (indata >> airline >> flightNum >> orig >> dest >> price >> seats)




You have a member function of the Flight object called set().

How can you fill the array of objects? :-
An array of objects: call its method
Was This Post Helpful? 1
  • +
  • -

#10 skatingrocker17   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 138
  • Joined: 01-September 11

Re: Question about reading in flights from file

Posted 05 September 2011 - 07:18 PM

I'm starting to feel pretty stupid about now.
Thank you #define that example you posted was very helpful. All I need to do is apply that to reading in the data.

If I wanted to use a for loop to store the data into the array I suppose it would go something like
for (i=0; i<nFlights; i++)
but before I do that I would need to know how many flights there are. So could I just make a while loop before that to the end of the file and increment nFlights each time?
Was This Post Helpful? 0
  • +
  • -

#11 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Question about reading in flights from file

Posted 05 September 2011 - 07:23 PM

View Postskatingrocker17, on 06 September 2011 - 05:18 AM, said:

... So could I just make a while loop before that to the end of the file and increment nFlights each time?


Yes, I think a while loop would be suitable.
Was This Post Helpful? 0
  • +
  • -

#12 skatingrocker17   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 138
  • Joined: 01-September 11

Re: Question about reading in flights from file

Posted 05 September 2011 - 07:31 PM

View Post#define, on 05 September 2011 - 07:23 PM, said:

Yes, I think a while loop would be suitable.

So possibly something along the lines of:

while(infile.eof())
     nFlights++;

for (i=0; i<nFlights; i++)
     infile >> airline >> flightNum >> orig >> dest >> price >> seats;

Was This Post Helpful? 0
  • +
  • -

#13 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Question about reading in flights from file

Posted 05 September 2011 - 09:07 PM

You only need one loop. To test your code write a main function that uses the Flight class.

A while loop is similar to a for loop.

int i;

i=0;
while(test)
{
  // do something
  // do something
  i++;
}




int i;

for (i=0; test; i++)
{
  // do something
  // do something
}


Was This Post Helpful? 1
  • +
  • -

#14 skatingrocker17   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 138
  • Joined: 01-September 11

Re: Question about reading in flights from file

Posted 06 September 2011 - 07:20 AM

View Post#define, on 05 September 2011 - 09:07 PM, said:

You only need one loop. To test your code write a main function that uses the Flight class.

A while loop is similar to a for loop.


So instead of having a for loop and a while loop I could get away with one loop for reading all the information in and incrementing nFlights until the end of the file? What would the header look like for the while loop if I wanted to read in all the data until the end of the file??

EDIT:
This is where the function is first called:
const int MAX_FLIGHTS = 10000;
const int MAX_MATCHING_FLIGHTS = MAX_FLIGHTS;
const string SENTINEL = "#";

////////////////////////////////////////
// 'readFlights' reads flight info from file 'flightFile'
//     into array 'flights' of objects of class Flight
// SENTINEL in airline position terminates the info
// returns number of read flights
// exits program if 'flightFile' not opened

int readFlights(Flight flights[], ifstream & flightFile);

This post has been edited by skatingrocker17: 06 September 2011 - 07:34 AM

Was This Post Helpful? 0
  • +
  • -

#15 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Question about reading in flights from file

Posted 06 September 2011 - 09:29 AM

View Postskatingrocker17, on 06 September 2011 - 05:20 PM, said:

So instead of having a for loop and a while loop I could get away with one loop for reading all the information in and incrementing nFlights until the end of the file? What would the header look like for the while loop if I wanted to read in all the data until the end of the file??



Assuming a filestream fstrm say :

ifstream fstrm;


Probably most learners would do something like :

while( !fstrm.eof() )
{
  fstrm >> variable;
  cout << variable << endl;
}



This works upto a point, but there can be problems.
This doesn't handle a failed read, eg. when trying to read a number you get a character.
This doesn't handle a bad read, eg. when the file isn't open, or the OS cannot read the file properly.
This doesn't handle whitespace near the end of the file (not considered here, but handled by fail later).

To handle these problems we could try this
while( !fstrm.eof() && !fstrm.fail() && !fstrm.bad() )
{
  fstrm >> variable;
  cout << variable << endl;
}



The while expression can be simplified to
while( fstrm.good() )
{
  fstrm >> variable;
  cout << variable << endl;
}



There can still be a problem because the stream is read and data processed without checking.
The stream and integrity of the data can be checked like so:-

while( fstrm.good() )
{
  fstrm >> variable;
  if( fstrm.good() )
  {
    cout << variable << endl;
  }
}



The read can work in an if expression and the code will work in the same way.

while( fstrm.good() )
{
  if( fstrm >> variable; )
  {
    cout << variable << endl;
  }
}




If the read can work in an if expression it can also work in a while expression.
while( fstrm >> variable; )
{
  cout << variable << endl;
}


Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3