consider the following specification file that simulates the string class library "string.h":
//i named it aa.h
#include<iostream>
#include<cstring>
using namespace std;
const int SIZE=100;
class MyString
{
public:
MyString();
MyString(char arr[]);
MyString operator+(MyString oString);//concatenation
bool operator==(MyString oString);//are equal strings
int MyString::length() const;//returns the number of characters stored in the object.
int Find(char arr[]) const;// finds the position of the first occurence for the paseed string
MyString SubStr(int start, int length);//extarcts a substring from the current string
void Swap(MyString oString);//exchanges the values of the strings
private:
char text[SIZE];
};
i wrote the client code, the implementation file, no errors, and yet, the program faces a problem in executing it..here are my codes:
//implementation file:
#include "aa.h"
MyString::MyString()
{
for (int i=0;i<SIZE;i++)
text[i]=0;}
cout<<"enter your string"<<endl;
char a[100];
cin.get(a,100);
MyString x=MyString();
MyString y=MyString(a);
x.operator+(y);
if(x.operator==(y))
cout<<"they are equal"<<endl;
else
cout<<"not equal"<<endl;
cout<<"the length of string= "<<x.length()<<endl;
cout<<"the position of the first occurence= "<<x.Find(a)<<endl;
cout<<"enter your start and length"<<endl;
int start, lengthh;
cin>>start>>lengthh;
x.SubStr(start,lengthh);
x.Swap(y);
return 0;
}
consider the following specification file that simulates the string class library "string.h":
//i named it aa.h
Code:
#include<iostream>
#include<cstring>
using namespace std;
const int SIZE=100;
class MyString
{
public:
MyString();
MyString(char arr[]);
MyString operator+(MyString oString);//concatenation
bool operator==(MyString oString);//are equal strings
int MyString::length() const;//returns the number of characters stored in the object.
int Find(char arr[]) const;// finds the position of the first occurence for the paseed string
MyString SubStr(int start, int length);//extarcts a substring from the current string
void Swap(MyString oString);//exchanges the values of the strings
private:
char text[SIZE];
};
i wrote the client code, the implementation file, no errors, and yet, the program faces a problem in executing it.
Well, to fix the problems "in executing" you have to use the debugger, run your code step by step and see what and where goes wrong.
Besides, better approach would be passing your MyString instance in some of the class methods by const reference to prevent unneeded copying. example:
I think you would benefit from taking this slow, coding one method at a time and testing it properly before going on to the next one. For example, from a quick look this code:
has three errors in it, so there are no doubt numerous bugs in the more complicated methods. Run this method through the debugger (plus look at the compiler warnings as mine - g++ - gives one for the 'for' line) and check whether the 'text' array ends up as it should.
I think you would benefit from taking this slow, coding one method at a time and testing it properly before going on to the next one. For example, from a quick look this code:
has three errors in it, so there are no doubt numerous bugs in the more complicated methods. Run this method through the debugger (plus look at the compiler warnings as mine - g++ - gives one for the 'for' line) and check whether the 'text' array ends up as it should.
Hi!
thank you so much! yes trying to code one method at a time has showed me where exactly the program got stuck, actually invoking the constructor and how i implemented them was the major problem . thanks again
Bookmarks