this program is inorder to split a string like krishnamoorthy into krishna & moorthy by splitting it at m
i tried the following coding.the compiler pointed out an error saying that for cannot be expanded inline.what shud i do to rectify the error?this

#include<iostream.h>
#include<string.h>
#include<conio.h>
class string
{
private:
char ch[20];
public:
void get()
{
cout<<"\nenter string";
cin>>ch;
}
void operator / (string s1)
{
 int l;
 l=strlen(s1.ch);
if(l%2==0)
{
 s=l/2;
 for(int i=0;i<s;i++)
{ 
 cout<<ch[i];
 }
}
else
{
s=(l+1)/2;
for(int i=0;i<s;i++)
{
cout<<ch[i];
 }
}
}
void main()
{
string s1,s2;
s2.get();
s1/s2;
getch();
}

line 4: >> class string
give you class a different name because string is the name of std::string that's in the <string> header file.

What compiler are you using? If TurboC++ then trash it and get a new compiler if you can because some of the code you are writing is non-standard.

line 31: semicolon required at the end of class declarations

lines 20 and 28: variabe s is undeclared.

i tried the following coding but it gave absurd results during runtime and the compiler showed a warning that for cannot be expanded inline.

#include<iostream.h>
#include<string.h>
#include<conio.h>
class splitstr
{
private:
char ch[20];
int s,i,l;
public:
void get()
{
cout<<"enter a string"<<endl;
cin>>ch;
}
void operator/(splitstr s1)
{
l=strlen(s1.ch);
if(l%2==0)
{
s=l/2;
for(i=s;i<l;i++)
{ cout<<ch[i]<<endl;}
}
else
{
s=(l+1)/2;
for(i=s;i<l;i++)
{ cout<<ch[i]<<endl;}
}
}
};
void main()
{
splitstr s1,s2;
s2.get();
s1/s2;
getch();
}

the output was as follows
enter a string
krishnamooorthy
-
>_
,
can u pls tell me what should i do to get the correct result?i want the string krishnamoorthy to be split at m & moorthy to be printed

First, use code tags when posting code to the boards on this site.

Second, main() should have return type int, not void.

Third, you should be using the iostream header and a using statement like : using namespace std;, assuming your compiler is compliant with that syntax.

Fourth, You have to decide what you are trying to do. If you are trying to split the string in two irrespective of the string content, then your code is on the right track. If want to split the string on any given m within the string then you could use something like strtok(). Or, I you are trying to isolate a substring, in this case moorthy from the original string, then you could use strstr(). There are alternatives to strtok() and strstr() if you use std::strings rather than C style strings.

Next, the variable s1 in main() is never given a value in main() before you call the operator / on it in line 36 in main(). That might be okay if operator / never tried to access any member variable of the s1 from main(), but it does, when you try to output the value of ch in lines 22 and 28. Therefore, the output of each ch is going to be junk.

Also, be careful with your variable naming conventions. For example, don't use variables by the name of l. Sooner or later it will be mistaken for 1. Second, get() is a standard function so you would be well advised to name your function something else, say maybe getString() or getCh(). Then you unnecessarily complicate things by using a variable name in line 15 that you also use in main(). That is, the variable s1 in line 15 is actually a copy of the variable s2 from main() in this program, and not the same variable as the variable s1 in main(), though it took me a little time rumblin tings roun 'n me noggin to work that'n out.

Last, it doesn't make much difference in this code so far, but you should be aware that given an array as member variable of your user defined type you really should write your own version of a copy constructor and not rely on the compilers version.

This article has been dead for over six months. Start a new discussion instead.