"B. Williams" <wi*******@hotmail.comwrote in message
news:V2******************@newsfe23.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(Color_t v);
bool operator==(const 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::ostream &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::getname() const
{
return name;
}
//changed to use std::string
void PPG::setname(const std::string &a)
{
name=a;
}//end setname
PPG::Power_t PPG::getpower() const
{
return power;
}
void PPG::setpower(Power_t z)
{
power = z;
}
PPG::Color_t PPG::getdresscolor() const
{
return dresscolor;
}
void PPG::setdresscolor(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(dresscolor)
{
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("girls.txt");
//Now use enumerated types
PPG girl1("Bubbles", PPG::Pink_e, PPG::Squirrel_Speak_e);
//PPG girl2("Bubbles2", PPG::Squirrel_Speak_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("Princess",PPG::Green_e, PPG::NoPower_e);
cout << badgirl;
text_file << badgirl;
return 0;
}//end main
// End file
main.cpp -------------------------------------------------------------------------