file processing problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • B. Williams

    #1

    file processing problem

    I wrote a program that would simply output data to the screen, but now I am trying to have the data saved to a file. I don't have a problem creating the file or even outputting data to it when there is no header file to include, but I can't seem to figure out how to output the data to the file I create when the main is in a different. Will someone give me an example or show me what I am doing wrong?



    #include <iostream>

    #include <string>



    #include <fstream>

    using std::ofstream;



    using std::cout;



    class PPG{

    public:

    PPG(char *a, char b, int c)

    { dresscolor =b;

    power = c;

    setname(a);}//end constructor 1



    PPG()

    { setname("Ms. Bellum");

    dresscolor ='p';

    power = 0;

    }//end default constructor



    char * getname()const {return name;}



    void setname(char *a){

    int l =strlen(a);

    name = new char[l+1];

    strcpy(name,a);

    name[l] = '\0';

    }//end setname



    int getpower() const{return power;}

    void setpower(int z){power = z;}

    char getdresscolor() const{return dresscolor;}

    void setdresscolor(c har v){dresscolor=v ;}



    void print() const

    { cout << name << " likes to wear ";

    switch (dresscolor){

    case 'g': case 'G':

    cout <<"green dresses. She uses her "; break;

    case 'b':case 'B':

    cout <<"blue dresses. She uses her ";break;

    case 'p': case 'P':

    cout <<"pink dresses. She uses her ";

    }//end switch

    if (power == 1)

    cout << "ice breath to defeat her enemies.\n";

    else if (power ==2)

    cout << "ability to talk to squirrels to confuse evil villians.\n";

    else if (power ==3)

    cout <<"bad attitude to stop evil doers.\n";

    else

    cout <<"girl power to rule the world.\n";

    }//end print



    bool operator==(PPG &ppg)

    { return (strcmp(name, ppg.name)==0); }



    private:

    char * name;

    char dresscolor; //g-reen, b-lue, p-pink

    int power; //1-ice breath, 2- squirrel speak, 3-bad attitude

    }; //end class



    #include "ppg.h"

    #include <iostream>

    #include <fstream>

    using std::ofstream;





    int main()

    {



    ofstream PPG("girls.txt" , ios::out);

    PPG girl1("Bubbles" , 'b', 2);

    girl1.print();

    PPG badgirl("Prince ss",'g', 4);

    badgirl.print() ;





    return 0;

    }//end main

  • Gianni Mariani

    #2
    Re: file processing problem

    B. Williams wrote:
    I wrote a program that would simply output data to the screen, but now I
    am trying to have the data saved to a file. I don't have a problem
    creating the file or even outputting data to it when there is no header
    file to include, but I can't seem to figure out how to output the data
    to the file I create when the main is in a different. Will someone give
    me an example or show me what I am doing wrong?
    You need to pass a reference to the file you want to print to.

    If you're using a std::ostream, then have your print function take a
    "std::ostre am &" (reference) as a parameter. You can have it as a
    parameter to the class constructor.


    Here is an example:

    void PrintToThisFile ( std::ostream & ofile )
    {
    ofile << "Hello world of other file\n";
    }

    int main()
    {
    PrintThisToFile ( std::cout );
    std::ofstream otherfile( "junk" );
    PrintThisToFile ( otherfile );
    }
    // caution - this is a brain dump - expect errors

    ...

    Comment

    • Adrian

      #3
      Re: file processing problem

      "B. Williams" <willdrama@hotm ail.comwrote in message
      news:V291h.1645 4$mX5.5353@news fe23.lga...
      I wrote a program that would simply output data to the screen, but now I
      am trying to have the data saved to a
      file. I don't have a problem creating the file or even outputting data to
      it when there is no header file to include, but I can't seem to figure out
      how to output
      the data to the file I create when the main is in a different. Will
      someone give me an example or show me what I am doing wrong?
      [code snip]

      I have improved on your program. Hopefully with no mistakes. I made a few
      changes.

      1. To use enumerated types for the colour and power as this makes the
      program more readable, improves type checking
      2. Using std::string to remove all the problems with char *'s This allows
      easy copying of the class with default constructor, and not having to worry
      about bounds checking etc
      3. Add a friend function to output your class
      4. Moved the print function to private (as people done need it now)
      5. Split code into 3 seperate files
      6. Stopped multiple includes of header file

      (Depending on platform a couple of example compile lines)
      g++ -Wall -pedantic -ansi -o ppg ppg.cpp main.cpp
      bcc32 -o ppg main.cpp ppg.cpp

      Code to follow:-
      // File
      ppg.h -------------------------------------------------------------------------
      #ifndef PPG_H //stop multiple includes of header
      #define PPG_H

      #include <ostream>
      #include <string>

      class PPG; //Forward definition class for friend function prototype
      std::ostream &operator<<(std ::ostream &os, const PPG &ppg);
      //Friend prototype (not sure if you need the two about or if I am being
      pedantic

      //removed inline functions for clarity and moved to ppg.cpp
      class PPG
      {
      public:
      typedef enum {NoColor_e, Green_e, Blue_e, Pink_e} Color_t;
      typedef enum {NoPower_e, Ice_Breath_e, Squirrel_Speak_ e, Bad_Attitude_e}
      Power_t;

      PPG(const std::string &a, Color_t b, Power_t c);
      PPG();
      // Using std::string stops the need for a copy constructor now as all
      types can be copied
      // using the default copy constructor - no worries about memory freeing
      etc

      const std::string &getname() const;
      void setname(const std::string &a);
      Power_t getpower() const;
      void setpower(Power_ t z);
      Color_t getdresscolor() const;
      void setdresscolor(C olor_t v);
      bool operator==(cons t PPG &ppg);

      //definition of friend function to output class to a stream
      friend std::ostream &operator<<(std ::ostream &os, const PPG &ppg);

      private:
      std::ostream &print(std::ost ream &os) const;
      std::string name;
      Color_t dresscolor; // No need for descriptions now //g-reen, b-lue,
      p-pink
      Power_t power; // No need for descriptions now //1-ice breath, 2-
      squirrel speak, 3-bad attitude
      }; //end class


      #endif
      // End file
      ppg.h -------------------------------------------------------------------------


      //File
      ppg.cpp -------------------------------------------------------------------------
      #include <ostream>
      #include "ppg.h"

      //Changed to use std::string so no worries about length etc
      PPG::PPG(const std::string &a, Color_t b, Power_t c)
      :name(a), dresscolor(b), power(c) // Here using constructor initizalizer
      lists
      {
      }//end constructor 1

      PPG::PPG()
      :name("Ms. Bellum"), dresscolor(Pink _e), power(NoPower_e ) // same here
      {
      }//end default constructor

      //changed to use std::string
      const std::string &PPG::getnam e() const
      {
      return name;
      }

      //changed to use std::string
      void PPG::setname(co nst std::string &a)
      {
      name=a;
      }//end setname

      PPG::Power_t PPG::getpower() const
      {
      return power;
      }

      void PPG::setpower(P ower_t z)
      {
      power = z;
      }

      PPG::Color_t PPG::getdressco lor() const
      {
      return dresscolor;
      }

      void PPG::setdressco lor(Color_t v)
      {
      dresscolor=v;
      }

      //changed to a private function and now uses ostream to output
      std::ostream &PPG::print(std ::ostream &os) const
      {
      os << name << " likes to wear ";
      switch(dresscol or)
      {
      case Green_e:
      os <<"green dresses. She uses her ";
      break;

      case Blue_e:
      os <<"blue dresses. She uses her ";
      break;

      case Pink_e:
      os <<"pink dresses. She uses her ";
      break;

      default:
      // Always worth having this even if it does nothing. Can stop some nasty
      crashes
      os << "[OOPS not a known colour]";
      break;
      }//end switch

      //changed to switch statement for style consitency
      switch(power)
      {
      case Ice_Breath_e:
      os << "ice breath to defeat her enemies.\n";
      break;

      case Squirrel_Speak_ e:
      os << "ability to talk to squirrels to confuse evil villians.\n";
      break;

      case Bad_Attitude_e:
      os <<"bad attitude to stop evil doers.\n";
      break;

      default:
      os <<"girl power to rule the world.\n";
      }
      return os;
      }//end print

      //changed to use std::string comparison
      bool PPG::operator== (const PPG &ppg)
      {
      return name==ppg.name;
      }


      std::ostream &operator<<(std ::ostream &os, const PPG &ppg)
      {
      return ppg.print(os);
      }
      // end file
      ppg.cpp -------------------------------------------------------------------------

      // File
      main.cpp -------------------------------------------------------------------------

      #include "ppg.h"
      #include <iostream>
      #include <fstream>

      using std::ofstream;
      using std::cout;


      //now uses friend function and print() to out to either cout or a file
      stream
      int main()
      {
      ofstream text_file("girl s.txt");

      //Now use enumerated types
      PPG girl1("Bubbles" , PPG::Pink_e, PPG::Squirrel_S peak_e);

      //PPG girl2("Bubbles2 ", PPG::Squirrel_S peak_e, PPG::Pink_e);
      //This now uses c++ type checking and will not compile where as
      //PPG girl3("Bubbles3 ", 2, 'g'); could compile without errors
      //Plus the added bonus of not having to go back and find out what 2 means

      //Now we can easily output using either a file or the screen
      cout << girl1;
      text_file << girl1;

      PPG badgirl("Prince ss",PPG::Green_ e, PPG::NoPower_e) ;

      cout << badgirl;
      text_file << badgirl;

      return 0;

      }//end main

      // End file
      main.cpp -------------------------------------------------------------------------

      Comment

      • B. Williams

        #4
        Re: file processing problem


        "Gianni Mariani" <gi3nospam@mari ani.wswrote in message
        news:45452ba0$0 $7991$5a62ac22@ per-qv1-newsreader-01.iinet.net.au ...
        B. Williams wrote:
        >I wrote a program that would simply output data to the screen, but now I
        >am trying to have the data saved to a file. I don't have a problem
        >creating the file or even outputting data to it when there is no header
        >file to include, but I can't seem to figure out how to output the data to
        >the file I create when the main is in a different. Will someone give me
        >an example or show me what I am doing wrong?
        >
        You need to pass a reference to the file you want to print to.
        >
        If you're using a std::ostream, then have your print function take a
        "std::ostre am &" (reference) as a parameter. You can have it as a
        parameter to the class constructor.
        >
        >
        Here is an example:
        >
        void PrintToThisFile ( std::ostream & ofile )
        {
        ofile << "Hello world of other file\n";
        }
        >
        int main()
        {
        PrintThisToFile ( std::cout );
        std::ofstream otherfile( "junk" );
        PrintThisToFile ( otherfile );
        }
        // caution - this is a brain dump - expect errors
        >
        ..
        This is exactly what I was looking for. I'll try to adapt my code to it.


        Comment

        • B. Williams

          #5
          Re: file processing problem


          "B. Williams" <willdrama@hotm ail.comwrote in message
          news:fMb1h.5282 $2A4.201@newsfe 24.lga...
          >
          "Gianni Mariani" <gi3nospam@mari ani.wswrote in message
          news:45452ba0$0 $7991$5a62ac22@ per-qv1-newsreader-01.iinet.net.au ...
          >B. Williams wrote:
          >>I wrote a program that would simply output data to the screen, but now I
          >>am trying to have the data saved to a file. I don't have a problem
          >>creating the file or even outputting data to it when there is no header
          >>file to include, but I can't seem to figure out how to output the data
          >>to the file I create when the main is in a different. Will someone give
          >>me an example or show me what I am doing wrong?
          >>
          >You need to pass a reference to the file you want to print to.
          >>
          >If you're using a std::ostream, then have your print function take a
          >"std::ostrea m &" (reference) as a parameter. You can have it as a
          >parameter to the class constructor.
          >>
          >>
          >Here is an example:
          >>
          >void PrintToThisFile ( std::ostream & ofile )
          >{
          >ofile << "Hello world of other file\n";
          >}
          >>
          >int main()
          >{
          > PrintThisToFile ( std::cout );
          > std::ofstream otherfile( "junk" );
          > PrintThisToFile ( otherfile );
          >}
          >// caution - this is a brain dump - expect errors
          >>
          >..
          This is exactly what I was looking for. I'll try to adapt my code to it.
          I just wanted to thank everyone for their assistance. This is my code and
          it works.

          #include <iostream>

          using std::cerr;

          using std::endl;

          using std::ios;

          using std::cout;

          #include <fstream>

          using std::ofstream;



          #include <cstdlib>

          using std::exit;



          class PPG{

          public:

          PPG(char *a, char b, int c)

          { dresscolor =b;

          power = c;

          setname(a);}//end constructor 1



          PPG()

          { setname("Ms. Bellum");

          dresscolor ='p';

          power = 0;

          }//end default constructor



          char * getname()const {return name;}



          void setname(char *a){

          int l =strlen(a);

          name = new char[l+1];

          strcpy(name,a);

          name[l] = '\0';

          }//end setname





          int getpower() const{return power;}

          void setpower(int z){power = z;}

          char getdresscolor() const{return dresscolor;}

          void setdresscolor(c har v){dresscolor=v ;}



          void print(std::ostr eam & outPPGFile) const

          { outPPGFile << name << " likes to wear ";

          switch (dresscolor){

          case 'g': case 'G':

          outPPGFile <<"green dresses. She uses her "; break;

          case 'b':case 'B':

          outPPGFile <<"blue dresses. She uses her ";break;

          case 'p': case 'P':

          outPPGFile <<"pink dresses. She uses her ";

          }//end switch

          if (power == 1)

          outPPGFile << "ice breath to defeat her enemies.\n";

          else if (power ==2)

          outPPGFile << "ability to talk to squirrels to confuse evil
          villians.\n";

          else if (power ==3)

          outPPGFile <<"bad attitude to stop evil doers.\n";

          else

          outPPGFile <<"girl power to rule the world.\n";

          }//end print



          bool operator==(PPG &ppg)

          { return (strcmp(name, ppg.name)==0); }



          private:

          char * name;

          char dresscolor; //g-reen, b-lue, p-pink

          int power; //1-ice breath, 2- squirrel speak, 3-bad attitude

          }; //end class



          int main()

          {



          ofstream outPPGFile("gir ls.txt");



          if ( !outPPGFile )

          {

          cerr << "File could not be opened" << endl;

          exit( 1 );

          } // end if





          PPG girl;

          girl.print(outP PGFile);

          PPG girl1("Bubbles" , 'b', 2);

          girl1.print(out PPGFile);

          PPG badgirl("Prince ss",'g', 4);

          badgirl.print(o utPPGFile);



          return 0;



          }//end main






          Comment

          • B. Williams

            #6
            Re: file processing problem


            "Adrian" <nntp@bluedream er.comwrote in message
            news:5Y2dnW1Lwd ZtqNjYnZ2dnUVZ_ qCdnZ2d@comcast .com...
            >"B. Williams" <willdrama@hotm ail.comwrote in message
            >news:V291h.164 54$mX5.5353@new sfe23.lga...
            >I wrote a program that would simply output data to the screen, but now I
            >am trying to have the data saved to a
            >file. I don't have a problem creating the file or even outputting data to
            >it when there is no header file to include, but I can't seem to figure
            >out how to output
            >the data to the file I create when the main is in a different. Will
            >someone give me an example or show me what I am doing wrong?
            [code snip]
            >
            I have improved on your program. Hopefully with no mistakes. I made a few
            changes.
            >
            1. To use enumerated types for the colour and power as this makes the
            program more readable, improves type checking
            2. Using std::string to remove all the problems with char *'s This allows
            easy copying of the class with default constructor, and not having to
            worry about bounds checking etc
            3. Add a friend function to output your class
            4. Moved the print function to private (as people done need it now)
            5. Split code into 3 seperate files
            6. Stopped multiple includes of header file
            >
            (Depending on platform a couple of example compile lines)
            g++ -Wall -pedantic -ansi -o ppg ppg.cpp main.cpp
            bcc32 -o ppg main.cpp ppg.cpp
            >
            Code to follow:-
            // File
            ppg.h -------------------------------------------------------------------------
            #ifndef PPG_H //stop multiple includes of header
            #define PPG_H
            >
            #include <ostream>
            #include <string>
            >
            class PPG; //Forward definition class for friend function prototype
            std::ostream &operator<<(std ::ostream &os, const PPG &ppg);
            //Friend prototype (not sure if you need the two about or if I am being
            pedantic
            >
            //removed inline functions for clarity and moved to ppg.cpp
            class PPG
            {
            public:
            typedef enum {NoColor_e, Green_e, Blue_e, Pink_e} Color_t;
            typedef enum {NoPower_e, Ice_Breath_e, Squirrel_Speak_ e, Bad_Attitude_e}
            Power_t;
            >
            PPG(const std::string &a, Color_t b, Power_t c);
            PPG();
            // Using std::string stops the need for a copy constructor now as all
            types can be copied
            // using the default copy constructor - no worries about memory freeing
            etc
            >
            const std::string &getname() const;
            void setname(const std::string &a);
            Power_t getpower() const;
            void setpower(Power_ t z);
            Color_t getdresscolor() const;
            void setdresscolor(C olor_t v);
            bool operator==(cons t PPG &ppg);
            >
            //definition of friend function to output class to a stream
            friend std::ostream &operator<<(std ::ostream &os, const PPG &ppg);
            >
            private:
            std::ostream &print(std::ost ream &os) const;
            std::string name;
            Color_t dresscolor; // No need for descriptions now //g-reen, b-lue,
            p-pink
            Power_t power; // No need for descriptions now //1-ice breath, 2-
            squirrel speak, 3-bad attitude
            }; //end class
            >
            >
            #endif
            // End file
            ppg.h -------------------------------------------------------------------------
            >
            >
            //File
            ppg.cpp -------------------------------------------------------------------------
            #include <ostream>
            #include "ppg.h"
            >
            //Changed to use std::string so no worries about length etc
            PPG::PPG(const std::string &a, Color_t b, Power_t c)
            :name(a), dresscolor(b), power(c) // Here using constructor initizalizer
            lists
            {
            }//end constructor 1
            >
            PPG::PPG()
            :name("Ms. Bellum"), dresscolor(Pink _e), power(NoPower_e ) // same here
            {
            }//end default constructor
            >
            //changed to use std::string
            const std::string &PPG::getnam e() const
            {
            return name;
            }
            >
            //changed to use std::string
            void PPG::setname(co nst std::string &a)
            {
            name=a;
            }//end setname
            >
            PPG::Power_t PPG::getpower() const
            {
            return power;
            }
            >
            void PPG::setpower(P ower_t z)
            {
            power = z;
            }
            >
            PPG::Color_t PPG::getdressco lor() const
            {
            return dresscolor;
            }
            >
            void PPG::setdressco lor(Color_t v)
            {
            dresscolor=v;
            }
            >
            //changed to a private function and now uses ostream to output
            std::ostream &PPG::print(std ::ostream &os) const
            {
            os << name << " likes to wear ";
            switch(dresscol or)
            {
            case Green_e:
            os <<"green dresses. She uses her ";
            break;
            >
            case Blue_e:
            os <<"blue dresses. She uses her ";
            break;
            >
            case Pink_e:
            os <<"pink dresses. She uses her ";
            break;
            >
            default:
            // Always worth having this even if it does nothing. Can stop some nasty
            crashes
            os << "[OOPS not a known colour]";
            break;
            }//end switch
            >
            //changed to switch statement for style consitency
            switch(power)
            {
            case Ice_Breath_e:
            os << "ice breath to defeat her enemies.\n";
            break;
            >
            case Squirrel_Speak_ e:
            os << "ability to talk to squirrels to confuse evil villians.\n";
            break;
            >
            case Bad_Attitude_e:
            os <<"bad attitude to stop evil doers.\n";
            break;
            >
            default:
            os <<"girl power to rule the world.\n";
            }
            return os;
            }//end print
            >
            //changed to use std::string comparison
            bool PPG::operator== (const PPG &ppg)
            {
            return name==ppg.name;
            }
            >
            >
            std::ostream &operator<<(std ::ostream &os, const PPG &ppg)
            {
            return ppg.print(os);
            }
            // end file
            ppg.cpp -------------------------------------------------------------------------
            >
            // File
            main.cpp -------------------------------------------------------------------------
            >
            #include "ppg.h"
            #include <iostream>
            #include <fstream>
            >
            using std::ofstream;
            using std::cout;
            >
            >
            //now uses friend function and print() to out to either cout or a file
            stream
            int main()
            {
            ofstream text_file("girl s.txt");
            >
            //Now use enumerated types
            PPG girl1("Bubbles" , PPG::Pink_e, PPG::Squirrel_S peak_e);
            >
            //PPG girl2("Bubbles2 ", PPG::Squirrel_S peak_e, PPG::Pink_e);
            //This now uses c++ type checking and will not compile where as
            //PPG girl3("Bubbles3 ", 2, 'g'); could compile without errors
            //Plus the added bonus of not having to go back and find out what 2 means
            >
            //Now we can easily output using either a file or the screen
            cout << girl1;
            text_file << girl1;
            >
            PPG badgirl("Prince ss",PPG::Green_ e, PPG::NoPower_e) ;
            >
            cout << badgirl;
            text_file << badgirl;
            >
            return 0;
            >
            }//end main
            >
            // End file
            main.cpp -------------------------------------------------------------------------
            >
            I really like your programming, but it is a bit over my head.


            Comment

            • Daniel T.

              #7
              Re: file processing problem

              "B. Williams" <willdrama@hotm ail.comwrote:
              I wrote a program that would simply output data to the screen, but now I am
              trying to have the data saved to a file. I don't have a problem creating the
              file or even outputting data to it when there is no header file to include,
              but I can't seem to figure out how to output the data to the file I create
              when the main is in a different. Will someone give me an example or show me
              what I am doing wrong?
              There are several problems with the code you posted. But to answer your
              explicit question, you do it the same way. Keep in mind, the
              pre-processor simply pastes the header file into the cpp file at the
              location of the #include statement. Now to the code review:
              #include <iostream>
              #include <string>
              #include <fstream>
              >
              using std::ofstream;
              using std::cout;
              Don't put 'using' statements in header files.
              class PPG
              {
              >
              public:
              PPG(char *a, char b, int c)
              {
              dresscolor = b;
              power = c;
              setname(a);
              }//end constructor 1
              >
              PPG()
              {
              setname("Ms. Bellum");
              dresscolor ='p';
              power = 0;
              }//end default constructor
              Learn about initializer lists and the use of the 'const' keyword. Use
              meaningful variable names as parameters.
              char* getname()const
              {
              return name;
              }
              >
              void setname(char *a)
              {
              int l =strlen(a);
              name = new char[l+1];
              strcpy(name,a);
              name[l] = '\0';
              }//end setname
              Note, if the object already has a name, calling setname will leak
              memory. If an object is copied, then two objects will point to the same
              name object. Memory will also leak when an object is destroyed.
              int getpower() const
              {
              return power;
              }
              >
              void setpower(int z)
              {
              power = z;
              }
              >
              char getdresscolor() const
              {
              return dresscolor;
              }
              >
              void setdresscolor(c har v)
              {
              dresscolor=v;
              }
              Only certain values are meaningful for the dress color, but you don't
              check to make sure the value entered will work.
              void print() const
              {
              cout << name << " likes to wear ";
              Your 'print()' function can't send its data to a file, it will only
              print to cout.
              switch (dresscolor) {
              case 'g':
              case 'G':
              cout <<"green dresses. She uses her ";
              break;
              case 'b':
              case 'B':
              cout <<"blue dresses. She uses her ";
              break;
              case 'p':
              case 'P':
              cout <<"pink dresses. She uses her ";
              }//end switch
              Whenever every branch in a switch/case statement has the same thing, you
              should factor out the likenesses. Remember "DRY", Don't Repeat Yourself.
              if (power == 1)
              cout << "ice breath to defeat her enemies.\n";
              else if (power == 2)
              cout << "ability to talk to squirrels to confuse evil villians.\n";
              else if (power == 3)
              cout <<"bad attitude to stop evil doers.\n";
              else
              cout <<"girl power to rule the world.\n";
              }//end print
              >
              bool operator==(PPG &ppg)
              {
              return (strcmp(name, ppg.name)==0);
              }
              If two different PPGs happen to have the same name, should operator==
              still return true?
              private:
              char* name;
              char dresscolor; //g-reen, b-lue, p-pink
              int power; //1-ice breath, 2- squirrel speak, 3-bad attitude
              }; //end class
              >
              #include "ppg.h"
              #include <iostream>
              #include <fstream>
              >
              using std::ofstream;
              >
              int main()
              {
              ofstream PPG("girls.txt" , ios::out);
              Don't give an object the same name as a class, this will hide the class.
              PPG girl1("Bubbles" , 'b', 2);
              girl1.print();
              PPG badgirl("Prince ss",'g', 4);
              badgirl.print() ;
              return 0;
              }//end main
              --
              To send me email, put "sheltie" in the subject.

              Comment

              • Daniel T.

                #8
                Re: file processing problem

                "Adrian" <nntp@bluedream er.comwrote:
                Code to follow:-
                Good job Adrian, just a few notes to what you did.
                // File
                ppg.h
                -------------------------------------------------------------------------
                #ifndef PPG_H //stop multiple includes of header
                #define PPG_H
                >
                #include <ostream>
                That's supposed to be:
                #include <iostream>
                or
                #include <iosfwd>
                #include <string>
                >
                class PPG; //Forward definition class for friend function prototype
                std::ostream &operator<<(std ::ostream &os, const PPG &ppg);
                //Friend prototype (not sure if you need the two about or if I am being
                pedantic
                No need to forward declare the class, nor is there need to declare the
                function here.
                //removed inline functions for clarity and moved to ppg.cpp
                class PPG
                {
                public:
                typedef enum {NoColor_e, Green_e, Blue_e, Pink_e} Color_t;
                typedef enum {NoPower_e, Ice_Breath_e, Squirrel_Speak_ e, Bad_Attitude_e}
                Power_t;
                No need for a typedef. Just, for example:

                enum Color_t { noColor, green, blue, pink };
                PPG(const std::string &a, Color_t b, Power_t c);
                PPG();
                // Using std::string stops the need for a copy constructor now as all
                types can be copied
                // using the default copy constructor - no worries about memory freeing
                etc
                >
                const std::string &getname() const;
                void setname(const std::string &a);
                Power_t getpower() const;
                void setpower(Power_ t z);
                Color_t getdresscolor() const;
                void setdresscolor(C olor_t v);
                bool operator==(cons t PPG &ppg);
                >
                //definition of friend function to output class to a stream
                friend std::ostream &operator<<(std ::ostream &os, const PPG &ppg);
                >
                private:
                std::ostream &print(std::ost ream &os) const;
                std::string name;
                Color_t dresscolor; // No need for descriptions now //g-reen, b-lue,
                p-pink
                Power_t power; // No need for descriptions now //1-ice breath, 2-
                squirrel speak, 3-bad attitude
                }; //end class
                >
                >
                #endif
                // End file
                ppg.h
                -------------------------------------------------------------------------
                >
                >
                //File
                ppg.cpp
                -------------------------------------------------------------------------
                #include <ostream>
                #include "ppg.h"
                It's generally better to put the class' include first.
                //Changed to use std::string so no worries about length etc
                PPG::PPG(const std::string &a, Color_t b, Power_t c)
                :name(a), dresscolor(b), power(c) // Here using constructor initizalizer
                lists
                {
                }//end constructor 1
                >
                PPG::PPG()
                :name("Ms. Bellum"), dresscolor(Pink _e), power(NoPower_e ) // same here
                {
                }//end default constructor
                >
                //changed to use std::string
                const std::string &PPG::getnam e() const
                {
                return name;
                }
                >
                //changed to use std::string
                void PPG::setname(co nst std::string &a)
                {
                name=a;
                }//end setname
                >
                PPG::Power_t PPG::getpower() const
                {
                return power;
                }
                >
                void PPG::setpower(P ower_t z)
                {
                power = z;
                }
                >
                PPG::Color_t PPG::getdressco lor() const
                {
                return dresscolor;
                }
                >
                void PPG::setdressco lor(Color_t v)
                {
                dresscolor=v;
                }
                >
                //changed to a private function and now uses ostream to output
                std::ostream &PPG::print(std ::ostream &os) const
                {
                os << name << " likes to wear ";
                switch(dresscol or)
                {
                case Green_e:
                os <<"green dresses. She uses her ";
                break;
                >
                case Blue_e:
                os <<"blue dresses. She uses her ";
                break;
                >
                case Pink_e:
                os <<"pink dresses. She uses her ";
                break;
                >
                default:
                // Always worth having this even if it does nothing. Can stop some nasty
                crashes
                os << "[OOPS not a known colour]";
                break;
                }//end switch
                It would probably be better to make it so the class can't possibly have
                an unknown dress color. That would mean changing the
                "setdresscolor( Color_t)" function.
                //changed to switch statement for style consitency
                switch(power)
                {
                case Ice_Breath_e:
                os << "ice breath to defeat her enemies.\n";
                break;
                >
                case Squirrel_Speak_ e:
                os << "ability to talk to squirrels to confuse evil villians.\n";
                break;
                >
                case Bad_Attitude_e:
                os <<"bad attitude to stop evil doers.\n";
                break;
                >
                default:
                os <<"girl power to rule the world.\n";
                }
                return os;
                }//end print
                >
                //changed to use std::string comparison
                bool PPG::operator== (const PPG &ppg)
                {
                return name==ppg.name;
                }
                >
                >
                std::ostream &operator<<(std ::ostream &os, const PPG &ppg)
                {
                return ppg.print(os);
                }
                // end file
                ppg.cpp
                -------------------------------------------------------------------------
                >
                // File
                main.cpp
                -------------------------------------------------------------------------
                >
                #include "ppg.h"
                #include <iostream>
                #include <fstream>
                >
                using std::ofstream;
                using std::cout;
                >
                >
                //now uses friend function and print() to out to either cout or a file
                stream
                int main()
                {
                ofstream text_file("girl s.txt");
                >
                //Now use enumerated types
                PPG girl1("Bubbles" , PPG::Pink_e, PPG::Squirrel_S peak_e);
                >
                //PPG girl2("Bubbles2 ", PPG::Squirrel_S peak_e, PPG::Pink_e);
                //This now uses c++ type checking and will not compile where as
                //PPG girl3("Bubbles3 ", 2, 'g'); could compile without errors
                //Plus the added bonus of not having to go back and find out what 2 means
                >
                //Now we can easily output using either a file or the screen
                cout << girl1;
                text_file << girl1;
                >
                PPG badgirl("Prince ss",PPG::Green_ e, PPG::NoPower_e) ;
                >
                cout << badgirl;
                text_file << badgirl;
                >
                return 0;
                >
                }//end main
                >
                // End file
                main.cpp
                -------------------------------------------------------------------------
                --
                To send me email, put "sheltie" in the subject.

                Comment

                Working...