B
B. Williams
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:
fstream;
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(char 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:
fstream;
int main()
{
ofstream PPG("girls.txt", ios:
ut);
PPG girl1("Bubbles", 'b', 2);
girl1.print();
PPG badgirl("Princess",'g', 4);
badgirl.print();
return 0;
}//end main
#include <iostream>
#include <string>
#include <fstream>
using std:
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(char 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:
int main()
{
ofstream PPG("girls.txt", ios:
PPG girl1("Bubbles", 'b', 2);
girl1.print();
PPG badgirl("Princess",'g', 4);
badgirl.print();
return 0;
}//end main