Thread: Coding problem

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    11

    Coding problem

    HI I have worte a code and the code looks a bit messed I need to simplify it. I need to insert std::string in the program too but I am not sure whether to do it or not can someone please guide me through please....
    Code:
    #ifndef RSTRING_H
    #define RSTRING_H
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <cmath>
    ;
    
    class RString{
    	char* name;
    	int length;
    	int getLength()
    { 
    return length;
    }
    	char* getName()
    {
    return name;
    }
    public:
    	RString()
    {
    name="";
    length=0;
    }
    	RString(const char* S)
          {
    	length=strlen(S) + 1;
    	name = new char[length];
    	strcpy(name,S);
    	}
    	RString(char * a,char * b)
    {
    	length=strlen(a)+strlen(b)+1;
    	name=new char[length];
    	strcpy(name,a);
    	strcat(name,b);
    	}
    	RString(int value){
    	int i= (int)(log10(value)+1.0);
    	length=i;
    	name=new char[i+1];
    	itoa((int) value,name,10);
    	}
    
    	void add(char *a){
    		char* tmp=new char[length+1];
    		strcpy(tmp,name);
    		delete name;
    		length+=+strlen(a);
    		name=new char[length+1];
    		strcpy(name,tmp);
    		strcat(name,a);
    		delete [] tmp;
    	}
    	
    	void add(int value){
    		char* tmp=new char[length+1];
    		strcpy(tmp,name);
    		delete name;
    	 	int i= (int)(log10(value)+2.0);
    		length+=i;
    		name=new char[length+1];
    		char* iSt=new char[i];
    		itoa((int) value,iSt,10);
    		strcpy(name,tmp);
    		strcat(name,iSt);
    		delete [] tmp;
    	}
    	RString(RString* a,RString* b)
    {
    		int len=a->getLength()+b->getLength();
    		if(len>100)
    {
    			cerr<<"String too long;";
    			exit(-1);
    		}
    		char tmp[100];
    		strcpy(tmp,a->getName());
    		strcat(tmp,b->getName());
    		name=tmp;
    		length=len;
    	}
    	
    	~RString() { delete name; }
    
    	char * print(){return name;}
    }
    
    ;
    endif

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What do you mean by "insert std::string into the program"?

    Better formatting of the posted code would help a bit.


    This:
    Code:
    ~RString() { delete name; }
    Should be:
    Code:
    ~RString() { delete [] name; }

    This:
    Code:
    #include <stdlib.h>
    Should be:
    Code:
    #include <cstdlib>
    Although I don't really see why you really need that at all for the code shown.


    Given all the C-style string function calls you probably also want to have:
    Code:
    #include <cstring>

    This:
    Code:
    int getLength()
    { 
        return length;
    }
    Should be a const function:
    Code:
    int getLength() const
    { 
        return length;
    }

    These:
    Code:
    char* getName()
    {
    return name;
    }
    
    char * print(){return name;}
    Are basically the same except for the name and one being public and the other private. Returning a straight char* can be dangerous, you're exposing the classes internals to direct manipulation by the user. A const char* would be somewhat better.



    Code:
    #ifndef RSTRING_H
    #define RSTRING_H
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <cmath>
    ;
    
    ...
    
    }
    
    ;
    endif
    Typos?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    11
    Hi,

    Thanks for replying actually this code was written in I dont know what language! I have to rectify this code and make it into C++ for better understanding and i am clueless and dont knw from where should I start? Thats y i need ur help

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    11
    Quote Originally Posted by shady_shockerz View Post
    Hi,

    Thanks for replying actually this code was written in I dont know what language! I have to rectify this code and make it into C++ for better understanding and i am clueless and dont knw from where should I start? Thats y i need ur help
    Another Issue is
    Code:
    int main()
    {
        RString s("abc");
        RString t = s;
    }
    This two line program has a double deletion error when the program terminates. This three line program has both a double deletion error and memory leaks:
    Code:
    int main()
    {
        RString s("abc");
        RString t("123");
        t = s;
    }
    Not being able to do simple copies makes a string class basically useless. But I dont know how to solve the problem. Can someone help me with this one

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Can't you just use std::string instead of RString, and therefore not worry about converting it. Why do you need RString?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shady_shockerz
    Not being able to do simple copies makes a string class basically useless. But I dont know how to solve the problem. Can someone help me with this one
    Since your RString class performs manual memory management, you should provide the copy constructor, copy assignment operator and destructor. You provided the destructor (though implemented incorrectly, as hk_mp5kpdw pointed out), but did not provide (or disable, but that does not make sense here) the other two.

    I note that your add() member functions are a little too complicated: you only need to use new[] and delete[] once.

    You also have another problem where if you default construct an RString object and it is destroyed immediately after, you would then attempt to use delete[] on a pointer that points to a string literal ("").
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    11
    Hi Laserlight,

    I saw a interesting observation but I dont know how to implement it can you please elobarate how to do it??

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shady_shockerz
    I saw a interesting observation but I dont know how to implement it can you please elobarate how to do it??
    As you have been told in another forum, you need to go through at least one good book on C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 16
    Last Post: 01-26-2006, 02:38 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM