can anyone debug this c++ program asap pls

rbk

Disciple
I have my Object Oriented Programming lab exam tomorrow. Hence I'm trying out some programs.
This program is to concatenate Strings using dynamic memory allocation.
I don't get a compiler error but when I run it it doesn't give output.
Please anyone find out the error & tell me asap before tomorrow morning..

Code:
#include<iostream.h>
#include<string.h>
class stringcon
{
int length;
char *name;
public:
stringcon()
{
length=0;
name=new char[length+1];
}
stringcon(char *s)
{
length=strlen(s);
name=new char[length+1];
}
void display(void)
{
cout<<name<<endl;
}
void join(stringcon &a, stringcon &b)
{
length=a.length+b.length;
delete name;
name=new char[length+1];
strcpy(name,a.name);
strcat(name,b.name);
}
};
int main()
{
stringcon name1("New"),name2("Delhi"),s1;
name1.display();
name2.display();
s1.join(name1,name2);
s1.display();
return 0;
}
 
look at the constructor accepting the char * as a parameter
you are allocating memory for the char * s
but i do not see the program copying it into the just allocated memory .

string operations depend on the NULL termination.
name1 and name2 (\0 terminated string) dun have them hence the crash ...
you need to strcpy after the new allocation in the constructor stringcon(char *s)

hope this helped ...
also ... debugging using a debugger helps in these situations as you might not have any syntactical issues ...
 
Thanks!!!.You r right .I had to use strcpy . had missed it.

and can u suggest any debuggers?
I use Turbo C++ 3.0 for compiling,running the programs.
 
Turbo 3.0!!! Thats like 16 years old. :p

Get something like Visual Studio Express, or better still emacs/vi + gcc on cygwin and compile using -Wall. That is my preferred method for windows. Even better would be to get Linux of course. :)
 
@ru121279 ,the exam went well. I got simple programs. A C++ program to illustrate the concept of unary operator overloading and a JAVA program to illustrate the concept of multi threading. :)

@vNerd

I do have linux .I'm having Suse 10.2 . I type the program in kwrite and run it at the konsole by giving cc filename.cpp ???

and in windows I tried out bloodshed dev C++ but didn't like it much. It keeps giving some warning (but turbo 3.0 says warnings 0 errors 0).
 
@rbk : gr8 man ...

agree with vNerd ...
Visual Studio is good on windows ...
have also heard of Eclipse (with a C++ plugin)

On Linux nothing beats vi(m) or emacs
gcc/g++ is also gr8

If you need an IDE try anjuta its got a good IDE
also eclipse is available on Linux.

debugging gdb rocks ... need a GI (graphical interface) there is gvd , ddd but i was never confortable with the visual interfaces ever ..
 
What are the warnings bloodshed gives you? I can guess as to what they are, but it would be nice to know.

Also, how on earth did you get operator overloading working in TCC? I didn't know TCC even had that capability... if you want to learn C++, you really need to move away from TCC.

g++ is ok. Eclipse with CDT is also just ok... nothing like Eclipse for Java.

I use emacs while doing C++ coding.

Personally I suggest using Visual Studio while doing C++ programming. It is a damned good IDE, and for the most part is compliant with the C++ standards (as much as gcc anyway). There are deviations maybe, but I have my doubts as to whether a compliant C++ compiler actually exists (maybe Intel has one..)
 
Back
Top