Creating a quiz with classes

I am having troule implementing part of my class for a quiz program that I created. I am having two problems: firstly being that I can't open my .txt file for the quiz, and second I can't seem to get the 'answers' part of the quiz right. The user doesn't have to input an answer, they just have to hit 'enter' to see the answer. I also need to give them the option to go to the next question or quiz at anytime.

main function:
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
#include "stdafx.h"
#include "Quiz.h"
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;
using namespace System;

int main()
{
   
	string qf;
	char answer;
	string ans;
	string choice;

	cout << "Welcome to the quiz program" << endl;
	do{
		cout << "Do you have a quiz file? (Y/N):";
		cin >> answer;
		if (!cin.good())
		{
			cin.clear();
			cin.ignore(1000, '\n');
			answer = 'N';

		}
		if (toupper(answer) == 'Y')
		{
			cin.ignore(1000, '\n');
			cout << "What is the path and name of the file? :";
			getline(cin, qf);
			Quiz q = Quiz(qf);
			if (!q.getErrstat())
			{
				cout << "Your file has " << q.getQcount() << " questions." << endl;
				cout << "Select question number or press <enter> for next question (" << q.getQnum() << "): ";
				getline(cin, choice);

				if (choice == "")
				{
					cout << "Your question is: " << endl << q.getQ() << endl;
					cout << "Hit enter for the answer";
					cin >> ans;
					if (ans == "")
					{
						cout << "The answer is: " << q.getA();
					}
					else
					{
						cout << "Please hit enter" << endl;
					}
				}
				else
				{
					int c = atoi(choice.c_str());
					cout << "Your questions is: " << endl << q.getQ(c) << endl;
					cout << "Hit enter for the answer";
					if (ans == "")
					{
						cout << "The answer is: " << q.getA(c);
					}
					else
					{
						cout << "Please hit enter" << endl;
					}

				}
			}
			else
			{
				cout << "Attempt to open quiz file " << qf << " failed. Error: " << q.getErrmsg() << endl;
			}
		}
	} while (choice != "0");
	system("pause");
    return 0;
}

  


quiz class:

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
#include "stdafx.h"
#include "Quiz.h"
#include <string>
#include <vector>
#include <fstream>

using namespace std;
using namespace System;

Quiz::Quiz(string qf)
{
	ifstream fin;
	string s;

	qcount = 0;
	ecode = false;
	emsg = "";
	fin.open(qf.c_str());
	if (fin.is_open())
	{
		while (!fin.eof())
		{
			getline(fin, s);
			Q.push_back(s);
			getline(fin, s);
			A.push_back(s);
			qcount++;
		}
		fin.close();
		qnum = 1;

	}
	else
	{
		ecode = true;
		emsg = "Unable to open file.";
		qnum = 0;
	}

}
string Quiz::getQ()
{
	if (qnum <= qcount)
	{
		return Q[qnum - 1];
	}
	else
	{
		return "That's all folks!";
	}
}
string Quiz::getQ(int n)
{
	string s;
	if (n <= qcount)
	{
		s = Q[n - 1];
		qnum = n;
	}
	else
	{
		s = "Illegal question number";
	}
	return s;
}

int Quiz::getQcount()
{
	return qcount;
}
bool Quiz::getErrstat()
{
	return ecode;
}
string Quiz::getErrmsg()
{
	return emsg;
}
int Quiz::getQnum()
{
	return qnum;
}
string Quiz::getA()
{
	if (qnum <= qcount)
	{
		return A[qnum-1];
	}
	else
	{
		return "That's all folks!";
	}
}
string Quiz::getA(int n)
{
	string s;
	if (n <= qcount)
	{
		s = Q[n - 1];
		qnum = n;
	}
	else
	{
		s = "Illegal question number";
	}
	return s;
}


quiz header file:
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
#pragma once

#include<vector>
using namespace std;

class Quiz
{
public:
	Quiz(string qf);
	string getQ();
	string getQ(int n);
	string getA();
	string getA(int n);
	int getQcount();
	int getQnum();
	bool getErrstat();
	string getErrmsg();

private:
	vector<string> Q;
	vector<string> A;
	int qcount;
	int qnum;
	bool ecode;
	string emsg;
	

};
Last edited on
It worked on my PC. At least I could open a file.
Maybe just a typo in the filename.
When testing a program it's normally good practize to output the input the program has received.

Really? Well atleast I coded that part right. I must be getting the file location/name wrong some how. I updated my code a bit for the 'answer' part, although I still can't test it because I can't open the file I need yet.
After you have successfully loaded the file you put a loop.
Something like that.
1
2
3
4
5
6
7
8
9
10
11
bool running = true;

while(running)
{
cout << "Select question number or press <enter> for next question or press 0 to quit 
(" << q.getQnum() << "): ";
getline(cin, choice);
if (choice == "0")
  running = false;
}

Fixed the hitting enter to get the answer problem for both; still working on looping it to get to the next question. Any hints? I am stuck. Ignore the comments please.


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
#include "stdafx.h"
#include "Quiz.h"
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;
using namespace System;

int main()
{
   
	string qf;
	char answer;
	string ans;
	string choice;
	int i;
	
	


	cout << "Welcome to the quiz program" << endl;
	do{
		cout << "Do you have a quiz file? (Y/N):";
		cin >> answer;
		answer = toupper(answer);
		if (!cin.good())
		{
			cin.clear();
			cin.ignore(1000, '\n');
			answer = 'N';



		}

		if (answer == 'Y')
		{
			cin.ignore(1000, '\n');
			cout << "What is the path and name of the file? :";
			getline(cin, qf);
			Quiz q = Quiz(qf);
			if (!q.getErrstat())
			{

				cout << "Your file has " << q.getQcount() << " questions." << endl << endl;

				for (i = 0; i <= q.getQcount(); i++)
				{
					
					cout << "Select question number, press '0' to quit, or press <enter> for next question (" << q.getQnum() << "): ";
					getline(cin, choice);



					if (choice == "")
					{
						
						cout << "Your question is: " << endl << q.getQ() << endl;
						cout << "Hit enter for the answer";
						getline(cin, ans);
						if (ans == "")
						{
							cout << endl << "The answer to " << q.getQnum() << " is: " << q.getA() << endl;

						}
						else
						{
							cout << "Please hit enter" << endl;
						}

						//  q.getQ() = q.getQ(2);
						//	q.getQ() = q.getQ(q.getQnum());
						//  a.getA() = a.getQ(q.getQnum());
					

					}
					else if (choice == "0")
					{
						cout << "Thanks for using my program" << endl<<endl;

						break;

					}
					else
					{
						int c = atoi(choice.c_str());
						cout << "Your questions is: " << endl << q.getQ(c) << endl;
						cout << "Hit enter for the answer";
						getline(cin, ans);
						if (ans == "")
						{
							cout << "The answer to " << q.getQnum() << " is: " << q.getA(c) << endl;
							
							
						}
						else
						{
							cout << "Please hit enter" << endl;
						}


					}
					
				}

			}
			else
			{
				cout << "Attempt to open quiz file " << qf << " failed. Error: " << q.getErrmsg() << endl;

			}



		}
		
	} while (answer == 'Y');
	system("pause");
    return 0;
}
Last edited on
Topic archived. No new replies allowed.