Info from text to arrays?

I have an assignment where I have to xfer information from a .txt file into arrays but when I attempt to do this I get a really bad error message that states "An unhandled exception of type 'System.AccessViolationException' occurred in Project 6.exe" Can someone please check my code and see whats causing this issue? Btw the content in the .txt file looks like this:
A.Smith 89
T.Philip 95
S.LOng 76

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
  // Project 6.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;





int main()
{
	ifstream myFile("c:\\Users\\Administrator\\Downloads\\data file for project 6 summer version.txt");

	if(myFile.is_open())
	{
		string myArray[6];

		for(int i=0; i<6; i++)
		{
			myFile >> myArray[i];
		}
		cout << myArray[6];
	}

	





	system("PAUSE");
	return 0;
	
}
1
2
    string myArray[6];   // valid subscripts in the range 0 to 5
    cout << myArray[6];  // the seventh element, it is outside the array. 
I changed the range of the array to 5 but I'm still getting the same error message
I changed the range of the array to 5

Edit: sorry , I may have misunderstood.

can you show the updated code please.
When I run this code with the supplied text data:
1
2
3
4
5
6
7
8
9
10
	if(myFile.is_open())
	{
		string myArray[6];

		for(int i=0; i<6; i++)
		{
			myFile >> myArray[i];
		}
		cout << myArray[5]; // last permitted subscript is 5
	}

this is the output:
76
Last edited on
closed account (G30oGNh0)
cout << myArray[5]

You are printing out the last part of the array.

To print the entire array, why not define a constant int ( 6 in your case ) and loop the output?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const int arrayLength = 6;

string myArray[arrayLength];

for(int i=0; i<arrayLength; i++)
{
	myFile >> myArray[i];
}

for(int i=0; i<arrayLength; i++)
{
	cout << myArray[i];
}


I'm sorry if this is wrong, I haven't coded in a while ._.
An unhandled exception of type 'System.AccessViolationException' occurred in Project 6.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.



^I keep getting that error message no matter what I do to my code =/
Nevertheless, the code I posted should work - I tested it with no problems.

If you need further assistance It would help if you show the latest version of your code.
closed account (G30GNwbp)
I tested it also with this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>

int main()
{

    using namespace std;

    const int sz{6};
    ifstream myFile("./wilson test .txt");
    string myArray[sz];

    if(myFile.good())
    {
        for(int i=0; i<sz; i++)
            myFile >> myArray[i];
        myFile.close();
    }

    for (int i = 0; i < sz ; ++i)
        cout << myArray[i] << endl;

}


using this as "wilson test .txt"
ity ujnbjkl;
ity ujnbjkl;
ity ujnbjkl;
ity ujnbjkl;
ity ujnbjkl;
ity ujnbjkl;
ity ujnbjkl;
ity ujnbjkl;
ity ujnbjkl;


there is no problem -- is your problem with your input?
Sometimes my compiler doesn't "compile" correctly and I have to make a new project. If everything is working correctly for others you may try just copying your code into a fresh project.
// Project 6.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;





int main()
{
ifstream myFile("c:\\Users\\Administrator\\Downloads\\data file for project 6 summer version.txt");

if(myFile.is_open())
{
string myArray[6];

for(int i=0; i<6; i++)
{
myFile >> myArray[i];
}
cout << myArray[5];
}


system("PAUSE");
return 0;

}

After running this code I get a 76...perhaps I should create two separate arrays...one for name and another for score?? and If so how would I go about cherry picking the content in the text file for each specific array?
It seems your code is working now.
perhaps I should create two separate arrays...one for name and another for score?
That's one approach. Another would be to define a struct to hold all the data for a single row.

I'm always wary of solutions which assume the file will contain a specific number of lines (in this case 3), I'd prefer more generalised code which could cope with a file containing more or fewer lines. Below, I've used a fixed size array to hold a maximum of 20 lines, and the actual number of rows read are counted during the input loop.
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream myFile("input.txt");

    if (!myFile.is_open())
    {
        cout << "file is not open" << endl;
        // system("PAUSE");
        return 1;
    }
    
    const int size = 20;
    string names[size];
    string scores[size];
    
    int count = 0;
    while (myFile >> names[count] >> scores[count])
    {
        count++;
    }
        
    cout << "Number of records read: " << count << endl;
    
    for (int i=0; i<count; i++)
    {
        cout << names[i] << " " << scores[i] << endl;
    }

    // system("PAUSE");
    return 0;
}



And the same thing using a structure, and just a single array:
1
2
3
4
struct person {
    string name;
    int score;
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    const int size = 20;
    person people[size];
    
    int count = 0;
    while (myFile >> people[count].name >> people[count].score)
    {
        count++;
    }
        
    cout << "Number of records read: " << count << endl;
    
    for (int i=0; i<count; i++)
    {
        cout << people[i].name << " " << people[i].score << endl;
    }
Last edited on
This works beautifully!! Thank you so much guys!!!
Topic archived. No new replies allowed.