We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,688 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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 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.58KB
2
Contributors
1
Reply
18 Hours
Discussion Span
9 Months Ago
Last Updated
2
Views
Question
Answered
pattilupwned
Newbie Poster
13 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Self-Answered as of 9 Months Ago

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.

Suzie999
Posting Whiz
322 posts since Jul 2010
Reputation Points: 49
Solved Threads: 15
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0628 seconds using 2.68MB