Not being able to sort out problem!! Please Help

In my class i have a pointer to its own type so that when it adds up then pointer of first gets address of second.
But the program is giving segmentation error. Now I found myself helpless in finding a solution. I'm struggling from many days in debugging. Plz help.
BackEnd Filename 1D.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
/*
 * Summary of Data Members
 * t		It is static member and counts for time of the complete system.
 * 			Resets to zero when a new oscillator attaches.
 * m		Mass of individual Oscillator Unit
 * S		Spring Constant of individual Oscillator Unit
 * l		Lenghth of unstretched spring of individual Oscillator Unit
 * x		Position of individual Oscillator Unit
 * x1		Velocity of individual Oscillator Unit
 * x2		Acceleration of of individual Oscillator Unit
 * *os		Identity of Oscillator with which it is attched to.
 */				
class osc
{
	osc* os;
	static long double t;
	long double m,S,l,x,x1,x2;
	public:
	osc(){m=S=l=x=x1=x2=0; os=NULL;}
	osc(double M, double s, double L, double X, double X1, double X2){m=M; S=s; l=L; x=X; x1=X1; x2=X2; os=NULL;}
	osc operator =(osc b){return b;}
	osc operator +(osc b){osc a=*this; a.attachToIt(b); return a;}
	osc operator +=(osc b){attachToIt(b); return *this;}
	void chOsc(double M, double s, double L, double X, double X1, double X2){m=M; S=s; l=L; x=X; x1=X1; x2=X2; os=NULL;}
	void attachToIt(osc b){os=&b; t=0;}
	void resetTime(void){t=0;}
	string getParam();
	void increment(double Sn, double ln, double xn, double dt, ofstream &output);
	void evolve(double dt, double timeToEvolve, ofstream &output);
};
string osc::getParam()
{
	ostringstream out;
	string a;
	out<<"Mass="<<m<<", Spring Constant="<<S<<", Unstretched Length="<<l<<", Position="<<x<<", Velocity="<<x1<<", Acceleration="<<x2<<endl;
	if(os!=NULL)
	{
		a=os->getParam();
		out<<"Next Unit\n"<<a;
	}
	else
	{
		out<<"Wall Encountered. End!\n";
	}
	return out.str();
}
void osc::evolve(double dt, double timeToEvolve, ofstream &output)
{
	double tf=t+timeToEvolve;
	while (t<tf)
	{
		output<<t;
		x+=x1*dt;
		x1+=x2*dt;
		if(os!=NULL)
		{
			x2+=dt*(x-os->x-l)*S/m;
			output<<"\t"<<x;
			os->increment(S,l,x,dt,output);
			cout<<endl;
		}
		else
		{
			x2+=((x-l)*S/m)*dt;
			output<<"\t"<<x;
		}
		t+=dt;
	}
}		
void osc::increment(double Sn, double ln, double xn, double dt, ofstream &output)
{
	x+=x1*dt;
	x1+=x2*dt;
	if(os!=NULL)
	{
		x2+=((x-os->x-l)*S/m+(x-xn-ln)*Sn/m)*dt;
		output<<"\t"<<x;
		os->increment(S,l,x,dt,output);
	}
	else
	{
		x2+=((x-l)*S/m+(x-xn-ln)*Sn/m)*dt;
		output<<"\t"<<x;
	}
}
long double osc::t=0;
//osc os; 


Front End
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include "1D.C"
using namespace std;
osc a;
double pi=4*atan(1),watch;
clock_t start;
ofstream output;
void showMenu()
{
	cout<<"\n 1. Change Oscillator Data";
	cout<<"\n 2. Add Oscillator";
	cout<<"\n 3. Show Parameters";
	cout<<"\n 4. Save Parameters To File";
	cout<<"\n 5. Evolve System";
	cout<<"\n 0. Exit";	
	cout<<"\nEnter Option( )\b\b";
}
void action(short o)
{
	char filename[1000];
	long double m,S,l,x,x1,x2;;
	switch(o)
	{
		case 1:{
		cout<<"\nMass               =\t";	cin>>m;
		cout<<"Spring Constant    =\t";	cin>>S;
		cout<<"Unstretched Length =\t";	cin>>l;
		cout<<"Position           =\t";	cin>>x;
		cout<<"Velocity           =\t";	cin>>x1;
		cout<<"Acceleration       =\t";	cin>>x2;
		a.chOsc(m,S,l,x,x1,x2);
		break;}
		case 2:{
		cout<<"\nMass               =\t";	cin>>m;
		cout<<"Spring Constant    =\t";	cin>>S;
		cout<<"Unstretched Length =\t";	cin>>l;
		cout<<"Position           =\t";	cin>>x;
		cout<<"Velocity           =\t";	cin>>x1;
		cout<<"Acceleration       =\t";	cin>>x2;
		a+=osc(m,S,l,x,x1,x2);
		break;}
		case 3:{
		cout<<"\n"<<a.getParam()<<endl;
		break;}
		case 4:{
		cout<<"Enter filename for Output File\n";
		cin>>filename;
		output.open(filename);			
		output<<a.getParam()<<endl;
		output.close();
		break;}
		case 5:
		{
			cout<<"\nEnter Time To Evolve\t";
			cin>>x;
			cout<<"\nEnter Time Step\t";
			cin>>x1;
			cout<<"Enter filename for Output File for launcher\n";
			cin>>filename;
			output.open(filename);			
			watch=time(NULL);
			start=clock();
			a.evolve(x1,x,output);
			output.close();
			cout<<"\nTime Elapsed in program is "<<time(NULL)-watch<<" seconds\n";
			cout<<"\nCPU Time Elapsed in program is "<<double(clock()-start)/CLOCKS_PER_SEC<<" seconds\n";
			cout<<"\n Clocks per sec are "<<CLOCKS_PER_SEC<<endl;
			break;
		}
		case 0:
		exit(0);
	}
}
int main()
{
	short o;
	cout<<"\nAll parameters assumed to be zero.\n";
	while(true){
		showMenu();
		cin>>o;
		action(o);
	}
}
Topic archived. No new replies allowed.