access reading and writing violation for no apparent reason.

okay so my program is about Little Alchemy. I have made the game actually. Four different classes. Earth, Water, Fire and Air. which combine to form more elements and further combine to form more elements. Now my problem is related to file handling. I am discussing here just my water class. Though the combinations are not complete but still it'll give you all an idea. I have made function for not repeating any element while storing in the file. (Using Sequential) Now if i keep on adding water and water as input, it gives me sea. Without displaying, if a keep on adding water and any other element in a row. At a specific point, it throws an exception on access reading violation. In water class, it's after five elements i guess while in other after three it crashes. However if I make random elements and also keep on displaying them after few elements, the program runs fine but even then, randomly at any point it again crashes. Like I don't know why it crashes and what the point for abnormal termination. If there is anything different, it's in the alreadyExists function.

Kindly go through the code very carefully. I hope you'll get it what i am trying to say. Like it is totally going random. Suddenly it crashes and when i keep on adding elements in a row, it crashes at any random point. What should I do!


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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#ifndef WATER_H
#define WATER_H
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include "element.h"
#include "Fire.h"
#include "Earth.h"
#include "Air.h"
using namespace std;

class Water :public Element 
{
public:
	
	Water ()
	{

	}


	Water(string n): lineCount (0), Element (n)
	{
		water = n;
		i=0;	
		
	}

	string water;
	int i;
	string Elements[4];
	int lineCount;

	void alreadyExists (string a)
	{
		
		ifstream read;
		read.open ("Water.txt", ios::in | ios::app);
		while(!read.eof())
			{
				string x;
				read>>x;
				lineCount++;

			}
			lineCount--;
			string *elements = new string[lineCount]; //length of the string array increasing with increase in line count//
			read.close();
			read.open("Water.txt", ios::in|ios::app);
			for(int i = 0; i < lineCount; i++)
			{
				read>>elements[i];
			}
			bool Found = false;
			for(int i = 0; i < lineCount; i++) {
				if(a == elements[i]) 
				{
					Found = true;
					break;
				}
			}
			if(!Found) 
			{
				write2file (a);
				//write << newE.air << endl;
			}

	}


/*void PushW (string newElement)
{
	
			
	if(i>=8)
	{
		cout<<"Cannot be added"<<endl;
	}
	else
	{
		Elements[i] == newElement;
		i++;
	}

}*/





void write2file (string water)
{
	
	ofstream write;
	
	write.open ("Water.txt", ios::out|ios::app);
	
			{
				write<<water<<endl;
				
			}			
}



 void DisplayW ()
	 {
		
		ifstream read ("Water.txt", ios::in|ios::app);
		 
		string x;
		 while (read >> x)
				 {
					 cout<<x<<endl;
				 }
				//(Link List)
				
	 }



	

	string getnameWater()
	{
		return water;
	}

	 Water operator+(const Air& air) //combining an element of water with an element of air. return water instance
	{                                    
		Water newElement ("NULL");       
		if ((this->water == "Water"||this->water=="water") && (air.air == "Air"||air.air=="air")) 

		{
			newElement.water = "Rain";
			cout<<"You've made: "<<newElement.water<<endl;
			
			alreadyExists (newElement.water);
			
				return newElement;
		}
		else return newElement;

	} //rain,

	 Water operator+(const Fire & fire) //combining an element of water with an element of fire. return water instance
	{
		Water newElement ("NULL");
		if ((this->water == "Water"||this->water=="water") && (fire.fire == "Fire"||fire.fire=="fire"))
		{
			newElement.water = "Steam";
			cout<<"You've made: "<<newElement.water<<endl;
			
			alreadyExists (newElement.water);
			
			return newElement;
		}
		else return newElement;

	}//steam, 

	Water operator+ (const Earth & earth) //combining an element of water with an element of earth. return water instance
	{
		Water newElement ("NULL");
		if ((this->water == "Water"||this->water=="water") && (earth.earth == "Earth"||earth.earth=="earth"))
		{
			newElement.water = "Mud";
			cout<<"You've made: "<<newElement.water<<endl;
			alreadyExists (newElement.water);
			
			return newElement;
		}
		else return newElement;
	}//mud


	 Water operator+ (const Water & water)
	{
		Water newElement ("NULL");
		if ((this->water == "Water"||this->water=="water") && (water.water == "Water"||water.water=="water"))
		{
			newElement.water = "Sea";
			cout<<"You've made: "<<newElement.water<<endl;
			alreadyExists (newElement.water);
			
			
			return newElement;
		}
		else return newElement;
	}//sea



};
#endif  

Topic archived. No new replies allowed.