467,077 Members | 1,131 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

help with string search

I have an assignment that requires me to write a program that uses a class, a constructor, a switch, and store the records in a text file. The second requirement is to create a function called updatePower which will search through the file looking at the names and if a name is a match, will allow you to replace the integer stored for power. I have written the program and completed the first task, but I need some assistance on the secind. Specifically, I need assistance on how to write a function that searches for a particular string. I belive that I can complete everything else after I get past the search function. Can someone assist me by showing me how to search for a string. It can be a simple search because I'm sure I can adapt it to this assignment myself after a little assistance. This is the program as I have written it so far.

Thanks in advance

#include <iostream>

using std::cerr;

using std::endl;



#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 = (int)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(std::ostream & 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("girls.txt");



if ( !outPPGFile )

{

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

exit( 1 );

} // end if



PPG girl;

girl.print(outPPGFile);

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

girl1.print(outPPGFile);

PPG badgirl("Princess",'g', 4);

badgirl.print(outPPGFile);



return 0;



}//end main

Nov 3 '06 #1
  • viewed: 1874
Share:
10 Replies

/* """ // re-formatted for those who did not go blind from OP.

B. Williams wrote in message ...
I have an assignment that requires me to write a program that uses a class, a
constructor, a switch, and store the records in a text file.
The second requirement is to create a function called updatePower which will
search through the file looking at the names and if a name is a match, will
allow you to replace the integer stored for power.
I have written the program and completed the first task, but I need some
assistance on the secind. Specifically, I need assistance on how to write a
function that searches for a particular string. I belive that I can complete
everything else after I get past the search function.
Can someone assist me by showing me how to search for a string. It can be a
simple search because I'm sure I can adapt it to this assignment myself after
a little assistance. This is the program as I have written it so far.

Thanks in advance

#include <iostream>
using std::cerr;
using std::endl;
#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 = (int)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(std::ostream &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("girls.txt");
if ( !outPPGFile ){
cerr << "File could not be opened" << endl;
exit( 1 );
} // end if

PPG girl;
girl.print(outPPGFile);
PPG girl1("Bubbles", 'b', 2);
girl1.print(outPPGFile);
PPG badgirl("Princess",'g', 4);
badgirl.print(outPPGFile);

return 0;
}//end main

""" */

ALWAYS post in 'plain text'!!! PLEASE!

Are you restricted from using 'std::string'? 'std::vector'?

--
Bob R
POVrookie
Nov 3 '06 #2

"BobR" <Re***********@worldnet.att.netwrote in message
news:HS*********************@bgtnsc04-news.ops.worldnet.att.net...
>
/* """ // re-formatted for those who did not go blind from OP.

B. Williams wrote in message ...
I have an assignment that requires me to write a program that uses a
class, a
constructor, a switch, and store the records in a text file.
The second requirement is to create a function called updatePower which
will
search through the file looking at the names and if a name is a match,
will
allow you to replace the integer stored for power.
I have written the program and completed the first task, but I need some
assistance on the secind. Specifically, I need assistance on how to write
a
function that searches for a particular string. I belive that I can
complete
everything else after I get past the search function.
Can someone assist me by showing me how to search for a string. It can be
a
simple search because I'm sure I can adapt it to this assignment myself
after
a little assistance. This is the program as I have written it so far.

Thanks in advance

#include <iostream>
using std::cerr;
using std::endl;
#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 = (int)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(std::ostream &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("girls.txt");
if ( !outPPGFile ){
cerr << "File could not be opened" << endl;
exit( 1 );
} // end if

PPG girl;
girl.print(outPPGFile);
PPG girl1("Bubbles", 'b', 2);
girl1.print(outPPGFile);
PPG badgirl("Princess",'g', 4);
badgirl.print(outPPGFile);

return 0;
}//end main

""" */

ALWAYS post in 'plain text'!!! PLEASE!

Are you restricted from using 'std::string'? 'std::vector'?

--
Bob R
POVrookie

There is no restriction on using std::string, but we have yet to go over the
vectors yet.
Nov 4 '06 #3

B Williams wrote in message ...
>
>Are you restricted from using 'std::string'? 'std::vector'?
There is no restriction on using std::string, but we have yet to go over the
vectors yet.
OK, I'll give you a shove. <G>

Change 'name' in your class:
// > char *name;
std::string name;

Then, let's get you going on 'initializer lists':

PPG(char const *a, char const b, int const c)
: name(a), dresscolor(b), power(c) { // note the colon
// > dresscolor =b; // already done
// > power = c; // already done
// > setname(a); // already done
}//end constructor 1

By the time the PPG Ctor (constructor) gets to the opening brace ( { ), the
class members are constructed and initialized. Pretty neat, eh?

PPG() : name( "Ms. Bellum" ), dresscolor( 'p' ), power(0) { // note the
colon
}//end default constructor

If you have not seen this in your studies, just keep what you had, and
change:

// > setname( a );
to:
name = a;

.....and fix up your other functions that affect 'name'

void setname(char const *a){
name = a;
}//end setname
// char const* getname() const { return name.c_str();}
// or better
std::string getname() const { return name;}

I'm not going to do the rest of your (home)work, but, I'll get you started:

// -------
#include <string>
{
std::string ReadFromFile( "Bubbles b 2" );
std::string FindThis( "Bubbles" );
// those are like "std::string FindThis = "Bubbles";".

if( ReadFromFile.find( FindThis ) != std::string::npos ){
std::cout << "Found " << FindThis << std::endl;
}
else{
std::cout << "Did not find " << FindThis
<<" in string "<<ReadFromFile<< std::endl;
}

if( ReadFromFile == FindThis ){
std::cout <<"Same"<< std::endl;
}
else{
std::cout <<"Not Same"<< std::endl;
}
}
/* -- output --
Found Bubbles
Not Same
// -------

One more thing, to pass a string to a 'char const *', use myString.c_str().
PPG girl;
std::string Marge( "Margie" );
girl.setname( Marge.c_str() ); // setname( char const *a );

Or change the arg list to:
void setname( std::string const a ){ /*.... */ }//end setname
girl.setname( Marge );

No more ' new char[ l+1 ]; ' or ' strcpy(); ' (which can have it's dangers).
'std::string' takes care of it's own memory management for you.

Look up 'std::getline' for your file read.

Give it a try and report back here if/when you need help.
--
Bob R
POVrookie
Nov 4 '06 #4
"B. Williams" <wi*******@hotmail.comwrites:
#include <iostream>
using std::cerr;
using std::endl;

#include <fstream>
using std::ofstream;

#include <cstdlib>
using std::exit;

class PPG{
public:
PPG(char *a, char b, int c) {
Better would be: PPG(const char *a, char b, int c)

[...]
setname(a);}//end constructor 1

PPG()
{ setname("Ms. Bellum");
[...]
}//end default constructor

void setname(char *a){
Better would be: void setname(const char *a)
int l = (int)strlen(a);
No need to cast. In fact, it's better to use: size_t l = strlen(a);
or at least: unsigned l = strlen(a);
name = new char[l+1];
strcpy(name,a);
name[l] = '\0';
Probably faster would be: memcpy(name, a, l+1);
}//end setname
[...]
private:
char * name;
[...]
}; //end class
Just want to point you that you're missing a destructor. setname()
method allocates memory for a string which gets never freed. That's
also the reason why using std::string (as Bob R shown in parallel
reply) is safer and easier - you don't have to worry about memory
allocation/deallocation.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Nov 4 '06 #5

"BobR" <Re***********@worldnet.att.netwrote in message
news:1_********************@bgtnsc04-news.ops.worldnet.att.net...
>
B Williams wrote in message ...
>>
>>Are you restricted from using 'std::string'? 'std::vector'?
There is no restriction on using std::string, but we have yet to go over
the
vectors yet.

OK, I'll give you a shove. <G>

Change 'name' in your class:
// > char *name;
std::string name;

Then, let's get you going on 'initializer lists':

PPG(char const *a, char const b, int const c)
: name(a), dresscolor(b), power(c) { // note the colon
// > dresscolor =b; // already done
// > power = c; // already done
// > setname(a); // already done
}//end constructor 1

By the time the PPG Ctor (constructor) gets to the opening brace ( { ),
the
class members are constructed and initialized. Pretty neat, eh?

PPG() : name( "Ms. Bellum" ), dresscolor( 'p' ), power(0) { // note
the
colon
}//end default constructor

If you have not seen this in your studies, just keep what you had, and
change:

// > setname( a );
to:
name = a;

....and fix up your other functions that affect 'name'

void setname(char const *a){
name = a;
}//end setname
// char const* getname() const { return name.c_str();}
// or better
std::string getname() const { return name;}

I'm not going to do the rest of your (home)work, but, I'll get you
started:

// -------
#include <string>
{
std::string ReadFromFile( "Bubbles b 2" );
std::string FindThis( "Bubbles" );
// those are like "std::string FindThis = "Bubbles";".

if( ReadFromFile.find( FindThis ) != std::string::npos ){
std::cout << "Found " << FindThis << std::endl;
}
else{
std::cout << "Did not find " << FindThis
<<" in string "<<ReadFromFile<< std::endl;
}

if( ReadFromFile == FindThis ){
std::cout <<"Same"<< std::endl;
}
else{
std::cout <<"Not Same"<< std::endl;
}
}
/* -- output --
Found Bubbles
Not Same
// -------

One more thing, to pass a string to a 'char const *', use
myString.c_str().
PPG girl;
std::string Marge( "Margie" );
girl.setname( Marge.c_str() ); // setname( char const *a );

Or change the arg list to:
void setname( std::string const a ){ /*.... */ }//end setname
girl.setname( Marge );

No more ' new char[ l+1 ]; ' or ' strcpy(); ' (which can have it's
dangers).
'std::string' takes care of it's own memory management for you.

Look up 'std::getline' for your file read.

Give it a try and report back here if/when you need help.
--
Bob R
POVrookie

Bob,
I have been messing with this all weekend and have come away more confused.
What I need help with is creating the search function.
Nov 6 '06 #6

B. Williams wrote in message ...
<snip old>
>>
Bob,
I have been messing with this all weekend and have come away more confused.
What I need help with is creating the search function.
OK. We need to find out where you are getting confused.

Here is your class PPG as I have modified it (so far). Point out what you
don't understand, and I'll back up or explain until you 'get it'.

// ---------------------------
#include <Iostream>
#include <ostream>
#include <string>
#include <sstream>
// #include <fstream// (if you use it)
// ---------------------------
class PPG{ public:
PPG( char const *a, char const b, int const c )
: name(a), dresscolor(b), power(c){ }// end constructor 1
PPG() : name( "Ms. Bellum" ), dresscolor( 'p' ), power(0){
} // end default constructor
// ----------------------
char const* Name() const { return name.c_str();} // getname()
char const* Name( char const *a ){ // setname( char const *a )
name = a;
return name.c_str();
}//end (set)name
// -------
int Power() const{ return power; } // getpower()
int Power( int z ){ // void setpower( int z )
if( power < 0 || power MaxLines - 1 ){ power = 0;}
else{ power = z;}
return power;
}
// -------
char Dresscolor() const{ return dresscolor; }
char Dresscolor( char v ){ dresscolor = v; return dresscolor; }
// -------
void print( std::ostream &out ) const {
out << name << " likes to wear ";
switch( dresscolor ){
case 'g': case 'G':
out <<"green"; break;
case 'b':case 'B':
out <<"blue"; break;
case 'p': case 'P':
out <<"pink"; break;
default:{
out <<"no";
}
} // end switch
out <<" dresses. She uses her ";

if( power < 0 || power MaxLines - 1 ){
out << Powerlines[ 0 ];
}
else{
out << Powerlines[ power ];
}
return;
} // end print
// -------
bool operator==( PPG &ppg ){
// was: return (strcmp(name, ppg.name)==0);
return ( name == ppg.name );
}
// ----------------------
private:
std::string name;
char dresscolor; //g-reen, b-lue, p-pink
int power; //1-ice breath, 2- squirrel speak, 4-bad attitude
static int const MaxLines = 6;
static std::string const Powerlines[ MaxLines ];
}; //end class PPG
// ---------------------------
// non-integral statics must be 'defined' outside class body.
std::string const PPG::Powerlines[ PPG::MaxLines ] = {
"girl power to rule the world.\n",
"ice breath to defeat her enemies.\n",
"ability to talk to squirrels to confuse evil villians.\n",
"fantastic hips to \"wow\" the dudes.\n",
"bad attitude to stop evil doers.\n",
"ERROR in indexing.\n" // a little 'safety net'
};
// ---------------------------

int main(){
// don't worry about understanding this yet.
std::ostringstream sos;

PPG girl;
girl.print( sos );
girl.Dresscolor( 'B' );
// girl.Power( 4 ); //girl.print( sos );
if( girl.Power( 4 ) == 4 ){
girl.print( sos );
}
PPG girl1( "Bubbles", 'b', 2);
girl1.print( sos );
// PPG badgirl( "Princess", 'g', 3);
PPG badgirl( "Princess", 'a', 3);
badgirl.print( sos );

std::cout<< sos.str() <<std::endl;
// or:
// std::ofstream outPPGFile( "girls.txt" );
// if( !outPPGFile ){
// cerr << "File could not be opened" << endl;
// exit( 1 );
// } // end if
// outPPGFile<< sos.str() <<std::endl;
// outPPGFile.close(); // only if you re-used 'outPPGFile'.
// outPPGFile.clear(); // only if you re-used 'outPPGFile'.

return 0;
} // main() end
// ------------------------------------
Because the Name() 'getter' function returns a 'char const *' instead of a
'std::string', you can't *directly* compare them. So:

std::string GirlA( girl.Name() );
std::string GirlB( badgirl.Name() );
if( GirlA == GirlB ){
std::cout<<"Same name.\n";
}
else{
badgirl.Name( GirlA.c_str() ); // change the name.
}

But your 'operator==( PPG &ppg )' in your class will allow:

if( girl == badgirl ){
std::cout<<"Same name.\n";
}
else{
std::cout<<"Not same name.\n";
}
Now we need to know the *exact* wording of your assignment (just the 'find'
part.).
Are you to load the file into the class instances, then compare?
Search the file, and then load that person into the class instance.
Is 'FindGirl' to be internal or external to the class? Or just in 'main()'?
Etc.

Post your code(attempt), what you expect and what you get (+errors (first
three, if many)).

--
Bob R
POVrookie
Nov 6 '06 #7

"BobR" <Re***********@worldnet.att.netwrote in message
news:SU*******************@bgtnsc05-news.ops.worldnet.att.net...
>
B. Williams wrote in message ...
<snip old>
>>>
Bob,
I have been messing with this all weekend and have come away more
confused.
What I need help with is creating the search function.

OK. We need to find out where you are getting confused.

Here is your class PPG as I have modified it (so far). Point out what you
don't understand, and I'll back up or explain until you 'get it'.

// ---------------------------
#include <Iostream>
#include <ostream>
#include <string>
#include <sstream>
// #include <fstream// (if you use it)
// ---------------------------
class PPG{ public:
PPG( char const *a, char const b, int const c )
: name(a), dresscolor(b), power(c){ }// end constructor 1
PPG() : name( "Ms. Bellum" ), dresscolor( 'p' ), power(0){
} // end default constructor
// ----------------------
char const* Name() const { return name.c_str();} // getname()
char const* Name( char const *a ){ // setname( char const *a )
name = a;
return name.c_str();
}//end (set)name
// -------
int Power() const{ return power; } // getpower()
int Power( int z ){ // void setpower( int z )
if( power < 0 || power MaxLines - 1 ){ power = 0;}
else{ power = z;}
return power;
}
// -------
char Dresscolor() const{ return dresscolor; }
char Dresscolor( char v ){ dresscolor = v; return dresscolor; }
// -------
void print( std::ostream &out ) const {
out << name << " likes to wear ";
switch( dresscolor ){
case 'g': case 'G':
out <<"green"; break;
case 'b':case 'B':
out <<"blue"; break;
case 'p': case 'P':
out <<"pink"; break;
default:{
out <<"no";
}
} // end switch
out <<" dresses. She uses her ";

if( power < 0 || power MaxLines - 1 ){
out << Powerlines[ 0 ];
}
else{
out << Powerlines[ power ];
}
return;
} // end print
// -------
bool operator==( PPG &ppg ){
// was: return (strcmp(name, ppg.name)==0);
return ( name == ppg.name );
}
// ----------------------
private:
std::string name;
char dresscolor; //g-reen, b-lue, p-pink
int power; //1-ice breath, 2- squirrel speak, 4-bad attitude
static int const MaxLines = 6;
static std::string const Powerlines[ MaxLines ];
}; //end class PPG
// ---------------------------
// non-integral statics must be 'defined' outside class body.
std::string const PPG::Powerlines[ PPG::MaxLines ] = {
"girl power to rule the world.\n",
"ice breath to defeat her enemies.\n",
"ability to talk to squirrels to confuse evil villians.\n",
"fantastic hips to \"wow\" the dudes.\n",
"bad attitude to stop evil doers.\n",
"ERROR in indexing.\n" // a little 'safety net'
};
// ---------------------------

int main(){
// don't worry about understanding this yet.
std::ostringstream sos;

PPG girl;
girl.print( sos );
girl.Dresscolor( 'B' );
// girl.Power( 4 ); //girl.print( sos );
if( girl.Power( 4 ) == 4 ){
girl.print( sos );
}
PPG girl1( "Bubbles", 'b', 2);
girl1.print( sos );
// PPG badgirl( "Princess", 'g', 3);
PPG badgirl( "Princess", 'a', 3);
badgirl.print( sos );

std::cout<< sos.str() <<std::endl;
// or:
// std::ofstream outPPGFile( "girls.txt" );
// if( !outPPGFile ){
// cerr << "File could not be opened" << endl;
// exit( 1 );
// } // end if
// outPPGFile<< sos.str() <<std::endl;
// outPPGFile.close(); // only if you re-used 'outPPGFile'.
// outPPGFile.clear(); // only if you re-used 'outPPGFile'.

return 0;
} // main() end
// ------------------------------------
Because the Name() 'getter' function returns a 'char const *' instead of a
'std::string', you can't *directly* compare them. So:

std::string GirlA( girl.Name() );
std::string GirlB( badgirl.Name() );
if( GirlA == GirlB ){
std::cout<<"Same name.\n";
}
else{
badgirl.Name( GirlA.c_str() ); // change the name.
}

But your 'operator==( PPG &ppg )' in your class will allow:

if( girl == badgirl ){
std::cout<<"Same name.\n";
}
else{
std::cout<<"Not same name.\n";
}
Now we need to know the *exact* wording of your assignment (just the
'find'
part.).
Are you to load the file into the class instances, then compare?
Search the file, and then load that person into the class instance.
Is 'FindGirl' to be internal or external to the class? Or just in
'main()'?
Etc.

Post your code(attempt), what you expect and what you get (+errors (first
three, if many)).

--
Bob R
POVrookie
That was alot to digest, but I'm about to get started.
Nov 6 '06 #8

"BobR" <Re***********@worldnet.att.netwrote in message
news:SU*******************@bgtnsc05-news.ops.worldnet.att.net...
>
B. Williams wrote in message ...
<snip old>
>>>
Bob,
I have been messing with this all weekend and have come away more
confused.
What I need help with is creating the search function.

OK. We need to find out where you are getting confused.

Here is your class PPG as I have modified it (so far). Point out what you
don't understand, and I'll back up or explain until you 'get it'.

// ---------------------------
#include <Iostream>
#include <ostream>
#include <string>
#include <sstream>
// #include <fstream// (if you use it)
// ---------------------------
class PPG{ public:
PPG( char const *a, char const b, int const c )
: name(a), dresscolor(b), power(c){ }// end constructor 1
PPG() : name( "Ms. Bellum" ), dresscolor( 'p' ), power(0){
} // end default constructor
// ----------------------
char const* Name() const { return name.c_str();} // getname()
char const* Name( char const *a ){ // setname( char const *a )
name = a;
return name.c_str();
}//end (set)name
// -------
int Power() const{ return power; } // getpower()
int Power( int z ){ // void setpower( int z )
if( power < 0 || power MaxLines - 1 ){ power = 0;}
else{ power = z;}
return power;
}
// -------
char Dresscolor() const{ return dresscolor; }
char Dresscolor( char v ){ dresscolor = v; return dresscolor; }
// -------
void print( std::ostream &out ) const {
out << name << " likes to wear ";
switch( dresscolor ){
case 'g': case 'G':
out <<"green"; break;
case 'b':case 'B':
out <<"blue"; break;
case 'p': case 'P':
out <<"pink"; break;
default:{
out <<"no";
}
} // end switch
out <<" dresses. She uses her ";

if( power < 0 || power MaxLines - 1 ){
out << Powerlines[ 0 ];
}
else{
out << Powerlines[ power ];
}
return;
} // end print
// -------
bool operator==( PPG &ppg ){
// was: return (strcmp(name, ppg.name)==0);
return ( name == ppg.name );
}
// ----------------------
private:
std::string name;
char dresscolor; //g-reen, b-lue, p-pink
int power; //1-ice breath, 2- squirrel speak, 4-bad attitude
static int const MaxLines = 6;
static std::string const Powerlines[ MaxLines ];
}; //end class PPG
// ---------------------------
// non-integral statics must be 'defined' outside class body.
std::string const PPG::Powerlines[ PPG::MaxLines ] = {
"girl power to rule the world.\n",
"ice breath to defeat her enemies.\n",
"ability to talk to squirrels to confuse evil villians.\n",
"fantastic hips to \"wow\" the dudes.\n",
"bad attitude to stop evil doers.\n",
"ERROR in indexing.\n" // a little 'safety net'
};
// ---------------------------

int main(){
// don't worry about understanding this yet.
std::ostringstream sos;

PPG girl;
girl.print( sos );
girl.Dresscolor( 'B' );
// girl.Power( 4 ); //girl.print( sos );
if( girl.Power( 4 ) == 4 ){
girl.print( sos );
}
PPG girl1( "Bubbles", 'b', 2);
girl1.print( sos );
// PPG badgirl( "Princess", 'g', 3);
PPG badgirl( "Princess", 'a', 3);
badgirl.print( sos );

std::cout<< sos.str() <<std::endl;
// or:
// std::ofstream outPPGFile( "girls.txt" );
// if( !outPPGFile ){
// cerr << "File could not be opened" << endl;
// exit( 1 );
// } // end if
// outPPGFile<< sos.str() <<std::endl;
// outPPGFile.close(); // only if you re-used 'outPPGFile'.
// outPPGFile.clear(); // only if you re-used 'outPPGFile'.

return 0;
} // main() end
// ------------------------------------
Because the Name() 'getter' function returns a 'char const *' instead of a
'std::string', you can't *directly* compare them. So:

std::string GirlA( girl.Name() );
std::string GirlB( badgirl.Name() );
if( GirlA == GirlB ){
std::cout<<"Same name.\n";
}
else{
badgirl.Name( GirlA.c_str() ); // change the name.
}

But your 'operator==( PPG &ppg )' in your class will allow:

if( girl == badgirl ){
std::cout<<"Same name.\n";
}
else{
std::cout<<"Not same name.\n";
}
Now we need to know the *exact* wording of your assignment (just the
'find'
part.).
Are you to load the file into the class instances, then compare?
Search the file, and then load that person into the class instance.
Is 'FindGirl' to be internal or external to the class? Or just in
'main()'?
Etc.

Post your code(attempt), what you expect and what you get (+errors (first
three, if many)).

--
Bob R
POVrookie
On my way to a tutoring session. I'll post my attempt when I return.
Nov 6 '06 #9

"BobR" <Re***********@worldnet.att.netwrote in message
news:SU*******************@bgtnsc05-news.ops.worldnet.att.net...
>
B. Williams wrote in message ...
<snip old>
>>>
Bob,
I have been messing with this all weekend and have come away more
confused.
What I need help with is creating the search function.

OK. We need to find out where you are getting confused.

Here is your class PPG as I have modified it (so far). Point out what you
don't understand, and I'll back up or explain until you 'get it'.

// ---------------------------
#include <Iostream>
#include <ostream>
#include <string>
#include <sstream>
// #include <fstream// (if you use it)
// ---------------------------
class PPG{ public:
PPG( char const *a, char const b, int const c )
: name(a), dresscolor(b), power(c){ }// end constructor 1
PPG() : name( "Ms. Bellum" ), dresscolor( 'p' ), power(0){
} // end default constructor
// ----------------------
char const* Name() const { return name.c_str();} // getname()
char const* Name( char const *a ){ // setname( char const *a )
name = a;
return name.c_str();
}//end (set)name
// -------
int Power() const{ return power; } // getpower()
int Power( int z ){ // void setpower( int z )
if( power < 0 || power MaxLines - 1 ){ power = 0;}
else{ power = z;}
return power;
}
// -------
char Dresscolor() const{ return dresscolor; }
char Dresscolor( char v ){ dresscolor = v; return dresscolor; }
// -------
void print( std::ostream &out ) const {
out << name << " likes to wear ";
switch( dresscolor ){
case 'g': case 'G':
out <<"green"; break;
case 'b':case 'B':
out <<"blue"; break;
case 'p': case 'P':
out <<"pink"; break;
default:{
out <<"no";
}
} // end switch
out <<" dresses. She uses her ";

if( power < 0 || power MaxLines - 1 ){
out << Powerlines[ 0 ];
}
else{
out << Powerlines[ power ];
}
return;
} // end print
// -------
bool operator==( PPG &ppg ){
// was: return (strcmp(name, ppg.name)==0);
return ( name == ppg.name );
}
// ----------------------
private:
std::string name;
char dresscolor; //g-reen, b-lue, p-pink
int power; //1-ice breath, 2- squirrel speak, 4-bad attitude
static int const MaxLines = 6;
static std::string const Powerlines[ MaxLines ];
}; //end class PPG
// ---------------------------
// non-integral statics must be 'defined' outside class body.
std::string const PPG::Powerlines[ PPG::MaxLines ] = {
"girl power to rule the world.\n",
"ice breath to defeat her enemies.\n",
"ability to talk to squirrels to confuse evil villians.\n",
"fantastic hips to \"wow\" the dudes.\n",
"bad attitude to stop evil doers.\n",
"ERROR in indexing.\n" // a little 'safety net'
};
// ---------------------------

int main(){
// don't worry about understanding this yet.
std::ostringstream sos;

PPG girl;
girl.print( sos );
girl.Dresscolor( 'B' );
// girl.Power( 4 ); //girl.print( sos );
if( girl.Power( 4 ) == 4 ){
girl.print( sos );
}
PPG girl1( "Bubbles", 'b', 2);
girl1.print( sos );
// PPG badgirl( "Princess", 'g', 3);
PPG badgirl( "Princess", 'a', 3);
badgirl.print( sos );

std::cout<< sos.str() <<std::endl;
// or:
// std::ofstream outPPGFile( "girls.txt" );
// if( !outPPGFile ){
// cerr << "File could not be opened" << endl;
// exit( 1 );
// } // end if
// outPPGFile<< sos.str() <<std::endl;
// outPPGFile.close(); // only if you re-used 'outPPGFile'.
// outPPGFile.clear(); // only if you re-used 'outPPGFile'.

return 0;
} // main() end
// ------------------------------------
Because the Name() 'getter' function returns a 'char const *' instead of a
'std::string', you can't *directly* compare them. So:

std::string GirlA( girl.Name() );
std::string GirlB( badgirl.Name() );
if( GirlA == GirlB ){
std::cout<<"Same name.\n";
}
else{
badgirl.Name( GirlA.c_str() ); // change the name.
}

But your 'operator==( PPG &ppg )' in your class will allow:

if( girl == badgirl ){
std::cout<<"Same name.\n";
}
else{
std::cout<<"Not same name.\n";
}
Now we need to know the *exact* wording of your assignment (just the
'find'
part.).
Are you to load the file into the class instances, then compare?
Search the file, and then load that person into the class instance.
Is 'FindGirl' to be internal or external to the class? Or just in
'main()'?
Etc.

Post your code(attempt), what you expect and what you get (+errors (first
three, if many)).

--
Bob R
POVrookie

Bob,
Sorry I'm just getting around to posting this. My tutor had me start over.

The search function I was asking for assistance with is posted below.

void updatePower(string filename) {
ifstream in(filename.c_str(), ios::in);
int cnt = 0;

// read all records into database until we reach the end of the file
while (!in.eof()) {
database[cnt++].input(in);
}

// close text file
in.close();

cout << "Enter entry ID to change balance:";
cin >id;
if (id<0 || id>49) {
cout << "illegal ID - abort." << endl;
return;
}

// enter new power
double power;
cout << "Enter new power:";
cin >power;

// set new customer balance
database[id].setupdatePower(power);

// write customers back to text file
ofstream ofs(filename.c_str(), ios::out);
for (int i=0;i<cnt-1;i++)
database[i].output(ofs);//update, ie reset your file pointer
ofs.close();
}
Nov 8 '06 #10

B. Williams wrote in message ...
>
>// ------------------------------------
Post your code(attempt), what you expect and what you get (+errors (first
three, if many)).
Bob R POVrookie

Bob,
Sorry I'm just getting around to posting this. My tutor had me start over.
Bummer!!!
>
The search function I was asking for assistance with is posted below.

void updatePower( string filename ) {
// ifstream in(filename.c_str(), ios::in);

The '(std::)ios::in' is not needed. It is the default for an 'std::ifstream'.

ifstream in( filename.c_str() );
int cnt = 0;

// read all records into database until we reach the end of the file
while (!in.eof()) {
Using the 'eof()' as a condition of the while is bad practice. By the time
'eof()' goes true is too late and you'll already have read past the end. It
will loop one too many.
database[cnt++].input(in);
I don't have a clue what 'database' is.
Without knowing what 'input()' does, I can't advise on how to do your while
loop.
Common use is:

std::string line;
while( std::getline( in, line) ){ // read a whole line from ifstream 'in'.
// do something with 'line'
}

}

// close text file
in.close();
The 'close()' does not reset the flags. It won't matter here, but, if you
ever need to re-use the object, do the following too:

in.clear();
>
cout << "Enter entry ID to change balance:";
cin >id;
I don't know what 'id' is. It's not in this scope.
if ( id < 0 || id 49 ) {
cout << "illegal ID - abort." << endl;
return;
}

// enter new power
double power;
cout << "Enter new power:";
cin >power;

// set new customer balance
database[ id ].setupdatePower( power );

// write customers back to text file
// ofstream ofs(filename.c_str(), ios::out);

The '(std::)ios::out' is not needed. It is the default for an
'std::ofstream'.
Are you aware that you are writeing out to the same file you just read in,
with the same data?
Since 'filename' is a non-const std::string, you could do:

filename = "new" + filename; // that prepends "new" to the name.

ofstream ofs( filename.c_str() );
for( int i=0; i < cnt-1; ++i )
database[i].output(ofs); // update, ie reset your file pointer
ofs.close();
The 'close()' is not needed since the closing brace will delete the ofs
object. (but, IMHO, it is not a bad habit (won't hurt anything)).
}

Keep at it.
--
Bob R
POVrookie
Nov 9 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Rahul | last post: by
2 posts views Thread by rufpirat@gmail.com | last post: by
66 posts views Thread by genestarwing@hotmail.com | last post: by
2 posts views Thread by babynailah | last post: by
9 posts views Thread by pic078 via AccessMonster.com | last post: by
2 posts views Thread by =?Utf-8?B?U3RldmU=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.