7 Replies - 3161 Views - Last Post: 08 September 2012 - 12:10 PM Rate Topic: -----

#1 pattilupwned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-March 12

Passing Input File into Parallel Arrays

Posted 08 September 2012 - 11:08 AM

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. An image of the console output is included. 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();
}



Posted Image

Sorry, it just included an image link at the bottom when I thought it would actually post the full image in the post. But the tiny image link will lead you to a screen cap of my console output.

This post has been edited by GunnerInc: 08 September 2012 - 11:18 AM
Reason for edit:: fixed image tags


Is This A Good Question/Topic? 0
  • +

Replies To: Passing Input File into Parallel Arrays

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3553
  • View blog
  • Posts: 11,014
  • Joined: 05-May 12

Re: Passing Input File into Parallel Arrays

Posted 08 September 2012 - 11:20 AM

Works just fine for me. Have you tried stepping through your code with a debugger? Specifically, inspect the values you are reading on line 29.

Also make sure that you are opening the correct file. If you are opening an incorrect file, the open() call should return an error code, at which point you should not try to read data out of the stream.
Was This Post Helpful? 0
  • +
  • -

#3 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: Passing Input File into Parallel Arrays

Posted 08 September 2012 - 11:23 AM

Well, although I am not sure what an array as parameter does, I'm sure it is not passing be reference/address. So, when you pass name and frequency, a copy of those arrays is created (google passing by value c++ for more information), and the original arrays remain unchanged. So, to overcome that, change the arrays is parameter of your readData to pointer to array. Besides, since you are already passing the address of first element of your array as parameter in main(), you won't have to modify your code much. However, if I'm wrong, please correct me.

EDIT : Oops, Ninja-ed by SkyDiver :P

EDIT 2 : Ignore me.

This post has been edited by aresh: 08 September 2012 - 11:56 AM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

Re: Passing Input File into Parallel Arrays

Posted 08 September 2012 - 11:25 AM

It seems to be working for me. Are you sure your file is opening correctly? Maybe you should check that it does.

The output when I ran the program.
Spoiler


Jim

This post has been edited by jimblumberg: 08 September 2012 - 11:26 AM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3553
  • View blog
  • Posts: 11,014
  • Joined: 05-May 12

Re: Passing Input File into Parallel Arrays

Posted 08 September 2012 - 11:54 AM

If you are using Visual Studio 2010 or 2012, you'll need to keep your prog1.txt up in your project directory. This is different from VS2008 (or was it VS.NET -- it's been so long that I've forgotten) where you had to take steps to put prog1.txt into the same directory as where your .exe is built: the Debug or Release directory. I have no idea how Code::Blocks handles the current working directory.

Any which way, if you check whether open() succeeds or fails, this will tell you right away if you have opened the file correctly.

This post has been edited by Skydiver: 08 September 2012 - 11:54 AM

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

Re: Passing Input File into Parallel Arrays

Posted 08 September 2012 - 11:55 AM

Quote

Well, although I am not sure what an array as parameter does, I'm sure it is not passing be reference/address. So, when you pass name and frequency, a copy of those arrays is created (google passing by value c++ for more information), and the original arrays remain unchanged. So, to overcome that, change the arrays is parameter of your readData to pointer to array. Besides, since you are already passing the address of first element of your array as parameter in main(), you won't have to modify your code much. However, if I'm wrong, please correct me.

Passing an array as a parameter is the same as passing a pointer (address). A copy is not made, you are passing the address, remember the address of an array is the array's name, the original arrays will not remain the same. There is nothing wrong with using array notation for a single dimensioned array, however there is also nothing wrong about using pointer notation either.

Corrected.

Jim
Was This Post Helpful? 1
  • +
  • -

#7 pattilupwned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-March 12

Re: Passing Input File into Parallel Arrays

Posted 08 September 2012 - 11:57 AM

Thank you all for the quick responses!

It ended up being the text file - I don't know what was up with it, but I copied and pasted the information into a new file, relabeled it, changed the name in my code and it works great now. Dunno if there's a bug in the file I received.

Thank you all again!
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

Re: Passing Input File into Parallel Arrays

Posted 08 September 2012 - 12:10 PM

This is why you should always check that opening a file succeeded and take action if it doesn't.
ifstream file("someFile");
if(!file)
  // OH OH file didn't open do something.


If you always check then you at least know that your program is finding/creating a file and you have one less problem to consider.

Jim

This post has been edited by jimblumberg: 08 September 2012 - 12:10 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1