Article View: alt.comp.lang.learn.c-c++
Article #451Re: Program crashes at end
From: Dan Cernat
Date: Tue, 28 May 2002 12:45
Date: Tue, 28 May 2002 12:45
94 lines
3301 bytes
3301 bytes
"SKAS" <f93c0532@yahoo.com> wrote in message news:<acv9rh$sopon$1@ID-31123.news.dfncis.de>...
> I've written a class for string datatype. The program runs fine but crashes
> in the end . However if I initialize the variable po in the main function
> int i, po= 8;) instead of getting it from the user (cin >>po) the program
> exits nicely and doesn't crash.
> I'll be very grateful for any assistance. I'm using turbo c++ 3.0.
>
> Thanks
>
>
> #include <iostream.h>
> #include <conio.h>
> #include <stdio.h>
> #include <string.h>
>
> class string
> {
> private:
> char *head;
> int len;
> public:
> string()
> {
> }
^^^^^^^ initialize head in the default constructor:
string():head(0) {}
>
> string(int l)
> {
> len=l;
> head=new char[len];
> }
>
> string(char a[])
> {
> strcpy(head,a);
^^^^^^^^^ trouble - head is not initialized before strcpy
> }
string(const char* a)
{
l = strlen(a);
head = new char[l + 1];
strcpy(head, a);
}
>
> ~string()
> {
> delete head;
> }
// code snipped
>
> void main(void)
> {
> char sub[30],j;
> int i,po;
> string s1("the 1st and most IMPORTANT thing");
^^^^^^^^^^^^^^^^^ call to the trouble constructor
> clrscr();
> s1.print();
> cout<<endl<<"The length of string (including null character)=
> "<<s1.length();
> cout<<"\nEnter the index for a single character value: ";
> cin>> po; /* NOTE : IF INITIALIZE PO ABOVE INSTEAD OF GETTING IT FROM THE
> USER. PROGRAM WORKS FINE*/
> cout<<"\nThe character at position "<<po<<" is \""<<s1.atindex(po)<<"\"";
> s1.upper();
> cout<<"\nThe string converted to upper case: ";
> s1.print();
> s1.lower();
> cout<<"\nThe string converted to lower case: ";
> s1.print();
> s1.proper();
> cout<<"\nThe string converted to proper case: ";
> s1.print();
> cout<<"\nEnter a substring to be searched in the main string: ";
> gets(sub);
> i=s1.substring(sub);
> if (i != -1)
> cout<<"The position of \""<<sub<<"\" is at index = "<<i;
> else
> cout<<"Substring not found!!!";
> getch();
> }
po has nothing to do with it. it was just a side effect.
Dan
Technical Details
Message-ID:
<aab4eb08.0205280445.16d48e41@posting.google.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mailReferences:
<acv9rh$sopon$1@ID-31123.news.dfncis.de>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 114af8,807a736b2ed72881 X-Google-Attributes: gid114af8,public X-Google-Thread: 114592,807a736b2ed72881 X-Google-Attributes: gid114592,public X-Google-Thread: 109fba,807a736b2ed72881 X-Google-Attributes: gid109fba,public X-Google-ArrivalTime: 2002-05-28 05:45:25 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail From: dcernat@excite.com (Dan Cernat) Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.turbocpp,comp.lang.c++ Subject: Re: Program crashes at end Date: 28 May 2002 05:45:25 -0700 Organization: http://groups.google.com/ Lines: 94 Message-ID: <aab4eb08.0205280445.16d48e41@posting.google.com> References: <acv9rh$sopon$1@ID-31123.news.dfncis.de> NNTP-Posting-Host: 204.92.66.10 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1022589925 7960 127.0.0.1 (28 May 2002 12:45:25 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 28 May 2002 12:45:25 GMT