Re: Q regarding eof() issue.

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 12/15/03


Date: Tue, 16 Dec 2003 02:15:58 +1100


"Val" <valmont_gaming@hotmail.com> wrote in message news:3FDDCA1F.7050001@hotmail.com...
| Oh, here is your message. I forgot I could use the Mozilla ng reader as well.
| Ok, to busines now.

[ snip - once again: please *DO NOT TOP POST* ! ]

Place your reply below the text you are responding to.
------------------------------------------------------

I have refactored your code, to a very simple model.

There are many more things you can do to improve it.

For example, you could incorporate the functions into
your class, and refactor further, by providing a
constructor and even overloaded stream operators
to read and write out the data.

This sounds like it might be a bit much for you at
the moment, however, you can in the mean time study
the following code, and ask any questions about the
parts you are not sure about.

# include <iostream>
# include <fstream>
# include <ostream>
# include <string>
# include <cstddef>

struct Customers
 {
  int Num;
  char Pkg;
  float Hours;
  std::string Bill;
 };

bool GetCust( const std::string& filename, Customers* P )
 {
  std::ifstream InFile( filename.c_str() );

  if( !InFile )
      return false;

  // You could use a for loop to, if you wish.
  std::size_t Idx( 0 );
  while( InFile >> P[ Idx ].Num >> P[ Idx ].Pkg
>> P[ Idx ].Hours >> P[ Idx ].Bill )
   {
    ++Idx;
   }

  if( !InFile.eof() )
      return false;

  return true;
 }

void DisplayCustomers( const Customers* P, const std::size_t Size )
 {
  for( std::size_t Idx = 0; Idx < Size; ++Idx )
  {
   std::cout << P[ Idx ].Num << " " << P[ Idx ].Pkg << " "
             << P[ Idx ].Hours << " " << P[ Idx ].Bill << std::endl;
  }
 }

int main()
 {
  Customers People[ 3 ];

  if( !GetCust( "CustomerFile.txt", People ) )
      std::cout << "Error: Could not read file properly. "
                << std::endl;
  else
      DisplayCustomers( People, sizeof People / sizeof* People );

  return 0;
 }

Anyway, I have to catch up on some sleep - it's boiling, and
I gotta work tomorrow :-).

Cheers.
Chris Val