No output when reading from txt file

I'm trying to read a text file I created that has the answers for a True/False test consisting of 20 questions as well as student IDs and their answers for the test. I'm trying to output their student ID, their answers, their score, and their grade. I'm banding my head against the wall here because I feel everything looks right but when I run the program I don't get any output or "File not Found." I'm not sure if I have the text file located in the correct place of if my code is wrong. I have the text file located in my <project name>/Debug folder and have added it to my project under the <Source File> tab and set the Content to "Yes".

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

const int NUMBER_OF_QUESTIONS = 20;
const int NUMBER_OF_STUDENTS = 150;

void readFile(ifstream& inFile);
void gradeTest(char studentAnswers[][NUMBER_OF_QUESTIONS], string studentID[], char testAnswers[]);
char studentGrade(int score);

int main()
{
	string studentID[NUMBER_OF_STUDENTS];
	char testAnswers[NUMBER_OF_QUESTIONS + 1];
	char studentAnswers[NUMBER_OF_STUDENTS][NUMBER_OF_QUESTIONS];
	ifstream inFile;

	readFile(inFile);

	inFile.getline(testAnswers, '/n');

	for (int i = 0; i < NUMBER_OF_STUDENTS; i++)
	{
		inFile >> studentID[i];
		inFile.get();
		for (int j = 0; j < NUMBER_OF_QUESTIONS; j++)
			studentAnswers[i][j] = inFile.get();
	}

	gradeTest(studentAnswers, studentID, testAnswers);

	return 0;
}
void gradeTest(char studentAnswers[][NUMBER_OF_QUESTIONS], string studentID[], char testAnswers[])
{
	
	for (int i = 0; i < NUMBER_OF_STUDENTS; i++)
	{
		cout << "Student ID: " << studentID[i] << endl;
		int score = 0;
		cout << "Answers: ";
		for (int j = 0; j < NUMBER_OF_QUESTIONS; j++)
		{
			cout << studentAnswers[i][j];
			if (studentAnswers[i][j] == testAnswers[j])
				score += 2;
			else if (studentAnswers[i][j] != testAnswers[j] && studentAnswers[i][j] != ' ')
				score -= 1;
		}
		if (score < 0)
			score = 0;

		cout << endl;
		char grade = studentGrade(score);
		cout << "Grade: " << grade << endl;
	}
}
char studentGrade(int score)
{
	double percentScore = static_cast<double>(score) / (NUMBER_OF_QUESTIONS * 2);
	cout << "Score: " << percentScore * 100 << "%" << endl;
	if (percentScore >= 0.9)
		return 'A';
	else if (percentScore >= 0.8)
		return 'B';
	else if (percentScore >= 0.7)
		return 'C';
	else if (percentScore >= 0.6)
		return 'D';
	else
		return 'F';
}
void readFile(ifstream& inFile)
{
	inFile.open("Scores.txt");
	if (!inFile)
		cout << "File not Found." << endl;

	exit(1);
}
Example of my text file:
1
2
3
4
5
6
7
8
9
10
TFFTFFTTTTFFTFTFTFTT

SID54301 TFTFTFTT TFTFTFFTTFT
SID59310 FTTFTFTTFTTFTTTFTFTT
SID39583 FTTFTFTTFTFFFTFTTFTF
SID38493 FTFTTFTFTFTTF TFFTTT
SID10394 FTTFTFTFFTFTFTTFTTFT
SID10006 FTFTFFFTFFTFTFTFFTFT
SID10007 FTFTFFTFTFTTTTFFTTFT
SID38558 FTFTTFTFFTFFFTFTFTFF
Line 82 looks like a problem:
 
    exit(1);
Line 82 looks like a problem:
 
    exit(1);



Wow! Looks like I was extremely tired last night when I finished this up. Everything is working perfectly now. Thank you.
Line 23 also looks like a problem. You may want to look up the documentation for getline().

Also why are you trying to use C-strings and arrays of char instead of std::strings?


Line 23 also looks like a problem. You may want to look up the documentation for getline().

Also why are you trying to use C-strings and arrays of char instead of std::strings?


We just learned about arrays and C-strings in this chapter so I felt the professor wanted us to use them.
Also, wouldn't it be easier to compare the student's test answers to the correct answers using a char array compared to a string variable?

Just trying to figure out why it would be the preferred method.
IMO, it's not any easier either way, however using a std::string will be safer. The only real differences between the array and a string is that a string knows it's size and it will grow to accommodate the data. You can iterate over a string exactly the same as you can the array.

Topic archived. No new replies allowed.