My CPP program gets abruptly terminated.....

I think this happens because by some part of the program itself, the TC.exe gets overwritten and thats why it gets terminated.....


Can any one help me out????

I'm more inclined to assume that your code is broken. Post a complete program that crashes and we'll be able to help you.

Why, when, in which situation and/or IDE you run your application?
Is it a console application or a GUI application using MFC?
Please clarify your question or provide some code.

600) 1(_)[|<.

I m talking about all the programs i have made and I use Turbo C compiler...

I am appending the whole code...... Check it out...

//Program implementing a string class and all the related
//operations...

#include "iostream.h"
#include "conio.h"


class strings //Class definition
{
	char *string; //for the string data

	public:
		int len;      //for lenght of string

		strings() //Default constructor
		{*string = NULL;len = 0;}

		strings(char *a) //Parameterized constructor
		{
			len = str_len(a);
			string = new char[len+1];
			while (*a)
			{
				*string = *a;
				string++;
				a++;
			}
			*string = '\0';
			string -= len;
		}

		~strings() //Destructor freeing memory
		{delete []string;}

		void getdata() //When the entry is going to be taken from user
		{
			delete [] string;
			char *a;
			cout << endl << endl <<"Enter the string to be operated upon (Maxlength = 100): ";
			cin.getline (a,100);
			len = str_len(a);
			string = new char[len+1];
			while (*a)
			{
				*string = *a;
				string ++;
				a++;
			}
			*string = '\0';
			string -= len;
		}
		void displaydata() //Displaying data to the user
		{
			cout << endl << endl << "Your string is : " << string;
		}

		//Function Declarations which will operate on the string
		void strcpy(strings &);
		int strcmp(strings &);
		void strcat(strings &);
		void strrev();
		void toupper();
		void tolower();
		char* substr(int &,int);
		char* rightstr(int&);
		int instr(char const *);
		void ltrim();
		void rtrim();
		void alltrim();
		int palindrome();
		int occur(char const *);
};

int strings::occur (char const *str)
{
	int i = 0;
	int j = 0;
	int result = 0;
	while (*(str+j))
		j++;
	int k;
	while (*(string+i))
	{
		if (*str == *(string+i))
		{
			for (k = 1;k <= j;k++)
			{
				if (*(str+k) != *(string+i+k))
					break;
			}
			if (k == j)
				result++;
		}
		i++;
	}
	return result;
}

int strings::palindrome()
{
	char *str = string + len -1;
	int i = 0;
	for (int j = 0; j <= len/2; j++)
	{
		if (*(string+i) != *(str-i))
			return 0;
		i++;
	}
	return 1;
}
void strings::ltrim()
{
	int i = 0;
	while (*string == 32)
		{string ++;i++;}
	len = len - i;
}

void strings::rtrim()
{
	char *str = (string+len-1);
	int i = 0;
	while (*str == 32)
		{str--;i++;}
	*(str+1) = '\0';
	len = len - i;
}

void strings::alltrim()
{
	ltrim();
	rtrim();
}

int strings::instr(char const *str)
{
	int i = 0;
	int j = 0;
	while (*(str+j))
		j++;
	int k;
	while (*(string+i))
	{
		if (*str == *(string+i))
		{
			for (k = 1;k <= j;k++)
			{
				if (*(str+k) != *(string+i+k))
					break;
			}
			if (k == j)
				return i+1;
		}
		i++;
	}
	return 0;
}

void strings::strcpy (strings &source)
{
	delete []string;
	string = new char[source.len+1];
	len = source.len;
	int i = 0;
	while (*(source.string+i) != '\0')
	{
		*(string+i) = *(source.string+i);
		i++;
	}
	*(string+i) = '\0';
}

int strings::strcmp (strings &second)
{
	int i =0;
	while (*(string+i) == *(second.string+i) && (*(string+i) != '\0' || *(second.string+i) != '\0'))
		i++;

	return (*(string+i) - *(second.string+i));
}

void strings::strcat (strings &other)
{
	char *temp = new char[len+other.len+1];
	int i = 0;
	while (*(string+i))
	{
		*(temp++) = *(string+i);
		i++;
	}
	delete []string;
	i = 0;
	while (*(other.string + i))
	{
		*(temp++) = *(other.string + i);
		i++;
	}
	*temp = '\0';
	temp -= len+other.len;
	string = temp;
	len = len+other.len;
}

void strings::toupper()
{
	int i = 0;

	while (*(string+i))
	{
		if (*(string+i) > 96 && *(string+i) < 123)
			*(string+i) -= 32;
		i++;
	}
}

void strings::tolower()
{
	int i = 0;

	while (*(string+i))
	{
		if (*(string+i) > 64 && *(string+i) < 91)
			*(string+i) += 32;
		i++;
	}
}

char * strings::substr(int &length, int start = 1)
{
	char* temp = new char[length+1];
	string += start-1;
	int i = 0;
	while (length != 0 && *(string+i) != '\0')
	{
		*(temp+i) = *(string+i);
		length--;
		i++;
	}
	*(temp+i) = '\0';
	string -= start-1;
	return temp;
}

int str_len (const char *a)
{
	int len = 0;
	while (*a)
	{
		len++;
		a++;
	}
	return len;
}

void strings::strrev()
{
		int temp = len/2;
		char *last = string, to_swap;
		last = last + len -1;
		while (temp)
		{
			to_swap = *string;
			*string = *last;
			*last = to_swap;
			string++;
			last--;
			temp--;
		}
		string -= len/2;
}

char* strings::rightstr(int &no_chars)
{
	if (no_chars > len)
		no_chars = len;
	int i= len - no_chars;

	char *temp = new char[i+1];
	while (*(string+i) != '\0')
	{
		*temp = *(string+i);
		i++;
		temp++;
	}
	*temp = '\0';
	temp -= no_chars;
	return temp;
}

int main()
{
	clrscr();
	char *str;
	int choice,result;
	int no_chars;
	strings main_string;

	//Taking entry for the main string
	main_string.getdata();

	clrscr();

 do
 {
	clrscr();
	cin.ignore(EOF);
	//Providing menu
	cout << "\t\tMenu for the operations on the given string" << endl
		<< "1.  Change the Main String." << "\t\t\t\t\t0.  Exit" << endl
		<< "2.  Display the string." << endl
		<< "---------------------------------------------------------------------------" << endl
		<< "3.  Count the length of the string (strlen)." << endl
		<< "4.  Copy a string into the main string (strcpy)." << endl
		<< "5.  Compare a string with the main string (strcmp)." << endl
		<< "6.  Concatenate a string with the main string (strcat)." << endl
		<< "7.  Reverse the main string (strrev)." << endl
		<< "8.  Convert all the characters in UPPER case (toupper)." << endl
		<< "9.  Convert all the characters in lower case (tolower)." << endl
		<< "10. Extract a substring (Mid$)." << endl
		<< "11. Extract the string from right site (Right$)." << endl
		<< "12. Extract the string from right site (Left$)." << endl
		<< "13. Find the first occurance of a string in the main string (Instr)." << endl
		<< "14. Remove all the trailing trims (Ltrim$)." << endl
		<< "15. Remove all the following trims (Rtrim$)." << endl
		<< "16. Remove all the trailing and following trims (Alltrim$)." << endl
		<< "17. Check whether the string is palindrome or not." << endl
		<< "18. Find the occurance of a substring in the main string (occur)." << endl;

	//Taking choice
	cout << endl << "Enter the no. of your choice here : ";
	cin >> choice;
	cin.ignore();

	//Taking actions
	switch (choice)
	{
		case 1:
			main_string.getdata();
		break;
		case 2:
			main_string.displaydata();
			cin.get();
		break;
		case 3:
			cout << endl << "Length of the string = " << main_string.len;
			cin.get();
		break;
		case 4:
			cout << endl <<"Enter a string to be copied (Maxlength = 100) : ";
			{
				char *str;
				cin.getline(str,100);
				strings temp_string (str);
				temp_string.displaydata();
				cin.get();
				main_string.strcpy(temp_string);
			}
		break;
		case 5:
			cout << endl <<"Enter a string to be compared (Maxlength = 100) : ";
			cin.getline(str,100);
			{
				strings temp_string (str);
				temp_string.displaydata();

				result = main_string.strcmp(temp_string);
			}
			cout << endl <<"The result of comparision = " << result;
			cin.get();
		break;
		case 6:
			cout << endl <<"Enter a string to be concatenated (Maxlength = 100) : ";
			cin.getline(str,100);
			{
				strings temp_string (str);
				temp_string.displaydata();

				main_string.strcat(temp_string);
			}
		break;
		case 7:
			main_string.strrev();
		break;
		case 8:
			main_string.toupper();
		break;
		case 9:
			main_string.tolower();
		break;
		case 10:
		{
			int start, length;
			cout << endl << "Enter starting point : ";
			cin >> start;
			cout << endl << "Enter length of the string to be extracted : ";
			cin >> length;
			cin.ignore();

			str = main_string.substr(length,start);
		}
			cout << endl << "The resultant string = " << str;
			cin.get();
		break;
		case 11:
			cout << endl<< "Enter the number of characters to be extracted from right side : ";
			cin >> no_chars;
			cin.ignore();

			str = main_string.rightstr(no_chars);
			cout << endl << "The resultant string = " << str;
			cin.get();
		break;
		case 12:
			cout << endl<< "Enter the number of characters to be extracted from left side : ";
			cin >> no_chars;
			cin.ignore();

			str = main_string.substr(no_chars);
			cout << endl << "The resultant string = " << str;
			cin.get();
		break;
		case 13:
		{
			int occurance_char;
			cout << endl << "Enter a string whose occurance is to be found : ";
			cin >> str;
			cin.ignore();

			occurance_char = main_string.instr(str);
			cout << endl << "The first position of the substring = " << occurance_char;
			cin.get();
		}
		break;
		case 14:
			main_string.ltrim();
		break;
		case 15:
			main_string.rtrim();
		break;
		case 16:
			main_string.alltrim();
		break;
		case 17:
			result = main_string.palindrome();
			if (result)
				cout << endl << "The string is PALINDROME.";
			else
				cout << endl << "The string is NOT palindrome.";

			cin.get();
		break;
		case 18:
			cout << endl << endl << "Enter the string whose occurance you want to find : ";
			cin.getline(str,100);

			result = main_string.occur(str);

			cout << endl << endl << "The substring occured for " << result << " time(s).";
			cin.get();
		break;
		case 0:
			cout << endl << "\t\t\t\tThank You";
			cin.get();
		break;
		default :
			cout << endl << "\t\t\t\tWrong Entry";
			cin.get();
		break;
	}
 } while (choice != 0);

	return 0;
}

Now i m beginner so dont laugh at me plz........

That's nice, now could you tell us exactly what sequence of actions on the part of the user cause the program to abruptly terminate?

I m talking about all the programs i have made and I use Turbo C compiler...

I am appending the whole code...... Check it out...

No way. You didn't use CODE tags which are mentioned
1) In The Rules you read when you signed up
2) In the post aptly titled Read Me: Read This Before Posting
3) In the post about BBCode Tags
4) On the background of the input box you posted that unreadable code.

Oooooopppppppppsssssssssss...........


Sorry sir....

Actually i m really new at this thing...
Gimme some time to adjust my self in the forum enviornment......


Thank you for appreaciating....

Now you run the programme in TC and i have provided one option to get exit from the menu to the editor window as no user is goin to use it..... But still you use it for me :-).
When you will chose the Exit option, it will bring you out of TC... Try it sir...

I made that for jz assignment purpose but the problem is reallly cumbersome so i asked you all..

>When you will chose the Exit option, it will bring you out of TC...
I don't understand. Your problem is that the program "abruptly terminates", yes? And the steps to reproduce the problem are to choose the exit option from the menu? If the exit option terminates the program, that sounds like the correct and intended behavior to me.

line 40: You are attempting to use an uninitialized pointer as the input buffer to getline(). That will most certainly cause your program to scribble all over hell and back and is most likely one of the causes of the behavior you are reporting. On line 38 replace char *a; with this: char a[100]; Then you will have to rework the loop starting at line 43 to index into the array a instead of incrementing the pointer.

function ltrim(). you created a memory leak by moving the pointer string. Can't do that because you will never successfully delete it after doing that. What you need to do is locate the first non-white-space (using isspace() ) and then shift all the characters left to overwrite all those spaces and tabs.

I suspect there may be other similar problems in your code, but I don't have time to look for them.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Sir I have tried that too....
But believe me its not so....
and I am not talking only abt this programme...
It is also the same case in one another programme....


Narue, you are right that the prog should get terminated when we choose Exit option, but as i m not a professional , i m not going to provide EXE file of this prog to any one... But as Practice Purpose, what i need is that the EXIT OPTION should draw me to the editor window instead of closing the batch job as i may further require it...

Sir I am very much confused.... Even our faculty is saying that there is no solution for such problem but how it can be possible... There must be any good reason behind that... Try to find that and please give me necessary suggesstions to enhance my programming skill....:S

Sir plz try to solve my problem as soon as possible....

Getting very confused sir......

This article has been dead for over six months. Start a new discussion instead.