Files problem

A problem with files - it's not inputing the way I want..

First run the code with teacher, write the test. I did "What is 1+1" and the answer I put "2"
Then run the code again but with student, and take the test.
The test doesn't show up..
What's the problem here?
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  // This is a program that for the two users (student, teacher)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
	ifstream inputFile;
	ofstream outputFile;
	char choice,
		 person;
	string username,
		   password,
		   STUDENT_USERNAME,
		   STUDENT_PASSWORD,
		   TEACHER_USERNAME,
		   TEACHER_PASSWORD,
		   question,
		   name;
	int option,
		numberOfQuestions,
		counter = 1,
		questionNumber = 1,
		number,
		answer = 0,
		solution = 0,
		registration = 0;

	cout << "Are you a teacher or a student? Enter in t for teacher and s for student." << endl;
	cin >> person;

	while (person != 's' && person != 'S' && person != 't' && person != 'T')
	{
		cout << "Please choose a valid option." << endl;
		cin >> person;
	}

	if (person == 's' || person == 'S')
	{
		cout << "Welcome, student!\n";
		cout << "Please register your username.\n";
		cin >> STUDENT_USERNAME;
		cout << "Please register your password.\n";
		cin >> STUDENT_PASSWORD;
		cout << "\nYour registration is complete! Thank you for registering!" << endl;
	}

	if (person == 't' || person == 'T')
	{
		inputFile.open("Registration.txt");
		inputFile >> registration;
		if (registration == 1)
		{
			cout << "You have already registered!" << endl;
			inputFile.close();
		}
		if (registration == 0)
		{
			inputFile.close();
			outputFile.open("Registration.txt");
			cout << "Welcome, teacher!\n";
			cout << "Please register your username.\n";
			cin >> TEACHER_USERNAME;
			cout << "Please register your password.\n";
			cin >> TEACHER_PASSWORD;
			cout << "\nYour registration is complete! Thank you for registering!" << endl;
			registration++;
			outputFile << registration << endl;
			outputFile << TEACHER_USERNAME << endl;
			outputFile << TEACHER_PASSWORD << endl;
			outputFile.close();
		}
	}
	inputFile.open("Registration.txt");
	inputFile >> registration;
	inputFile >> TEACHER_USERNAME;
	inputFile >> TEACHER_PASSWORD;
	cout << "\nPlease log in.\n";
	cout << "Username: ";
	cin >> username;
	cout << "Password: ";
	cin >> password;

	while (!(username == STUDENT_USERNAME && password == STUDENT_PASSWORD) && 
		!(username == TEACHER_USERNAME && password == TEACHER_PASSWORD))
	{
	cout << "\nPlease log in.\n";
	cout << "Username: ";
	cin >> username;
	cout << "Password: ";
	cin >> password;
	}

	if ((username == STUDENT_USERNAME) && (password == STUDENT_PASSWORD))
	{
		cout << "Welcome Student! Would you like to take the test? Choose Y or N\n";
		cin >> choice;
		while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'n') 
		    {
				cout << "\nPlease choose a valid option." << endl;
				cin >> choice;
		   	}
		switch (choice)
		{
			case 'y':
			case 'Y': cout << "So you wish to take the test!" << endl;
					  inputFile.open("Test.txt");
					  inputFile >> name;
					  cout << name << endl;
					  cin >> answer;
					  inputFile >> number;
					  cout << number << endl;
					  outputFile.open("Results.txt");
					  outputFile << answer;
					  cout << "Your test answers are saved!" << endl;
					  outputFile.close();
					  inputFile.close();
				break;
			case 'n':
			case 'N': cout << "" << endl;
				break;
		} 
	}

	if ((username == TEACHER_USERNAME) && (password == TEACHER_PASSWORD))
	{
		cout << "Welcome Teacher! What would you like to do?" << endl;
		cout << "1. Write a test." << endl;
		cout << "2. Check the test scores." << endl;
		cout << "3. Nothing - terminate the program." << endl;
		cin >> option;
		while (option != 1 && option != 2 && option != 3)
			 {
				cout << "Please choose a valid option." << endl;
				cin >> option;
			 }
		switch (option)
		{
			case 1: cout << "So you chose to write a test!" << endl;
					outputFile.open("Test.txt");
					cout << "How many questions are there going to be?" << endl;
					cin >> numberOfQuestions;
					while (numberOfQuestions <= 0)
					{
						cout << "Please enter in a valid number of questions that is not negative or zero." << endl;
						cin >> numberOfQuestions;
					}
					cout << "So you choose to have " << numberOfQuestions << " questions on the test." << endl;
					while (counter <= numberOfQuestions)
					{
						cin.ignore(1000,'\n');
						cout << "What is the question for question #" << questionNumber << "?" << endl;
						getline(cin,question);
						cout << "What is the answer to this question?" << endl;
						cin >> solution;
						outputFile << question << endl;
						outputFile << solution << endl;
						counter++;
						questionNumber++;
					}
					outputFile.close();
					cout << "Your questions and answers are saved!" << endl;
					break;
			case 2: cout << "So you wish to check the test scores!" << endl;
					inputFile.open("Results.txt");
					inputFile >> answer;
					cout << answer << endl;
					inputFile.close();
					break;
			case 3:
				break;
		} 
	}
	
		

		return 0;
	}

Hi,

So the program runs as expected, the break on line 164 ultimately caused the program to end. You need another loop to allow for parts of the program to be run again.

But before you do any of that, you need some reorganisation.

First up, functions - you need them. Functions (including main) should not have more than 40 lines of code, some go for even less. Candidates for things that go in functions, could be switch cases, the body of a loop, a compound statement (code between braces) and anything that is a logical task like login or printing a menu for example.

If you use functions properly, you will be able to organise you code more logically.

I really dislike no, really hate constructs like the following, and feel like strangling whoever teaches that crap: :+D

while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'n')

First of all they are error prone, UGLY, non-scalable and generally a disaster - did I mention UGLY?

One can halve the logic by making use of the toupper or tolower function, that way you only need to compare 2 things, not 4.

Even better, use a switch with a default case. In some places you have both !!

I hope all goes well, and I hope that I haven't come across as being to harsh or anything - I mean well, and am just trying to promote writing better code :+)

Good Luck !! :+D
Thanks for the reply but it wasn't the first part I'm stuck with.
What I'm concerned is lines 107-118.
You have to run the program first time as a teacher role to write the question, so yes the break at line 164 ends the program. What you were supposed to do was after you write the questions, you run the program again, but as a student this time. You enter in that you want to take the test, but the test questions written previously by the teacher doesn't show up by inputting the files.

As for the function, I know registration and logins can be a function, what else can you put in there?
Last edited on
As for the function, I know registration and logins can be a function, what else can you put in there?


TheIdeasMan wrote:
Candidates for things that go in functions, could be switch cases, the body of a loop, a compound statement (code between braces) and anything that is a logical task like login or printing a menu for example.


but the test questions written previously by the teacher doesn't show up by inputting the files.


When opening a file, check to see that it worked. You don't do this on lines 108 and 114. Actually you don't seem to do this anywhere.

http://www.cplusplus.com/doc/tutorial/files/


Try using a debugger to see what the values of your variables are on a watch-list, see how they change as you step through the code 1 line at a time, and deduce where things go wrong.

Good Luck !!

I tried debugging, but it didn't seem to work out that well. I couldn't figure out what went wrong.
I tried debugging, but it didn't seem to work out that well. I couldn't figure out what went wrong.


What progress did you make? Did the file open correctly? Could you read values from it? What variables did you have on the watch list?

Another advantage of reorganising the code is that you might be able to track down errors easier.
I inputted the lines from the file, (I changed it to getline) but it showed filebuffer and I couldn't figure out what's wrong.
Topic archived. No new replies allowed.