🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #448

Re: Program crashes at end

#448
From: Neal E. Coombes
Date: Tue, 28 May 2002 07:14
198 lines
5231 bytes
SKAS wrote:
> 
> 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>

#include <iostream>
#include <cstdio>
#include <cstring>
// no such beast as conio.h in standard C++ 

> class string
> {
>  private:
>       char *head;
>       int len;
>  public:
>       string()
>    {
>    }
> 
>   string(int l)
>   {
>    len=l;
>    head=new char[len];
>   }
> 
>   string(char a[])
>   {
>    strcpy(head,a);  // OUCH: you forgot to allocate memory!
>   }

How about:
    string(char a[])
    {
      head = new char[strlen(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)

int main()  // no such thing as void main()!

> {
>  char sub[30],j;

out of curiosity, what's j for?

HTH
Neal

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

-- 
char*a(char*b,char c){while(*b!=c&&*b)++b;return b;}int main(){\
char*d=".CEfmc oliaNbkHrse\n", *e="lNoCs\nofrHli\nlbC  \nl cscs\
c\nbsHNa",*f=a(d,'a'),*g=e;int putchar(),h=f-d;while(*e++){if(!\
*e)e=g;h+=a(d,*e++)-f;putchar(d[h]);if(!*e)e=g;++e;}return 0;}
Technical Details
Message-ID: <3CF32E4B.5F6E812E@telus.net>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!cyclone.bc.net!newsfeed.telusplanet.net!news0.telusplanet.net.POSTED!53ab2750!not-for-mail
References: <acv9rh$sopon$1@ID-31123.news.dfncis.de>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 109fba,807a736b2ed72881
X-Google-Attributes: gid109fba,public
X-Google-Thread: 114af8,807a736b2ed72881
X-Google-Attributes: gid114af8,public
X-Google-Thread: 114592,807a736b2ed72881
X-Google-Attributes: gid114592,public
X-Google-ArrivalTime: 2002-05-28 00:14:22 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!cyclone.bc.net!newsfeed.telusplanet.net!news0.telusplanet.net.POSTED!53ab2750!not-for-mail
Message-ID: <3CF32E4B.5F6E812E@telus.net>
From: "Neal E. Coombes" <Neal.Coombes@telus.net>
Organization: http://www.coombez.net/
X-Mailer: Mozilla 4.76 [en] (Win98; U)
X-Accept-Language: en-GB,en
MIME-Version: 1.0
Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.turbocpp,comp.lang.c++
Subject: Re: Program crashes at end
References: <acv9rh$sopon$1@ID-31123.news.dfncis.de>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 198
Date: Tue, 28 May 2002 07:14:21 GMT
NNTP-Posting-Host: 209.53.14.76
X-Trace: news0.telusplanet.net 1022570061 209.53.14.76 (Tue, 28 May 2002 01:14:21 MDT)
NNTP-Posting-Date: Tue, 28 May 2002 01:14:21 MDT