Passing Input File into Parallel Arrays

Hello,

I am having trouble passing a file into parallel arrays. Based on the code below, I get a bunch of jumbled numbers on my cout screen that don't even show up in the input file. I am at a loss :( Any help would REALLY be appreciated.

The input file is as follows:

ANDERSON
12
BROWN
4
CLARK
25
DAVIS
7
GARCIA
8
GONZALEZ
23
HARRIS
24
HERNANDEZ
15
JACKSON
18
JOHNSON
2
JONES
5
LEE
22
LOPEZ
21
MARTIN
17
MARTINEZ
11
MILLER
6
MOORE
16
RODRIGUEZ
9
SMITH
1
TAYLOR
13
THOMAS
14
THOMPSON
19
WHITE
20
WILLIAMS
3
WILSON
10

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void readData(string[], int[]);

int main()
{
	string name[25];
	int frequency[25];
	readData(name, frequency);
	for (int j = 0; j < 25; j++)
	{
		cout << name[j] << endl;
		cout << frequency[j] << endl;
	}

	system("pause");
	return 0;
}

void readData(string name[], int frequency[])
{
	ifstream inputFile;
	inputFile.open("prog1.txt");
	for (int k = 0; k < 25; k++)
	{
		inputFile >> name[k] >> frequency[k];
	}
	inputFile.close();
}


Your code and file works for me... One thing u should do is test to make sure u are still reading in the file instead of just looping 25 times.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void readData(string name[], int frequency[])
{
	ifstream inputFile;
	inputFile.open("prog1.txt");
        int k=0;
        while(inputFile)
        {
                if(k==25)
                {
                      break;
                }
		inputFile >> name[k] >> frequency[k];
                k++;
        }	
	inputFile.close();
}

That'll hopefully make the issue known. Are u sure the .txt file is in the right location (ie: the same folder that the .exe is located) ? Else, with ur debugger u should break it at line 29 to see what ur variables are storing...because it should 'work' as is.
Last edited on
Haha, I'm an idiot. I had the text file ONE folder above and didn't even realize it. The little things, I swear...thanks!
Topic archived. No new replies allowed.