Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

C++ techs help me!


  • Please log in to reply

#1
Encrypted Devil

Encrypted Devil

    New Member

  • Member
  • Pip
  • 4 posts
heyyy geeks...
i m a learner in OOP C++ programming...
d problem dat i m facing is the abrupt termination of C++ when i execute a program...let me explain in detail..

wen i run a program..smtimes it runs well and fine widout any problem...but wen i exit the program the TC.exe terminates abruptly :) ....(it also hapens at time wen a fatal error occurs..hwever its natural... :) )

i thnk d reason behind mite be like dis : during d program execution...somehow d memory location of TC.exe mite be gettin overwritten by some or d other component of d program...however dis is my guess... :) :) :)

sooo...is dere neone who can help me out here ??? i want a perfect reason and a solution to that....
:)
  • 0

Advertisements


#2
Granz00

Granz00

    Member

  • Member
  • PipPipPip
  • 226 posts
Got any code for us to look at? Got anything else than what you provided? It doesn't seem like you provided anything at all except the knowledge that you have a problem...
  • 0

#3
Encrypted Devil

Encrypted Devil

    New Member

  • Topic Starter
  • Member
  • Pip
  • 4 posts
okkk...
here is my code..
look at the code with ease... :)

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

#include "iostream.h"
#include "conio.h"
#include "stdlib.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);
/*while (*a)
len++;*/
//*a -= len;
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);
//while (*a)
// len++;
//a -= len;
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 &);
};

void strings::strcpy (strings &source)
{
delete []string;
string = new char[source.len+1];

while (*source.string)
{
*string = *source.string;
string ++;
source.string++;
}
*string = '\0';
string -= len;
}

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

void main()
{
clrscr();
char *str;
int choice;
strings main_string;

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

clrscr();

do
{
clrscr();
//Providing menu
cout << "\t\tMenu for the operations on the given string" << endl
<< "1. Change the Main String." << endl
<< "2. Display the string." << endl
<< "3. Count the length of the string." << endl
<< "4. Copy a string into the main string." << endl
<< "0. Exit" << 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) : ";
cin.getline(str,100);
strings temp_string (str);
temp_string.displaydata();
cin.get();
main_string.strcpy(temp_string);
break;
case 0:
cout << endl << "\t\t\t\tThank You";
cin.get();
exit(0);
break;
default :
cout << endl << "\t\t\t\tWrong Entry";
cin.get();
break;
}
getch();
} while (choice != 0);

}


i am also attaching the .cpp file...
  • 0

#4
Encrypted Devil

Encrypted Devil

    New Member

  • Topic Starter
  • Member
  • Pip
  • 4 posts
okkk...
here is my code..
look at the code with ease... :)

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

#include "iostream.h"
#include "conio.h"
#include "stdlib.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);
			/*while (*a)
				len++;*/
			//*a -= len;
			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);
			//while (*a)
			  //	len++;
			//a -= len;
			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 &);
};

void strings::strcpy (strings &source)
{
	delete []string;
	string = new char[source.len+1];

	while (*source.string)
	{
		*string = *source.string;
		string ++;
		source.string++;
	}
	*string = '\0';
	string -= len;
}

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

void main()
{
	clrscr();
	char *str;
	int choice;
	strings main_string;

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

	clrscr();

 do
 {
	clrscr();
	//Providing menu
	cout << "\t\tMenu for the operations on the given string" << endl
		<< "1. Change the Main String." << endl
		<< "2. Display the string." << endl
		<< "3. Count the length of the string." << endl
		<< "4. Copy a string into the main string." << endl
		<< "0. Exit" << 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) : ";
			cin.getline(str,100);
			strings temp_string (str);
			temp_string.displaydata();
			cin.get();
			main_string.strcpy(temp_string);
		break;
		case 0:
			cout << endl << "\t\t\t\tThank You";
			cin.get();
			exit(0);
		break;
		default :
			cout << endl << "\t\t\t\tWrong Entry";
			cin.get();
		break;
	}
	getch();
 } while (choice != 0);

}

now check out the code...
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP