Article View: alt.comp.lang.learn.c-c++
Article #447Program crashes at end
From: SKAS
Date: Tue, 28 May 2002 07:53
Date: Tue, 28 May 2002 07:53
170 lines
4143 bytes
4143 bytes
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()
{
}
string(int l)
{
len=l;
head=new char[len];
}
string(char a[])
{
strcpy(head,a);
}
~string()
{
delete head;
}
int length();
char atindex(int);
int substring(char []);
void print();
void concatenate(char []);
void upper();
void lower();
void proper();
int compare(char []);
};
int string::length()
{
int index=0;
while (*(head+index))
index++;
len=++index;
return index;
}
void string::print()
{
int index=0;
while (*(head+index))
{
cout<<*(head+index);
index++;
}
}
char string::atindex(int index)
{
return *(head+index);
}
void string::upper()
{
int index=0;
while (*(head+index))
{
if ((*(head+index)>=97) && (*(head+index)<=122))
*(head+index)-=32;
index++;
}
}
void string::lower()
{
int index=0;
while (*(head+index))
{
if ((*(head+index)>=65) && (*(head+index)<=90))
*(head+index)+=32;
index++;
}
}
void string::proper()
{
int index=0;
while (*(head+index))
{
if (((index==0)||(*(head+index-1)==32))&&((*(head+index)>=97) &&
(*(head+index)<=122)))
*(head+index)-=32;
else if ((*(head+index)>=65) && (*(head+index)<=90))
*(head+index)+=32;
index++;
}
}
int string::substring(char a[])
{
int index=0,i,flag=-1;
while (*(head+index))
{
if(*(head+index) == *a)
{
flag=index;
i=1;
while (*(a+i))
{
if (*(head+index+i) != *(a+i))
{
flag=-1;
break;
}
else
i++;
}
}
if (flag != -1)
return flag;
else
index++;
}
return flag;
}
void main(void)
{
char sub[30],j;
int i,po;
string s1("the 1st and most IMPORTANT thing");
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();
}
Technical Details
Message-ID:
<acv9rh$sopon$1@ID-31123.news.dfncis.de>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.freenet.de!fu-berlin.de!uni-berlin.de!202.63.218.71!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 114af8,807a736b2ed72881,start X-Google-Attributes: gid114af8,public X-Google-Thread: 114592,807a736b2ed72881,start X-Google-Attributes: gid114592,public X-Google-Thread: 109fba,807a736b2ed72881,start X-Google-Attributes: gid109fba,public X-Google-ArrivalTime: 2002-05-27 23:58:59 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.freenet.de!fu-berlin.de!uni-berlin.de!202.63.218.71!not-for-mail From: "SKAS" <f93c0532@yahoo.com> Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.turbocpp,comp.lang.c++ Subject: Program crashes at end Date: Tue, 28 May 2002 12:53:55 +0500 Lines: 172 Message-ID: <acv9rh$sopon$1@ID-31123.news.dfncis.de> NNTP-Posting-Host: 202.63.218.71 X-Trace: fu-berlin.de 1022569138 30172951 202.63.218.71 (16 [31123]) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200