C++ ERROR.

Hello, when I run my program I got this error and my visual studios crashes or it asks to "Break, continue or ignore". This is the error :

First-chance exception at 0x0119A645 in Project10.exe: 0xC0000005: Access violation writing location 0x01A8ACE0.

If there is a handler for this exception, the program may be safely continued

I was wondering if any of you guys can find what the problem is...Here is my code


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
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
	int count = 0;
	int ar[500];
	int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0;
	ifstream myReadFile;
	ofstream myOutFile;
	myReadFile.open("example.txt");
	myOutFile.open("output.txt");
	char output[100];
	int n = 0;
	while (!myReadFile.eof())
	{


		myReadFile >> output;
		// cout<<output << "\n"; this line you can remove cooments if you want the output on the screen.
		n = (int)(output); // converting character to integer.
		ar[count] = n;
		if (n >= 180)
		{
			c5++;
		}
		else if (n >= 160)
			c4++;
		else if (n >= 140)
			c3++;
		else if (n >= 120)
			c2++;
		else
			c1++;

		count++;


	}
	myOutFile << c1 << c2 << c3 << c4 << c5 << count;

	// here yoiu can print the array into the file if you wish in to the file.
	myReadFile.close();
	myOutFile.close();
	return 0;
}
Last edited on
Almost certainly you are overrunning the boundaries of array int ar[500]
But there are a number of problem areas in the code.
What is the contents of file "example.txt" and what is it you are attempting to do?
Line 21 almost certainly doesn't do what you think it does. And looping on eof() - not recommended at all. For example if the file was not opened, it will never reach eof, instead go into infinite loop.
Last edited on
This is my assignment:
The attached text data file contains an unknown quantity of integers whose values are between 100 and 200. Write a program that opens the file, counts how many values it contains, and creates a 1D array to store the values. Once the array is populated, calculate average and standard deviation. Also count how many values are between 100 and 119 (inclusive), 120 and 139 (inclusive), 140 and 159 (inclusive), 160 and 179 (inclusive), and 180 and 200 (inclusive). Output all the results (including the count of how many total values there are and the counts of how many values are in each range) to a separate text file

and the content of "example.txt" is:
107 141 101 160 105 147 152 128 117 100 175 140 114 118 115 198 142 106 169 197 186 126 104 101 123 170 146 101 162 117 118 117 124 109 169 182 178 111 139 129 133 199 102 153 160 199 129 174 177 183
166 155 134 181 140 175 135 176 158 129 188 162 104 148 146 166 125 112 137 155 134 166 178 199 108 185 150 125 193 163 134 187 192 105 139 178 146 190 151 126 147 195 118 119 114 101 137 179 157 183
167 107 181 117 191 175 150 174 171 108 149 100 197 191 133 186 198 102 172 168 179 127 185 195 200 182 199 159 161 124 156 158 150 104 134 179 109 141 165 129 182 158 108 196 197 112 105 181
160 187 144 112 174 115 177 103 120 115 168 194 172 127 110 175 190 186 114 117 113 137 187 111 179 139 157 124 172 157 117 197 198 100 107 167 126 177 192 119 105 162 125 168 100 195 200 108
194 151 197 141 105 196 102 192 148 101 133 196 130 182 165 113 183 197 122 167 105 121 140 185 122 200 158 135 115 133 179 101 111 128 198 169 196 148 124 124 175 148 168 133 199 160
135 173 182 103 152 193 155 137 125 105 145 118 164 174 200 180 182 111 108 181 127 189 181 122 110 116 108 194 193 114 140 134 130 168 188 197 127 173 177 179 121 157 124 198 120 119
157 144 121 104 165 179 169 189 176 115 193 143 129 126 186 161 137 176 116 120 120 116 160 176 156 195 193 125 119 100 112 163 196 193 146 139 178 112 132 159 113 183 195 133
184 187 144 110 121 103 170 127 175 193 174 149 141 104 119 128 122 170 168 169 130 101 184 176 134 124 163 100 145 199 160 114 106 126 176 185 198 158 109 158 144 122 106 192
122 143 153 184 197 142 156 131 145 130 100 137 159 138 124 127 130 168 121 123 180 127 199 159 172 110 175 120 172 112 137 191 124 174 139 161 195 184 193 121 125 156
199 148 186 136 108 187 138 124 155 156 111 125 105 168 126 188 107 147 132 147 114 101 152 144 156 120 157 153 110 180 127 147 186 100 178 180 116 190 165 129 160 195
149 179 107 179 145 156 102 151 122 194 195 147 169 192 137 147 200 165 114 101 107 187 113 139 122 152 106 122 177 154 109 188 113 124 141 104 184 123 155 192
127 146 130 178 103 198 135 105 151 191 177 101 187 160 122 152 145 168 131 110 107 122 179 143 123 164 111 161 116 181 181 106 123 186 199 117 103 188 169 138
122 161 148 148 161 126 174 140 199 149 184 140 146 119 186 128 113 109 180 145 199 168 171 197 182 125 195 126 107 137 157 102 102 180 198 122 162 195
125 164 118 123 119 159 150 200 150 130 155 168 155 177 117 139 157 155 116 112 104 124 198 102 101 109 197 123 177 161 109 127 111 117 191 197 184 107
193 180 109 161 124 149 126 132 143 188 116 113 198 160 152 189 154 189 107 142 146 157 148 164 122 194 119 189 125 100 123 168 173 126 148 120
196 178 166 132 149 114 196 174 142 181 156 190 119 176 173 133 111 166 175 112 178 124 165 193 132 176 103 101 162 150 159 190 126 172 178 118
179 146 103 126 121 120 150 197 166 199 102 139 189 123 178 105 105 177 105 124 196 192 175 143 151 180 130 128 188 172 185 183 109 128
188 162 139 170 144 105 180 178 113 121 164 165 135 159 118 129 144 145 179 188 176 133 110 194 191 171 105 147 200 192 103 146 146 162
174 166 162 117 177 189 187 194 171 112 102 116 189 194 162 159 158 118 158 198 180 128 136 103 180 114 104 139 142 107 195 147
156 116 119 101 165 178 197 135 131 117 157 133 122 196 200 100 130 196 139 197 194 194 135 180 123 174 154 147 152 185 108 111
106 193 142 120 160 145 120 103 108 114 173 131 149 138 102 193 176 105 177 145 164 120 177 150 177 130 167 194 190 191
191 166 157 130 174 153 137 105 123 169 179 105 124 123 172 150 161 152 131 174 188 113 184 117 172 158 196 184 128 101
146 196 190 173 155 155 103 132 154 188 112 108 155 181 187 112 108 185 165 179 179 140 166 197 181 134 185 144
189 116 161 151 154 184 181 144 200 126 121 130 160 108 184 115 168 137 138 145 114 155 129 197 117 130 128 144
171 127 125 153 155 145 153 174 194 180 195 172 141 120 172 187 100 152 123 158 159 119 124 200 120 161
197 171 134 199 170 186 107 150 151 119 158 129 137 115 158 133 196 177 103 133 147 141 180 141 159 127
115 118 107 143 174 118 177 121 183 155 144 191 156 183 177 123 182 118 192 160 198 146 194 126
125 106 120 142 112 166 174 169 167 160 133 156 122 110 188 148 102 163 120 104 196 166 178 129
141 112 178 115 105 113 104 114 118 148 169 192 103 186 107 180 119 126 166 121 122 187
122 160 168 127 153 179 166 156 181 130 110 166 155 180 126 114 157 100 192 123 113 152
117 102 117 118 123 130 200 115 163 185 167 152 108 158 164 148 199 171 127 191
130 118 118 198 131 140 148 134 185 126 196 188 197 145 179 131 172 180 183 195
107 183 113 154 143 161 108 120 152 168 146 198 131 137 181 135 200 161
152 118 181 113 143 145 164 115 133 175 192 183 115 113 192 126 163 182
119 191 184 128 149 143 151 123 125 191 133 174 188 106 123 176
115 175 143 101 132 147 163 102 175 164 157 131 158 147 155 125
121 138 149 155 182 146 169 106 150 176 117 148 116 122
100 177 133 154 200 145 103 177 143 125 149 141 183 152
149 110 191 125 101 196 142 149 161 193 105 120
139 199 121 176 195 163 133 123 131 178 126 134
176 189 178 157 105 132 123 118 109 185
170 151 171 173 160 182 115 170 150 156
142 113 171 105 119 102 136 120
164 170 155 135 117 200 184 106
187 166 109 132 115 115
108 112 157 191 188 109
189 167 108 158
152 175 191 177
176 132
182 187

I think the problem is in line 21, because when the error appears and I press break, some weird symbol appears in the line 21.
Thanks for the reply.

Line 21 is certainly wrong, it attempts to cast the address of the array into an integer. This is not the way to read the data from your file.

Did you consider how many numbers are in the file, even if the code worked, it would be more than the capacity of the array ar[500]

I suggest you start with some simple code to read the data and count how many there are.
Remember, the ifstream is a stream, just like cin. So, you can use the same syntax as cin >> number;

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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream myReadFile("example.txt");
    
    if (!myReadFile)
    {
        cout << "Unable to open input file\n";
        return 1;
    }

    int n = 0;
    int count = 0;
    
    while (myReadFile >> n)
    {
        cout << n << ' ';
        count++;
    }
    
    cout << "\nCount = " << count << '\n';

    return 0;
}
I should add, if you intend to use a fixed-size array to store an unknown quantity of data, then you should add a check to ensure that the capacity is not exceeded. Or use a std::vector which can resize itself as needed.
Thanks! You were right there were 1300 elements. However, I still can not do it. My output is the address of the array I think, its "000013011301" What I was trying to do it was maybe change
int ar[1300]
to
1
2
3
double *ar
and then put 
ar=new double[count]


But it didn't work either
I't's getting late where I am, so I can't give a full answer.
But look at this line:
myOutFile << c1 << c2 << c3 << c4 << c5 << count;
if we assume c1 to c4 are zero, and both c5 and count are 1301, would that not give your output of
"000013011301" ?
Thank you so much for your help I got it now !!
It does worry me that the number 1301 should have appeared at all, since you said earlier that there were 1300 elements. It sounds like you are still looping on eof() which is unreliable for all sorts of reasons, but in this case it may have processed the last entry twice, thus causing an array out of bounds condition if you allocated exactly 1300 elements.

In any case, the code should be checking for that out of bounds error.

const int SIZE = 500; // Set the size of the array

int ar[SIZE]; // allocate the array


1
2
3
4
5
6
7
8
9
10
    if (count < SIZE)
    {
        ar[count] = n;
        count++;
    }
    else
    {
        cout << "array size exceeded: " << count <<'\n';
        break;
    }


For a recent thread expanding a little on one aspect of eof() problems,
http://www.cplusplus.com/forum/beginner/180402/#msg885614
Topic archived. No new replies allowed.