Why aren't values being stored in this array?

I'm reading in files from a txt file and attempting to store it in an array. Something with my logic is messed up, as nothing is being stored in the array.
I'm aware inFile is not needed.

This is the function for the input storage.

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 <iomanip>
#include <stdlib.h>
#include <string.h>
#include <string>     //These are all stored in a header file in my program
#include <iostream>
#include <fstream>

using namespace std;


void GetName(ifstream &InFile, const int AR_SIZE, string nameArrayF[], int idsArF[], float balanceArF[])
{
	string name;
	int index;
	ifstream inFile;


	for(index = 0; index < AR_SIZE; index++)
	{
		getline(inFile, nameArrayF[index]);

		inFile >> idsArF[index];
		inFile.ignore(1000, '\n');

		inFile >> balanceArF[index];
		inFile.ignore(1000,'\n');
	}

	inFile.close();
}


The input file looks like this:
1
2
3
4
5
6
7
8
9

Jacob Lingston
1544 15.25
Ricardo Bob
1122 1423.20
Kat Miston
1234 53.33



My calling function in main is:

GetName(inFile, AR_SIZE, nameArray, idsAr, balanceAr);

//AR_SIZE = 10

Help?
Last edited on
Yeah, don't include ifstream &InFile in your function argument. Go to the tutorials under file I/O and see what that says. It's pretty useful and may have what you are looking for :)
Thank you for the reply - I removed them but it is still not reading into the array.

I have gone over the tutorials but don't quite understand them, that's why I am asking the question.
You have a parameter InFile and a local-to-GetName variable inFile. You do not use InFile, and you do not open inFile, so there can be no successful extraction from input streams in GetName.
I made the following changes and nothing is being stored still.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void GetName(ifstream &InFile, const int AR_SIZE, string nameArrayF[], int idsArF[], float balanceArF[], string userInput)
{
	string name;
	int index;
	index = 0;

	InFile.open(userInput.c_str());

	while(InFile && index < AR_SIZE)
	{
		getline(InFile, nameArrayF[index]);

		InFile >> idsArF[index];
		InFile.ignore(1000, '\n');

		InFile >> balanceArF[index];
		InFile.ignore(1000,'\n');
	}

	InFile.close();
}


userInput is prompted in main to get the name of the file to be used for input.
Last edited on
bump - still do not know solution
You make no report if the file is not opened and index is always 0.
Thank you for the quick reply.

Is my code not opening the file? I thought to open a file you only had to define ifstream, open the file using .open(filename.txt), and then proceed to close it, which I believe is what I am doing here. If not can you point out which line my error begins on?

EDIT:
I am looking at the example on http://www.cplusplus.com/doc/tutorial/files/
I implemented index++ at the end of the while loop.
Last edited on
Attempting to open a file and opening a file are different things.

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
void GetName(ifstream &InFile, const int AR_SIZE, string nameArrayF[], int idsArF[], float balanceArF[], string userInput)
{
	string name;
	int index;
	index = 0;

	InFile.open(userInput.c_str());

	if (!InFile.is_open())
	{
		std::cerr << "Unable to open file \"" << userInput << "\"\n";
		return;
	}

	while(InFile && index < AR_SIZE)
	{
		getline(InFile, nameArrayF[index]);

		InFile >> idsArF[index];
		InFile.ignore(1000, '\n');

		InFile >> balanceArF[index];
		InFile.ignore(1000,'\n');

		++index;
	}

	InFile.close();
}


How exactly do I see this error message? This function is almost simply skipped over and outputs nothing (since it is a void function).

The attempt to open a file nor opening the file work. Not sure if it was misunderstood but I want to open the file, not attempt to open it.
Last edited on
Topic archived. No new replies allowed.