program stopped working!!

Hi everyone,
can someone help me plz? my program has no errors nor wornings yet it stops working and the output is all missed up :(
(in case you're wondering what it does, it's a simulator of the reader writer problem ... but it's not complete yet and i'm open to other design ideas if you have one ... please ;P )

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <ctime>    // For time()
#include <cstdlib>  // For srand() and rand()
#include <iostream>
#include <fstream>
#include <string>


using namespace std;
class Reader
{
private:
	string city[5];
	int tempr[5];

public:
	Reader()
	{
		for(int i=0; i<5; i++)
			tempr[i]=0;
	}
	void read()
	{
		ifstream inClientFile("record.txt",ios::in);
		if(!inClientFile)
		{
			cerr<<"File could not be opened"<<endl;
			exit(1);
		}

		for(int i=0;i<5; i++)
		{
			inClientFile>>city[i]>>tempr[i];
			cout<<city[i]<<"\t\t"<<tempr[i]<<endl;
		}
	}
	};

	class Writer
	{
	private:
		string city[5];
		int tempreture;

	public:

		Writer(){
			city[0]="Kuwait";
			city[1]="Manama";	
			city[2]="Ryadh";
			city[3]="Abu Dhabi";
			city[4]="Muscat";
	}
	
	void write()
	{
		srand(time(0));  
		

		ofstream outClientFile("record.txt",ios::out);
		if(!outClientFile)
		{
			cerr<<"File could not be opened"<<endl;
			exit(1);
		}


		for(int i=0; i<5; i++)
		{
			tempreture = (rand() % 60) + 1;
			outClientFile<<city[i]<<"\t\t"<<tempreture<<endl;
			cout<<city[i]<<"\t\t"<<tempreture<<"\n";	
		}
	}
	
	};



int main()
{
	srand(time(0)); // seeding the random generator function.

	cout<<"Welcome to our reader writer simulator :)\n\nIn this simulator, 5 reading processes and 3 writer processes are operating.\n\n\n";
	
	Reader* rdrProc[5];
	Writer* wrtProc[3];

	// Creating processes.
	for(int i=0; i<5; i++)
	{
		rdrProc[i]= new Reader();
	}

		for(int j=0; j<3 ; j++)
	{ 
		wrtProc[i]= new Writer();
	}
	
		// let any writer process write data to the file for starter.
		wrtProc[1]->write();
		for(int count=0; count<20; count++)
		{
			int r,p;
			r = ((rand() % 8) + 1);// randomly selecting a process to enter the file (1,2 & 3 are the writer processes. the rest are readers)
			if(r<=3)// the process is a writer process.
			{
				p=(rand() % 3) + 1;
				cout<<"Hi, I'm writer "<<p<<" and I wrote to the file the following data:\n";
				wrtProc[p]->write();
			}
			else // the process is a writer process.
			{
				p=(rand() % 5) + 1;
				cout<<"Hi, I'm reader "<<p<<" and I read the following data from the file:\n";
				rdrProc[p]->read();
			}
		}

	return 0;
}
Topic archived. No new replies allowed.