473,513 Members | 4,116 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(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
10 2224

/* """ // 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2200
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...
1
3694
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...
2
4446
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...
66
5279
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...
2
1688
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...
3
1978
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...
0
5522
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
3926
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...
2
1202
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:...
0
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7125
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...
0
7543
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...
0
5703
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...
1
5102
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...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.