473,625 Members | 2,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(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

Nov 3 '06 #1
10 2236

/* """ // 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(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

""" */

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.ne twrote in message
news:HS******** *************@b gtnsc04-news.ops.worldn et.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(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

""" */

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::strin g FindThis = "Bubbles";" .

if( ReadFromFile.fi nd( FindThis ) != std::string::np os ){
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*******@hotm ail.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*chr ome.pl>--ooO--(_)--Ooo--
Nov 4 '06 #5

"BobR" <Re***********@ worldnet.att.ne twrote in message
news:1_******** ************@bg tnsc04-news.ops.worldn et.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::strin g FindThis = "Bubbles";" .

if( ReadFromFile.fi nd( FindThis ) != std::string::np os ){
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::ostringstr eam 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.clos e(); // only if you re-used 'outPPGFile'.
// outPPGFile.clea r(); // 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<<"Sam e 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<<"Sam e 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.ne twrote in message
news:SU******** ***********@bgt nsc05-news.ops.worldn et.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::ostringstr eam 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.clos e(); // only if you re-used 'outPPGFile'.
// outPPGFile.clea r(); // 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<<"Sam e 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<<"Sam e 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.ne twrote in message
news:SU******** ***********@bgt nsc05-news.ops.worldn et.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::ostringstr eam 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.clos e(); // only if you re-used 'outPPGFile'.
// outPPGFile.clea r(); // 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<<"Sam e 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<<"Sam e 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.ne twrote in message
news:SU******** ***********@bgt nsc05-news.ops.worldn et.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::ostringstr eam 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.clos e(); // only if you re-used 'outPPGFile'.
// outPPGFile.clea r(); // 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<<"Sam e 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<<"Sam e 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(str ing filename) {
ifstream in(filename.c_s tr(), 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2209
by: Liz Malcolm | last post by:
Hello and TIA for guidance. I am building a reusable search procedure (thanks go to Graham Thorpe for his example that set me on my way). Everything works up until the 2nd match is found, the command doesn't go to the third match, it exits out of the loop. I've read everything I could find in NG's, but I can't figure out where I'm going wrong. Here is my code. Function cmdSearch(ctlSearchText As Control, ctlFoundText As Control,...
1
3703
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
2
4450
by: rufpirat | last post by:
Hello I'm in the middle of trying to build an "AD phone book", and this being my first try at asp.net, I have a few questions that I hope some of you might be able to help with: 1. Is it correct, that PageSize equals the max size of the result set? 2. Is there a way to make asp cache the search result, so the domain controller won't be to bother by all the lookups, and also to speed up
66
5334
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it occurs. After the program has read the file, it should offer a menu with three choices. the first is to list all the words along with the number of occurences. The second is to let you enter a word, with the program reporting how many times the...
2
1692
by: babynailah | last post by:
This is what i have to do and this is all i have.... HELP ME PLEASE MY GRADE IN CLASS DEPENDS ON IT!!!! Your problem is to simulate certain aspects of a cell phone. The following features are required for the first assignment. The interface must be a GUI that resembles a cell phone. The cell phone must contain an array to hold a list of contacts. Each contact has a name and number. Make the size of the array 5 and be sure to...
3
1987
by: splintercell | last post by:
well i got this code from java.sun.com and tried modiifying it in all the possible ways,but to no good.. stil its not workin..pleas help me out and try postin good workinw web cralwer if u have.. need help asap... import java.applet.Applet; import java.text.*; import java.awt.*; import java.awt.List; import java.awt.event.*; import java.util.*;
0
5557
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
9
3936
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result - I have read every post out there and spent hours trying to figure out the problem with no success whatsoever - I have constrained the problem to one form however, and I think it's hiding somewhere in my code associated with this form, which is...
2
1210
by: =?Utf-8?B?U3RldmU=?= | last post by:
Hello all, I need to find all the distribution lists/groups a user is a member of using Active Directory when they request an INTRANET webpage. Impersonation is DISABLED on this site. I have a ASP.NET webpage written in VB.NET where when a user loads the page it get's their network user name by processing: Request.ServerVariables("LOGON_USER") and now I need to find out what email distribution lists they are a member of.
1
8354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7182
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6116
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4192
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1499
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.