can't get program to read file


The program only outputs a black screen, nothing else. I'm not even sure if I got the program to at least save the first line of the file to use in comparison to the rest.
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
  #include <iostream> // std::cout
#include <fstream>  // std::ifstream
#include <string>

	double MeanExamScore; //must be a real number with 2 digits of precision
	int SumOfAllScores; //sum of all scores
	int NumScores=25; //number of scores
	char Answer;
	int point;
	char T, t, F, f;

using namespace std;

int main()
{
	string students[25];
	string answerkey[30];
	string questions[30]; 
	ifstream myfile ("xfile.txt");

	std::ifstream ifs ("xfile.txt", std::ifstream::in);

	int a = 0;
	int b = 0;

	getline(myfile, answerkey[30], ' ');

	while(!myfile.eof())
	{
		getline(myfile, students[a], ' ');
		getline(myfile, questions[b], ' ');
	}

	
	MeanExamScore = SumOfAllScores / NumScores; //this calculates the Mean Exam Score

	//the following block will output each student's name and their test score


	//the following block will output the histogram
	cout << "Frequency\n" << 
	cout << "---------\n" <<
	cout << '\n' <<
	cout << "Score" << "        " << "Obtained By" << "        " << "    5     10     15     20     25" << endl;
	cout << "-----" << "        " << "-----------" << "        " << "----|------|------|------|------|" << endl;
	cout << "0...5" << "        " << endl;
	cout << "6...10" << "        " << endl;
	cout << "11...15" << "        " <<endl;
	cout << "16...20" << "        " << endl;
	cout << "21...25" << "        " << endl;
	cout << "26...30" << "        " << endl;



		return 0;
}
You're trying to open the file twice here. You don't need the second one.
1
2
3
ifstream myfile ("xfile.txt");

std::ifstream ifs ("xfile.txt", std::ifstream::in);


Also, you're passing in an uninitialized reference here, since answerkey[30] is out of bounds. I'm guessing it probably crashed here.

getline(myfile, answerkey[30], ' ');
Topic archived. No new replies allowed.