inputing text files into arrays

I am working on some college work and having an issue with my assignment. The problem I am having is that my program won't open the txt file. I've read though my book and some of the documents on the website and seeing that much of what i have on my program almost matches. I know you're not suppose to help with homework but a pointer down the road or some tips/hints of whats the problem would be great. Here is my code currently.
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
//Christopher J. Lacour//
//Voting Array//
//Feb 07, 2013//

#include <iostream>                                          //Declarations//
#include <string>
#include <fstream>
#include <iomanip> 

double percentage(int val1, int val2);                       //setting the function to gain percentages//

using namespace std;

int main()
{
	string candidate[100];
	int vote[100];                                           //declaring variables for the program//
	int numCandidate = 0, numVotes = 0, totalVotes = 0, candidateCounter = 0;

	ifstream candidateList;
	candidateList.open("candidateList.txt");                //opens the text file for the candidates//

	if (candidateList.is_open())
	{
		while(candidateList.good())
		{
			getline(candidateList, candidate [candidateCounter]);
			candidateCounter++;
		}
	}

	for (int i = 1; i < numCandidate; i++)
	{
		cout << "Enter votes for " << candidate [i - 1];   //selects the candidate from the list//
		cin >> numVotes;                                   //user inputs the amount of votes for each candidate//
		vote [i - 1] = numVotes;
		totalVotes = totalVotes + numVotes;                  
	}

	candidateList.close();

	double percentage(int val1, int val2);                //calling function//

	system("PAUSE");
	return 0;
}

double percentage(int val1, int val2)                    //function calculates the percentage of the votes//
{
	double perc =(val1/val2) * 100;
	return perc;
}


If any more information is needed I'll gladly post, like I said, I'm not looking for a direct answer, just something that'll point me into the right path. Thanks for the help.
Last edited on
I'm also new, but #include <vector> and try something like this:

 
#include <vector> 



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
int main()
{
	string candidate[100];
	int vote[100];                                           //declaring variables for the program//
	int numCandidate = 0, numVotes = 0, totalVotes = 0, candidateCounter = 0;

	vector<string> v;
	ifstream in("candidateList.txt");
	string line;
	while(getline(in, line)) {
		v.push_back(line);
		candidateCounter++;
	}
	for(int i = 0; i < v.size(); i++)
		cout << v[i] << endl;


	for (int i = 1; i < numCandidate; i++)
	{
		cout << "Enter votes for " << candidate [i - 1];   //selects the candidate from the list//
		cin >> numVotes;                                   //user inputs the amount of votes for each candidate//
		vote [i - 1] = numVotes;
		totalVotes = totalVotes + numVotes;                  
	}

	in.close();

	double percentage(int val1, int val2);                //calling function//

	system("PAUSE");
	return 0;
}

double percentage(int val1, int val2)                    //function calculates the percentage of the votes//
{
	double perc =(val1/val2) * 100;
	return perc;
}


with this being the main difference:
1
2
3
4
5
6
7
8
9
vector<string> v;
	ifstream in("candidateList.txt");
	string line;
	while(getline(in, line)) {
		v.push_back(line);
		candidateCounter++;
	}
	for(int i = 0; i < v.size(); i++)
		cout << v[i] << endl;


This outputted the contents of candidateList.txt in the root folder of the program. This should get you on your way
Last edited on
Thanks for the help.
Topic archived. No new replies allowed.