[RESOLVED] help C++ code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] help C++ code

  1. #1
    Join Date
    Oct 2011
    Posts
    3

    [RESOLVED] help C++ code

    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;}

    MyString::MyString(char arr[])
    {
    for(int i=0; i!='/0'; i++)
    text[i]=arr[i];
    }

    MyString MyString:perator+(MyString oString)
    {
    MyString a;
    strcpy(a.text,strcat(text,oString.text));

    return a;

    }
    bool MyString:perator==(MyString oString)
    {
    for(int i=0;i!='/0';i++)
    if(oString.text[i]==text[i])
    return true;
    else
    return false;
    }
    int MyString::length() const
    {
    int a=strlen(text);
    return a;
    }

    int MyString::Find(char arr[]) const
    {
    int o=0;
    int i=0;
    while(arr[i]!='/0'){
    for( ; arr[i]!=text[i];i++)
    o++;
    if(arr[i]==text[i])
    i++;
    else
    {o=0;
    break;}
    }

    return o;
    }

    MyString MyString::SubStr(int start, int length)
    {
    MyString a;
    int j=0;
    for(int i=start;i<=length; i++)
    a.text[j]=text[i];
    cout<<"the substring is: "<<a.text<<endl;
    text[start]=text[length+1-length];
    text[start+1]='/0';
    return a;


    }


    void MyString::Swap(MyString oString)
    {
    MyString t;
    for(int i=0;i!='/0';i++)
    {t.text[i]=text[i];
    text[i]=oString.text[i];
    oString.text[i]=t.text[i];

    }}

    //client code:

    # include "aa.h"
    int main()
    {

    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;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: help C++ code

    Quote Originally Posted by aynsar View Post
    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:
    Code:
    	MyString operator+(const MyString &oString);//concatenation
    	bool operator==(const MyString &oString);//are equal strings
    And, please, next time use Code tags while posting code snippets. Read Announcement: Before you post....
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2009
    Posts
    596

    Re: help C++ code

    Hi,

    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:

    Code:
    MyString::MyString(char arr[])
    {
    for(int i=0; i!='/0'; i++)
    text[i]=arr[i];
    }
    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.

  4. #4
    Join Date
    Oct 2011
    Posts
    3

    Re: help C++ code

    Quote Originally Posted by VictorN View Post
    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:
    Code:
    	MyString operator+(const MyString &oString);//concatenation
    	bool operator==(const MyString &oString);//are equal strings
    And, please, next time use Code tags while posting code snippets. Read Announcement: Before you post....
    thanks a lot! i sure will use the code tags next time

  5. #5
    Join Date
    Oct 2011
    Posts
    3

    Re: help C++ code

    Quote Originally Posted by Peter_B View Post
    Hi,

    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:

    Code:
    MyString::MyString(char arr[])
    {
    for(int i=0; i!='/0'; i++)
    text[i]=arr[i];
    }
    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center