sorting strings from a file and Arrays

Dear all,

I cant find the problem, may i have some help please :'(

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

using namespace std;

int main()
{
	int index[10];
	int i, j,temp;
	string mystring;
	
	ifstream myfile("input.txt");

cout << "The file contains the following contents: " << endl;

		...
	
	for (int i=0; i < 10; ++i)
	{
		cout << mystring[index[i]] << endl;
	}

  system("pause");
  return 0;
}



Error: "String subscript out of range"
Thank you
Last edited on
After the while loop at line 17 completes, we know that myfile.eof() must be true.

Thus the loop at line 23 can never execute. The array index[10] contains uninitialised data, attempting to use these values for any purpose will give unpredictable results.

Also, the if statement at line 35 is incomplete, the code doesn't compile.
Last edited on
Topic archived. No new replies allowed.