Topic: Can anyone fix this?


Author: joneskei@cps.msu.edu (Keith John Jones)
Date: 23 Jul 1994 06:10:44 GMT
Raw View
Can anyone fix this?  It will not compile with g++ on unix because teh
iomanip.h is being affected by the output to a file.  It is only two
functions that will be compiled with the professors object code.

#include<iostream.h>

#include<iomanip.h>

#include<fstream.h>

#include<stdlib.h>



void check_files( fstream&, fstream&, int& );

void display_report( fstream&, fstream& );



void check_files( fstream& InStream, fstream& OutStream, int& Error )

{

  Error = 0;



  if (InStream.fail())

  {

     cerr << "\n\n*** Error in Opening Input File!\n";

     Error = 1;



     if (OutStream.fail())

     {

          cerr << "\n\n*** Error in Opening the Output File!\n";

          Error = 3;

     }



  }

  else if (OutStream.fail())

  {

     cerr << "\n\n*** Error in Opening the Output File!\n";

     Error = 2;



     if (InStream.fail())

     {

          cerr << "\n\n***Error in Opening Input File!\n";

          Error = 3;

     }

  }



  if (Error == 0)

  {

     cerr << "\n\nBoth Input and Output files successfully opened!\n";

  }

}



void display_report( fstream& InStream, fstream& OutStream)

{

  int

     PlayNum,

     Bats,

     Hits,

     Singles,

     Doubles,

     Triples,

     HomeRuns,

     Scored,

     RBI,

     TotalBases,

     TotHits,

     TotBats,

     TotTotalBases,

     Count = 0;



  double

     BatAvg,

     SlugPercent,

     TeamSlugPer,

     TeamBatAvg,

     TotBatAvg = 0.0,

     TotSlugPer = 0.0;



  OutStream << "\n\n\t\tTeam Statistics\n"

            << "\t\t~~~~~~~~~~~~~~~~\n\n"

            << "Player  At                                     Runs
       Batting  Slugging\n"

            << "Number  Bat  Hits  Doubles  Triples  HomeRuns  Scored
R.B.I.  Average  Percentage\n"

            <<
"==================================================================================\n";



  InStream >> PlayNum;



  while(!InStream.eof())

  {

     Count++;



     InStream >> Bats;

     InStream >> Hits;

     InStream >> Doubles;

     InStream >> Triples;

     InStream >> HomeRuns;

     InStream >> Scored;

     InStream >> RBI;



     if (Bats < 0 || Hits < 0 || Doubles < 0 || Triples < 0 ||
HomeRuns < 0

        || Scored < 0 || RBI < 0)

     {

          cerr << "\n\n **** ERROR:  Bad Statistics in Input File, "

               << "Output to File with Player Number: " << PlayNum

               << "is invalid because of negative values!\n";

     }



     Singles = Hits - Doubles - Triples - HomeRuns;



     TotalBases = Singles + (2*Doubles) + (3*Triples) + (4*HomeRuns);



     if (Bats > 0)

     {

          BatAvg = Hits / Bats;

          SlugPercent = TotalBases / Bats;

     }

     else

     {

          SlugPercent = 0;

          BatAvg = 0;

     }



     TotBats += Bats;

     TotHits += Hits;

     TotTotalBases += TotalBases;



     OutStream << setiosflags(ios::showpoint | ios::fixed) <<
setprecision(0)

               << setw(6) << PlayNum << setw(5) << Bats << setw(6) <<
Hits

               << setw(9) << Doubles << setw(9) << Triples

               << setw(10) << HomeRuns << setw(8) << Scored << setw(5)
<< RBI

               << setprecision(3) << setw(9) << BatAvg

               << setw(12) << SlugPercent

               << "\n";



     InStream >> PlayNum;

  }



  if (Count != 0)

  {

     if (TotBats > 0 )

     {

          TeamSlugPer = TotTotalBases / TotBats;

          TeamBatAvg = TotHits / TotBats;

     }

     else

     {

          TeamSlugPer = 0;

          TeamBatAvg = 0;

     }





  OutStream <<
"\n\n====================================================="

            << "\n\n The Team Batting Average is:       " << setw(9)
<< TeamBatAvg

            << "\n The Team Slugging Percentage is:   " << setw(9) <<
TeamSlugPer

            <<
"\n\n=====================================================\n\n";

  }



  if (Count == 0)

  {

       system( "clear" );



       cerr << "\n\n**** ERROR: The input file does not contain any "

               << "records!\n\n";

  }





--

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=
     _/ _/ _/_/_/ _/_/_/ _/_/_/ _/  _/       vvv
    _/_/  _/_/     _/     _/   _/_/_/       (. .)
   _/ _/ _/_/_/ _/_/_/   _/   _/  _/    ooo0 (_) 0ooo
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Keith J. Jones: joneskei@cps.msu.edu  Computer Engineering