Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

left/right/mid function

Status
Not open for further replies.
Mar 29, 2002
33
US
I'm trying to use a base class from another project and create a derived class with three new functions for left/mid/right. I'm still receiving a few errors. What am I missing in my code? Any pointers or hints would help.
Thanks-



#include <iostream>
#include <string>
using namespace std;
////////////////////////////////////////////////
class String
{
protected:
enum {SZ=80};
char str[SZ];

public:
String getstring()
{
cin.get(str, SZ);
return str;
}

String()
{ str[0] = '\0'; }

String( char s[] )
{ strcpy(str, s); }

void display()
{ cout << str; }

operator char* ()
{return str;}

bool operator == (String st) const
{return ( strcmp (str, st.str)==0 ) ? true : false;}
bool operator < (String st) const
{return ( strcmp (str, st.str)<0 ) ? true : false;}
bool operator > (String st) const
{return ( strcmp (str, st.str)>0 ) ? true : false;}
bool operator <= (String st) const
{return ( strcmp (str, st.str)<=0 ) ? true : false;}
bool operator >= (String st) const
{return ( strcmp (str, st.str)>=0 ) ? true : false;}
bool operator != (String st) const
{return ( strcmp (str, st.str)!=0 ) ? true : false;}
};

////////////////////////////////////////////////
class Pstring2: public String
{
public:
Pstring2() : String() {} ;
Pstring2(char s[ ]): String(s) {};
void right(Pstring2, int l);
void left(Pstring2, int l);
void mid(Pstring2, mid, int l);
};

void Pstring2::right(Pstring2 s, int n)
{
int i,j;
for (j=0, i= n<strlen(s) ? strlen(s)-n : 0;
i<=strlen(s);
i++,j++)
str[j]=s.str;
str[j] = '\0';
}

void Pstring2::left(Pstring2 s, int n)
{
int i,j;
for (j=0, i= n<strlen(s) ? strlen(s)-n : 0;
i<=strlen(s);
i--,j++)
str[j]=s.str;
str[j] = '\0';
}


void Pstring2::mid(Pstring2 s, int m, int n)
{
int i,j;
for (j=0, i= n<strlen(s) ? strlen(s)-n : 0;
i<=strlen(s);
i--,j++)
str[j]=s.str[n+i-1];
str[j+1] = '\0';
}


//////////////////////////////////////////////////////
void main()
{
Pstring2 s1, s2;
int length; mid;

cout<<&quot;Enter phrase: &quot;;
s1.getstring();
cout<<&quot;Enter concatenation length: &quot;;
cin>>length;
s2.right(s1,length);

cout << &quot;\nPhrase now reads: &quot; ; s2.display();
cout<<&quot;\n\n&quot;;


cout<<&quot;Enter phrase: &quot;;
s1.getstring();
cout<<&quot;Enter concatenation length: &quot;;
cin>>length;
s2.left(s1,length);

cout << &quot;\nPhrase now reads: &quot; ; s2.display();
cout<<&quot;\n\n&quot;;


cout<<&quot;Enter phrase: &quot;;
s1.getstring();
cout<<&quot;Enter concatenation length: &quot;;
cin>>length;
cin>>mid;
s2.mid(s1, mid, length);
cout << &quot;\nPhrase now reads: &quot; ; s2.display();
cout<<&quot;\n\n&quot;;

}
 
>I'm still receiving a few errors.

Be specific


/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
I think you should study the possibilities of the class std::string. You have all these possibilities and yu don't need at all implementing the Basic language functions.

Ion Filipski
1c.bmp
 
It is not a good ideea to return a void for mid(0, left(0, right() functions. You should implement these functions like as follow:
Pstring2 Pstring2::left(int position)
{

}
Pstring2 Pstring2::right(int number)
{
}
Pstring2 Pstring2::mid(int start, int howmany)
{
}
With these changes you can apply them on the same object without problems:
Pstring2 s1(&quot;This is a string ...&quot;);
Pstring2 sLeft=s1.left(10);
Pstring2 sRigth = s1.right(3);
Pstring2 sMid = s1.mid(3,4);

-obislavu-


 
Thanks for the tips. I'm still working through things now. As for the errors I am receiving, they are as follows.
Thanks again-


error C2061: syntax error : identifier 'mid'
error C2065: 'mid' : undeclared identifier
error C2511: 'mid' : overloaded member function 'void (class Pstring2,int,int)' not found in 'Pstring2'
 
void mid(Pstring2, mid, int l);

should probably be

void mid(Pstring2, int mid, int l);

Vincent
 
It is about learning std::string:

#include<iostream>
#include<string>
using namespace std;
int main()
{
string x = &quot;hello to whole world&quot;;
cout<< x.c_str()<< endl;
int lf;
//left lf
l = 5;
cout<< x.substr(0, lf).c_str()<< endl;
//mid lf, rt
l = 6;
int rt = 8;
cout<< x.substr(lf, rt).c_str()<< endl;
//right rt
rt = 5;
cout<< x.substr(x.size() - rt, rt).c_str()<< endl;


return 0;
}

Ion Filipski
1c.bmp
 
>error C2061: syntax error : identifier 'mid'
Revise this declaration: void mid(Pstring2, mid, int l);
and should be something like that:
void mid(Pstring2, int , int );
> error C2065: 'mid' : undeclared identifier
It is related with the 2nd line in the main():
int length; mid;
Should be :
int lenght, mid;
In the right() and left() you cannot store a string in one char:
str[j]=s.str;
Should be an index applied on the right member.
Use
Code:
when you post your code.
-obislavu-





 
I finally went with another alternative and got the program to run correctly. I wanted to thank everyone who helped me along the way.
Thanks, redducati748


#include <iostream>
#include <string>
using namespace std;
///////////////////////////////////////////////////
class String
{
protected:
enum {SZ=80};
char str[SZ];

public:
String getstring()
{
cin.get(str, SZ);
return str;
}

String()
{ str[0] = '\0'; }

String( char s[] )
{ strcpy(str, s); }

void display()
{ cout << str; }

bool operator == (String st) const
{return ( strcmp (str, st.str)==0 ) ? true : false;}
bool operator < (String st) const
{return ( strcmp (str, st.str)<0 ) ? true : false;}
bool operator > (String st) const
{return ( strcmp (str, st.str)>0 ) ? true : false;}
bool operator <= (String st) const
{return ( strcmp (str, st.str)<=0 ) ? true : false;}
bool operator >= (String st) const
{return ( strcmp (str, st.str)>=0 ) ? true : false;}
bool operator != (String st) const
{return ( strcmp (str, st.str)!=0 ) ? true : false;}
};

///////////////////////////////////////////////////////
class Pstring2:public String
{
public:

void right(Pstring2 s, int n)
{
int i,m;
m=strlen(s.str);
for (i=0; i<=n; i++)
str=s.str[m-n+i];
}

void left(Pstring2 s, int n)
{
int i,m;
m=strlen(s.str);
for (i=0; i<n; i++)
str=s.str;
}


void mid(Pstring2 s, int mid, int n)
{
int i,m;
m=strlen(s.str);
for (i=0; i<n; i++)
str=s.str[n+i-1];
}
};

///////////////////////////////////////////////
void main()
{
Pstring2 s1, s2;
int length, mid;

cout<<&quot;Enter phrase: &quot;;
s1.getstring();
cout<<&quot;Enter concatenation length: &quot;;
cin>>length;

s2.right(s1,length);
cout << &quot;\nPhrase now reads right: &quot; ; s2.display();
cout<<&quot;\n&quot;;

s2.left(s1,length);
cout << &quot;\nPhrase now reads left: &quot; ; s2.display();
cout<<&quot;\n\n&quot;;

cout<<&quot;Enter mid starting position: &quot;;
cin>>mid;
s2.mid(s1, mid, length);
cout << &quot;\nPhrase now reads mid: &quot; ; s2.display();
cout<<&quot;\n\n&quot;;

}
 
redducati748, don't waste your time with reinventing something what already exist and is standard in C++. Use method substr from the STL class string (std::string) instead of any left/mid/right. Look at my post above.


Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top