out_of_range at memory location errors

I am working on a school project and have it figured out - almost.

note: I do not want someone to write a program for me, I am in school to learn this, not to use other peoples work. But if I could get a bit of guidance for my problem it would help, I am in an online course and don't have readily available access to ask my instructor questions most times.

I am working in Windows Vista x64 using Microsoft Visual Studio 2008 Professional Edition.

Here is the assignment, I will bold the part that I can't seem to get working.

First, write a simple program that will read 5 numbers from the Console (user input). Store these values into an array of int []. Using the length of the array in a for() loop and decrementing the loop counter, write the array values in reverse order to the screen. This is step one and may be enough for partial credit. If you have difficulty with this, try seeking some assistance from your instructor before going on to step two.

Next, add code to read 5 words from the Console (user input). Store these values into an array of string []. Make sure the string[] array is large enough to hold at least 6 values. Store the string constant “end_of_array” into the last element of the array. Using a do…while() loop, print out the 1st and 3rd letters of each word (two letters per line) using the substring function. This is step two and should ensure you at least a passing grade.

Finally, modify the above program to read the numbers in from a text file (numbers.txt), write the values in reverse order to a second file, and read the words in from a third file, displaying the results to the Console as described above. This is your completed project.


Here is my code, I have commented in where the errors begin.

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

using namespace std;

int main()
{

//declare variables
int array[5];
int counter;
string words[6];
const string endWord("end_of_array");






//five numbers in array
	cout << "Getting numbers from text file";

	ifstream inData;
	ofstream outData;


	inData.open("numbers.txt");
	outData.open("output.txt");


		for (counter = 0; counter < 5; counter++)
		{
			inData >> array[counter];
		}

	cout << endl;


	cout << "The numbers are: ";

		for(counter = 0; counter < 5;counter++)
		{
			cout << array[counter] << " ";
		}

	cout << endl;
	cout << "Hit enter to print these numbers in reverse order into the output.txt file" << endl;
	cin.get();
	

	outData << "The numbers in reverse order are: ";

		for (counter = 4; counter >= 0; counter--)
		outData << array[counter] << " ";

	cout << endl;


inData.close();
outData.close();



// This is where my problem starts and where the errors are coming from

inData.open("words.txt");// text file with 5 words in it like this: hello aloha forms paper grade
	cout << "Getting words from words.txt file" << endl;

		for (counter = 0; counter < 5; counter++)
		{
			inData >> words[counter];

		}

		words[5]=endWord;


		counter=0;

//using 0 to identify first letter, 0 is the starting position just as 2 is the position of the third letter

	cout << "Here is the 1st and 3rd letter of each word: " << endl;

		do
		{
			cout << words[counter].substr(0,1);
			cout << words[counter].substr(2,1);
			cout << endl;
			counter++;
		}
		
		while( words[counter]!=endWord);		

cin.get();
cin.get();
return 0;
} 


The program compiles and works fine when I have a user input the words. I am getting the following error 5 times when I try to make it read the words from a text file - I assume it's for each time it tries to read in a word from the text file, but I can't figure out why, or what I need to do to fix this problem.

First-chance exception at 0x76f8f328 in George_Miller_Week8.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0043f358..
Unhandled exception at 0x76f8f328 in George_Miller_Week8.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0043f358..


Any help would be appreciated, I just can't seem to get this one figured out.
Last edited on
Call inData.clear(); before reopening it on line 67.
worked like a charm.

thank you very much, I never even thought to clear the old data before getting new data from a separate text file.
Topic archived. No new replies allowed.