Input to parallel arrays

Hello,
I'd like to apologize for posting a homework problem. I really try to figure these things out by myself, but I've been at this little problem for a few hours now and it is past being a productive learning experience.

I am trying to read an input file and populate two arrays. The input file is of a cd collection like this:

cdtitle
bandname
cdtitle
bandname
etc
etc

I need to load the file using a function and populate two parallel arrays, so that I can sort and manipulate them later. I'm pretty sure the problem is in my while loop. I have tried changing the parameter to !cdList.eof(). I have also tried using getline(cdList, cdTitle[incr]) to populate the arrays. I get unhandled exception errors and sometimes the program prompts me for the input file as expected, but will then either do nothing, crash, error, or display (). I also have tried having the input file in both debug folders, i hope thats where its supposed to be.

I think my problem is an array out of bounds because of improperly using an eof controlled while loop, but I haven't been able to find an applicable example in the forum or anywhere else online. Any help is appreciated, thank you!

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

const int ARRAY_SIZE = 100;
string cdTitle [ARRAY_SIZE];
string bandName [ARRAY_SIZE];


int loadData(string pathName);  
//void showAll(int count);
//void showCDsByBand (int count, string name);
//void showCDsByTitle (int count, string title);

int loadData(string pathName)
{
	ifstream cdList;
	string title, band;
	int incr = 0;
	cdList.open(pathName);
	while(cdList)
	{
		getline(cdList, title);
		getline(cdList, band);
		cdTitle[incr] = title;
		bandName[incr] = band;
		incr++;
	}	
	
	if(cdList.fail())
		incr = -1;
	cdList.close();
	return incr;
}



int main()
{
	string fileName;
	int cdCount;

	cout << "Welcome to Uriah's CD Database!" << endl;
	cout << "Please enter the filename: ";
	cin >> fileName;
	cdCount = loadData(fileName);
	if(cdCount == -1)
	{
		cout << "Error loading file." << endl;
		abort();
	}
	for(int i = 0; i < cdCount; i++)
	{
		cout << cdTitle[i] << " (" << bandName[i] << ")" << endl;
	}

	system("PAUSE");
	return 0;
}
Last edited on
The way you have it now is not the way to loop through a file (it will loop forever as long as the file exists). You said you tried the getline method which should work correctly.

1
2
3
4
5
6
7
while(getline(cdList, title))
{
	getline(cdList, band);
	cdTitle[incr] = title;
	bandName[incr] = band;
	incr++;
}	


You will want to check if the file exists at the beginning of the function and return immediately if it doesn't (you are checking at the end right now). Everything else seems set up correctly.
Thanks James!
I hadn't tried it exactly that way yet, but it makes sense. I implemented your code and put the error statement first and the function returns -1 and I get my "Error loading file." message. If i comment out the error protection, incr stays at 0 because it will display" () "and end the program.

I'm not sure what I am doing wrong when it comes to loading the input file. There are two debug folders in the project file and I have tried putting it in both of them, and I know I am typing the correct filename with the correct extension. There must be something else I am overlooking. I am using visual studio 2010. Is there a way to check which directory it is looking in? Do i need to import the file into the project somehow? I had tried adding it as a source file prior to posting, but it change anything then.

Here is the new function code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int loadData(string pathName)
{
	ifstream cdList;
	string title, band;
	int incr = 0;
	cdList.open(pathName);
	if(cdList.fail())
		incr = -1;
	else
	{
		while(getline(cdList,title))
		{
		
			getline(cdList, band);
			cdTitle[incr] = title;
			bandName[incr] = band;
			incr++;
		}	
	}	
	cdList.close();
	return incr;
}
Last edited on
If you are running it from the IDE you should put it in your project folder (whatever you named it) not the debug folder.

Otherwise you could try hardcoding the filename instead of having the user enter it, including the .txt, and see if it has the same problem. I am guessing your file is just in the wrong place though.
You were right, wrong file location. Thank you so much for your help!
Topic archived. No new replies allowed.