Opening a file

I'm trying to write to a file and open it, but I only get my default statement.

1
2
3
4
5
6
7
8
 void getFile(ifstream& writeFile) 
{
	writeFile.open("\n ch9ex6.txt");
	if (!writeFile) 
	{
		cout << "There was an error opening the input file" << endl;
		exit(1);
	}


What am I missing..
closed account (48T7M4Gy)
1
2
writeFile.open("ch9ex6.txt");
if(!writeFile.is_open())
Last edited on
I'm not sure if the file is being created.. maybe that's the issue.

here's the whole code:

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

const int NUM_OF_QUEST = 20;
const int NUM_OF_STUDENTS = 10;

void getFile(ifstream& writeFile);
void markAnswr(char studentAnswers[][NUM_OF_QUEST], string studentIDs[], char correctAnswers[]);
char grade(int score);

//main function to get and read the answers
int main()
{

	string studentIDs[NUM_OF_STUDENTS];
	char correctAnswers[NUM_OF_QUEST];
	char studentAnswers[NUM_OF_STUDENTS][NUM_OF_QUEST];
	ifstream writeFile;

	getFile(writeFile);

	// Read the set of correct answers
	for (int i = 0; i <= NUM_OF_QUEST; i++) 
	{
		correctAnswers[i] = writeFile.get();
		cout << correctAnswers[i];
	}

	// Read the input of each student ID and answers
	for (int i = 0; i <= NUM_OF_STUDENTS; i++)
	{
		writeFile >> studentIDs[i]; 
		writeFile.get(); 
		for (int a = 0; a <= NUM_OF_QUEST; a++)
			studentAnswers[i][a] = writeFile.get(); 
	}

	markAnswr(studentAnswers, studentIDs, correctAnswers);


	return 0;
}
//function to read each string and mark the answers correct/incorrect
void markAnswr(char studentAnswers[][NUM_OF_QUEST], string studentIDs[], char correctAnswers[]) 
{

	for (int x = 0; x < NUM_OF_QUEST; x++)
	{
		cout << correctAnswers[x]; 
	}
	cout << endl;

	for (int i = 0; i < NUM_OF_STUDENTS; i++) 
	{
		int score = 0;
		for (int a = 0; a < NUM_OF_QUEST; a++) 
		{
			if (studentAnswers[i][a] == correctAnswers[a]) 
			{
				score += 2; 
			}
			else if (studentAnswers[i][a] != correctAnswers[a] && studentAnswers[i][a] != ' ') 
			{
				score -= 1; 
			}

			cout << "Correct Answer: " << static_cast<char>(correctAnswers[a]) << " Student Answer: " << studentAnswers[i][a] << endl;
		}
		if (score < 0)
			score = 0;

		cout << studentIDs[i] << " score = " << score << endl;
	}
}

char grade(int score) 
{
	if (score == 0)
		return 'F';


}

void getFile(ifstream& writeFile) 
{
	writeFile.open("C:\\Users\\Kristina\\Desktop\\CIS242\\Week8\\ch9ex6.txt");
	if (!writeFile.is_open())
	if (!writeFile) 
	{
		cout << "There was an error opening the input file" << endl;
		exit(1);
	}
}
closed account (48T7M4Gy)
I meant that my two lines replace the corresponding two lines you had in your original snippet. So 89,90 replace 89,90 & 91

Just use the simple file reference, see where the file is created and put the real data file there.
Topic archived. No new replies allowed.