help with string search

B

B. Williams

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::eek:fstream;



#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::eek:stream & 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
 
B

BobR

/* """ // 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::eek:fstream;
#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::eek:stream &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'?
 
B

B Williams

BobR said:
/* """ // 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::eek:fstream;
#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::eek:stream &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'?
There is no restriction on using std::string, but we have yet to go over the
vectors yet.
 
B

BobR

B Williams wrote in message ...
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.
 
M

Michal Nazarewicz

B. Williams said:
#include <iostream>
using std::cerr;
using std::endl;

#include <fstream>
using std::eek:fstream;

#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.
 
B

B. Williams

BobR said:
B Williams wrote in message ...
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,
I have been messing with this all weekend and have come away more confused.
What I need help with is creating the search function.
 
B

BobR

B. Williams wrote in message ...
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::eek:stream &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::eek:stringstream 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::eek:fstream 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)).
 
B

B. Williams

BobR said:
B. Williams wrote in message ...
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::eek:stream &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::eek:stringstream 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::eek:fstream 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)).
That was alot to digest, but I'm about to get started.
 
B

B. Williams

BobR said:
B. Williams wrote in message ...
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::eek:stream &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::eek:stringstream 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::eek:fstream 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)).
On my way to a tutoring session. I'll post my attempt when I return.
 
B

B. Williams

BobR said:
B. Williams wrote in message ...
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::eek:stream &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::eek:stringstream 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::eek:fstream 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::eek:ut);
for (int i=0;i<cnt-1;i++)
database.output(ofs);//update, ie reset your file pointer
ofs.close();
}
 
B

BobR

B. Williams wrote in message ...
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::eek:ut);

The '(std::)ios::eek:ut' is not needed. It is the default for an
'std::eek:fstream'.
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.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top