GIDForums

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Mar-2008, 11:41
takachi takachi is offline
Junior Member
 
Join Date: Dec 2007
Posts: 71
takachi is an unknown quantity at this point

I have a segmentation error in my program


my program consists of two parts.class word is already tested and works fine yet, class Sentence isn't tested yet.I had tried to test in this main function the addWord function and the << operator which means print.however,at the end of the program runtime I get a segmentation fault.

this is the main:

CPP / C++ / C Code:
#include <iostream>
#include <cstring>
#include "Word.h"
#include "Sentence.h"

int main()
{
	Sentence s;
	char str[80];
                                cin>>str;
                                Word w(str),k("eli");
                                s.addWord(w);
                               s<<s;
                               cout<<endl;
                               s.addWord(k);
                               s<<s;
                               cout<<endl;

	return 0;
}


this is the class Word.h
CPP / C++ / C Code:
#include <iostream>
#include <cstring>

#ifndef STUDENT_H
#define STUDENT_H

class Word
{
	private:
		char *letters;

	public:
		Word(char str[80])
		{
			int i,errorF=0,size;
			for (i=0;i<strlen(str);i++)
			{
				if ((str[i]<65)||(((str[i]>90)&&(str[i]<97)))||(str[i]>122))
					errorF=1;
			}
			if (errorF==0)
			{
				size=strlen(str);
				letters=new char[size];
				strcpy(letters,str);
			}
			else	
				letters=NULL;
		}

		Word(const Word &word)
		{	
			int size;
			if (word.letters!=NULL)
			{	size=strlen(word.letters);
                     		letters=new char[size];
              			strcpy(letters,word.letters);
			}
			else 
				letters=NULL;
		}
		
		~Word()
		{
			if (letters!= NULL)
				delete [] letters;
		}
		char * getletters();
		Word& operator = (const Word &word);
		Word operator * (const Word &word);
		void operator *= (const Word &word);
		void operator ! ();
		bool operator == (char str[80]);
		int checkOrder1(char str[80]);
		int checkOrder2(char str[80]);
		bool operator < (char str[80]);
		bool operator > (char str[80]);
		char operator [ ] (int place);
		void operator << (const Word &word);
};

#endif



the class Word.cpp
CPP / C++ / C Code:
#include <iostream>
#include <cstring>

#include "Word.h"
using std::cout;
using std::endl;

char * Word::getletters()
{
	return letters;
}

Word& Word::operator = (const Word &word)
{
	int size;
	if (word.letters!=NULL)
	{	
		if (letters!= NULL)
			delete [] letters;
		size=strlen(word.letters);
        	letters=new char[size];
              	strcpy(letters,word.letters);
	}
	else 
		letters=NULL;

	return *this;
}

Word  Word::operator*(const Word &word)
{
	char str[80];
	int size,i,j;
	size=strlen(letters)+strlen(word.letters);
	for(i=0;i<strlen(letters);i++)
		str[i]=letters[i];
	for(j=i;j<size;j++)
		str[j]=word.letters[j-i];
	str[j]='\0';
	Word newWord(str);
	return newWord;
}

void Word::operator *= (const Word &word)
{
	char buffer[80];
        int size,i,j;
        size=strlen(letters)+strlen(word.letters);
	strcpy(buffer,letters);
	if (letters!= NULL)
        	delete [] letters;	
	letters=new char[size];
        for(i=0;i<strlen(buffer);i++)
                letters[i]=buffer[i];
        for(j=i;j<size;j++)
                letters[j]=word.letters[j-i];
        letters[j]='\0';
}

void Word::operator ! ()
{
	char buffer[80];
	int i,j=0;	
	strcpy(buffer,letters);
	for(i=strlen(buffer)-1;i!=-1;i--)       
        {	letters[j]=buffer[i];
		j++;
	}
	letters[j]='\0'; 
}

bool Word::operator == (char str[80])
{
	int i,flagError=0;
	if (strlen(letters)==strlen(str))
	{
		for(i=0;i<strlen(letters);i++)
		{
			if ((letters[i]!=str[i])&&(letters[i]!=str[i]-32)&&(letters[i]!=str[i]+32))
				flagError=1;
		}
	}
	else
		flagError=1;
	if (flagError==1)
		return false;

	else
		return true;
}

int Word::checkOrder1(char str[80])
{	
	int i,flag;
	for (i=0;i<strlen(str)||(letters[i]>str[i])||(letters[i]<str[i]);i++)
                {
                        if (letters[i]>str[i])
                                flag=1;
                        else
                                flag=0;
                }
	return flag;
}

int Word::checkOrder2(char str[80])
{
        int i,flag;
        for (i=0;i<strlen(letters)||(letters[i]>str[i])||(letters[i]<str[i]);i++)
                {
                        if (letters[i]>str[i])
                                flag=1;
                        else        
                                flag=0;
                }
        return flag;
}

bool Word::operator < (char str[80])
{
	if (strlen(letters)>strlen(str))
	{
		if (checkOrder1(str)==1)
			return true;
		else
			return false;
	}
	else
	{
                if (checkOrder2(str)==1)
                        return true;
                else
                        return false;
        }
}

bool Word::operator > (char str[80])
{
        if (strlen(letters)>strlen(str))
        {
                if (checkOrder1(str)==1)
                        return false;
                else
                        return true;
        }
        else
        {
                if (checkOrder2(str)==1)
                        return false;
                else
                        return true;
        }
}

char Word::operator [ ] (int place)
{
	if ((place<0)||(place>strlen(letters)))
		return '!';
	else
		return letters[place];
}

void Word::operator << (const Word &word)
{
	if (letters!=NULL)
		cout<<word.letters;
}	


the class Sentence.h
CPP / C++ / C Code:
#include <iostream>
#include <cstring>
#include "Word.h"

#ifndef SENTENCE_H
#define SENTENCE_H

class Sentence
{
        private:
                Word **sentence;
		int numOfWords;
        public:
               Sentence() {sentence=NULL;}
		
		~Sentence()
		{
			int i;                      
       			for (i=0;i<numOfWords;i++)
           			delete sentence[i];
			delete [] *sentence;
		}
		void addWord(Word w);
		int findWord(char *str);
		void removeWord(char *str);
		void switchWords(int place1,int place2);
		void Sort();
		void concat(int place1,int place2);
		void newConcat(int place1,int place2);
		void reverse(int place);
		void copy(int place1,int place2);
		char getChar(int senIndex,int wordIndex);
		Word  operator [ ] (int place);
		void operator >> (Sentence &sentence);
		void operator << (Sentence &sentence);

};

#endif


the class Sentence.cpp
CPP / C++ / C Code:
#include <iostream>
#include <cstring>

#include "Word.h"
#include "Sentence.h"
using std::cout;
using std::endl;

void Sentence::addWord(Word w)
{
	Word **temp,*word;
	int i;
	word=new Word(w.getletters());
	if (numOfWords==0)
	{
		sentence=new Word*;
		*sentence=word;
		numOfWords=1;
	}
	else
	{
		numOfWords++;
		temp=new Word*[numOfWords];	
		for (i=0;i<numOfWords-1;i++)
        	 	temp[i]=sentence[i];
     		temp[i]=word;    
     		delete [] sentence;
     		sentence=temp;
	}
}

int Sentence::findWord(char *str)
{
	int i,returnFlag=-1;
	for(i=0;i<numOfWords;i++)
	{
		if (sentence[i]->getletters()==str)
			returnFlag=i;
	}
	return returnFlag;
}

void Sentence::removeWord(char *str)
{
	if (findWord(str)!=-1)
   	{
      		int place=findWord(str),i,size;
      		Word **temp;
      		numOfWords--;
      		temp=new Word*[numOfWords];
      		for (i=0;i<place;i++)
      			temp[i]=sentence[i];
      		for (i=place+1;i<=numOfWords;i++)
      			temp[i-1]=sentence[i];
     		delete [] sentence;
     		sentence=temp;      
   	}
}  	
void Sentence::switchWords(int place1,int place2)
{
	int i;
	Word **temp;
      	temp=new Word*[numOfWords];
      	for (i=0;i<place1;i++)
        	temp[i]=sentence[i];
     	temp[i]=sentence[place2];
        for (i=place1+1;i<place2;i++)
             	temp[i]=sentence[i];
      	temp[i]=sentence[place1];
     	for (i=place2+1;i<numOfWords;i++)
     		temp[i]=sentence[i];
      	delete [] sentence;
      	sentence=temp;
} 
void Sentence::Sort()
{
	int i,j;
	for(i=0;i<numOfWords;i++)
	{
		for(j=0;j<numOfWords;j++)
		{
			if (((sentence[i]->getletters()>sentence[j]->getletters())==true)&&(i<j))
				switchWords(i,j);
		}
	}
}
					
void Sentence::concat(int place1,int place2)
{	
	if ((place1>=0)&&(place1<numOfWords)&&(place2>=0)&&(place2<numOfWords))
	{
		*sentence[place1]*=*sentence[place2];
	}
}

void Sentence::newConcat(int place1,int place2)
{
	
        if ((place1>=0)&&(place1<numOfWords)&&(place2>=0)&&(place2<numOfWords)) 
        {
                Word newWord=*sentence[place1]**sentence[place2];
		addWord(newWord);
        }
} 

void Sentence::reverse(int place)
{
	if ((place>=0)&&(place<numOfWords))
	{
		!sentence[place];
	}
}

void Sentence::copy(int place1,int place2)
{
	if ((place1>=0)&&(place1<numOfWords)&&(place2>=0)&&(place2<numOfWords))
        {
		sentence[place1]=sentence[place2];
	}
}

char Sentence::getChar(int senIndex,int wordIndex)
{
	char *s;
	s=sentence[senIndex]->getletters();
	return s[wordIndex];	
}

Word  Sentence::operator [ ] (int place)
{
        if ((place>=0)&&(place<numOfWords))
        {
        	return *sentence[place];
        }
/*	else 
		return "error";
*/}

void Sentence::operator >> (Sentence &sentence)
{
	int i,j;
	char *str,temp[80];
	cin>>str;
	for (i=0;i<strlen(str);i++)
	{
		for (j=i;j!=' ';j++)
			temp[j-i]=str[j];
		temp[j-i]='\0';
		Word w(temp);
		addWord(w);
		i=j+1;
	}
					
}

void Sentence::operator << (Sentence &sentence)
{
	if (numOfWords!=0)
	{
		int i=0;
		
		  for (i=0;i<numOfWords-1;i++)
		   {
		    	sentence[i]<<sentence[i];
			    cout<<" ";
		     }
     
		sentence[i]<<sentence[i];
		cout<<endl;
	}
	else
		cout<<"empty";
}

  #2  
Old 26-Mar-2008, 11:45
fakepoo fakepoo is online now
Regular Member
 
Join Date: Oct 2007
Posts: 395
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: I have a segmentation error in my program


One problem that I saw immediately is that when you allocate memory for a word, you allocate strlen() number of bytes. You actually need strlen()+1 in order to hold the '\0' NULL-terminating character.
  #3  
Old 26-Mar-2008, 19:09
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 348
Peter_APIIT is on a distinguished road

Re: I have a segmentation error in my program


Another things i saw from this code is passing array as arguments,

CPP / C++ / C Code:
bool operator == (char str[80]);
		int checkOrder1(char str[80]);
		int checkOrder2(char str[80]);
		bool operator < (char str[80]);
		bool operator > (char str[80]);

This is a ugly style which i always avoided from.

I rather will do ,
CPP / C++ / C Code:
#define size 80
int checkOrder1(char str[], int size);

Please correct me if i worng.

I hope this help.
__________________
Linux is the best OS in the world.
  #4  
Old 26-Mar-2008, 19:58
C++_Bandit's Avatar
C++_Bandit C++_Bandit is offline
Junior Member
 
Join Date: Mar 2008
Location: Virginia
Posts: 40
C++_Bandit will become famous soon enough

Re: I have a segmentation error in my program


Just to give you a little background (I haven't looked at your code yet) a segmentation fault occurs when you have a pointer that is null being used, it is uninitialized, or you have deleted it twice (yes this can occur). Or you have an array that is accessing something outside of the memory that has been allocated to it. Check your code to see if any of these have occurred.

Hope this helps a bit.
  #5  
Old 27-Mar-2008, 08:35
L7Sqr L7Sqr is offline
Junior Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 91
L7Sqr will become famous soon enough

Re: I have a segmentation error in my program


Quote:
Originally Posted by Peter_APIIT
I rather will do ,
CPP / C++ / C Code:
#define size 80
int checkOrder1(char str[], int size);
Eeeek!
If you define the string 'size' to be 80 and then use that name as an argument to a function it will be replaced by the preprocessor and you should see an error similar to
Quote:
error: syntax error before numeric constant
From your compiler because it sees
CPP / C++ / C Code:
int checkOrder (char str[], int 80);

By convention, macros (or defined constants) are usually all capital letters.
Also, there is nothing wrong with passing only an array as an argument, especially if the size is not needed.

Of course, the part about the array is just my opinion - you are free to dismiss.
  #6  
Old 27-Mar-2008, 09:47
takachi takachi is offline
Junior Member
 
Join Date: Dec 2007
Posts: 71
takachi is an unknown quantity at this point

Re: I have a segmentation error in my program


thank you for your help,the problem was with my destructor.
 

Recent GIDBlogMaster?s Degree by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Employee program segmentation error. ShadowTH CPP / C++ Forum 1 08-Dec-2007 13:28
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 10:13
Help with a complex program lordfuoco CPP / C++ Forum 5 24-Jun-2006 06:03
Dynamic memory, loops, and segmentation faults ubergeek CPP / C++ Forum 2 07-May-2005 16:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:09.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.