Start New Discussion within our Software Development Community

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 have attached a PNG of the console output. 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:

#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();
}
Attachments error.png 51.58 KB

Try modifying line 26

inputFile.open("prog1.txt", ifstream::in);

(edit) I know this was marked as solved, but in lieu of the OP providing a solution
I thought I'd try to provide one for reference.

This question has already been answered. Start a new discussion instead.