Comparing user input always equals false. Why?

Pages: 12
I can't figure out why when I try to compare the users input answer to the answer in the array it always prints out incorrect, no matter if the answer is wrong or right. Can anyone tell me what I'm doing wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
		std::string qtype;
		std::string theAnswerIs;
		std::string ans[6];
		for (int i = 0; i < numquestions; i++)
				{
					qtype = myQuestions[i]->getQuestionType();
					std::cout << qtype << " " << myQuestions[i]->getValue() << "\n";
					myQuestions[i]->printOptions();
					theAnswerIs = myQuestions[i]->getAnswer();
					std::cout << "\n";
					std::cout << "Please enter your answer: ";
					std::cin >> ans[i];
						if (ans[i] == theAnswerIs)
						{
							std::cout << "You are correct" << std::endl;
						}
						else
						{
							std::cout << "Incorrect, the correct answer is: " << theAnswerIs << std::endl;
						}
					

					delete myQuestions[i];//dealocate
				}



The output on the console looks like this for the first two questions:
TF 1
5+5 = 55?

Please enter your answer: false
Incorrect, the correct answer is: false
TF 5
11+11=22?

Please enter your answer: true
Incorrect, the correct answer is: true

Any help would be greatly appreciated. Thanks.
Hi,

Try looking at your code with a debugger. It will save you days of staring at code :+)

Why do you have delete at the end of the for loop?

One should avoid new and delete anyway, use an STL container or a smart pointer instead. What is the type of myQuestions ?
I am using Visual Studio and using the start debugging option. Is that what you mean?

I have delete at the end of the loop to deallocate the array of myQuestions. Which I actually just moved it out of that loop and put it in its own loop after everything is done. What do you mean by "type of myQuestions"?

here is my whole class if that helps:
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
class Quiz : questions
{
private:
	std::string fileName;
	questions *myQuestions[10];
	int numberOfQuestions;
	std::string theAnswer;

public:
	int loadQuiz()
	{
		std::ifstream infile;
		infile.clear();
		//checking to see if user specified file exist, as well as making sure the user has used the correct format
		while (!infile.is_open())
		{
			std::cout << "Please enter the .txt file name: " << std::endl;
			std::getline(std::cin, fileName);
			//fileName += ".txt";
			infile.open(fileName.c_str(), std::ios::in | std::ios::binary);
			if (infile)
				break;
			std::cout << "Either the file doesn't exist or you forgot the .txt extension. ";
			std::cout << "Please try again. " << std::endl << std::endl;//promt user to try again if the file name is invalid
		}


		//std::ifstream infile("quiz.txt");
		std::streambuf *cinbuf = std::cin.rdbuf();       //save old buf
		std::cin.rdbuf(infile.rdbuf());             //redirect std::cin to infile.txt!

		std::string line;
		std::string theQuestion;
		std::string questiontype;
		//std::string theAnswer;
		int  questionvalue;

		while (true)//error check to ensure the file has an acceptable amount of questions
		{

			//get the number of questions from the first line in the file
			//std::getline(infile, line);
			std::getline(std::cin, line);
			numberOfQuestions = atoi(line.c_str());
			if (numberOfQuestions != 6)
			{
				std::cout << "Error! The quiz has too many or too litte questions. The desired amount is \'6\'" << std::endl;
				std::cout << "Please check the file to ensure there is an acceptable amount of questions." << std::endl;
				throw;
			}
			else
			{
				break;
			}
		}
		for (int count = 0; count < numberOfQuestions; count++)
		{
			//std::getline(infile, line);
			std::getline(std::cin, line);
			//get the next line with the question type and the value of the question
			int npos = line.size();
			int prev_pos = 0;
			int pos = 0;
			while (line[pos] != ' ')
				pos++;
			questiontype = line.substr(prev_pos, pos - prev_pos);
			prev_pos = ++pos;
			questionvalue = atoi(line.substr(prev_pos, npos - prev_pos).c_str()); // Last word

			if (questiontype == "TF")//do this if questiontype = TF
			{
				myQuestions[count] = new tfQuestion;
				//std::getline(infile, line);
				std::getline(std::cin, theQuestion);
				myQuestions[count]->setQuestion(theQuestion, questionvalue);
			}

			if (questiontype == "MC")//do this if questiontype = MC
			{
				myQuestions[count] = new mcQuestion;
				//std::getline(infile, line);
				std::getline(std::cin, theQuestion);
				myQuestions[count]->setQuestion(theQuestion, questionvalue);
			}

		}
		std::cin.rdbuf(cinbuf);//restore cin to standard input
		infile.close();//close the file stream
		return numberOfQuestions;
	}

	void displayQuizQuestions(int numquestions)
	{
		//print out the questions that have been processed
		std::string qtype;
		std::string theAnswerIs;
		std::string ans[6];
		for (int i = 0; i < numquestions; i++)
		{
			qtype = myQuestions[i]->getQuestionType();
			std::cout << qtype << " " << myQuestions[i]->getValue() << "\n";
			myQuestions[i]->printOptions();
			theAnswerIs = myQuestions[i]->getAnswer();
			std::cout << "\n";
			std::cout << "Please enter your answer: ";
			std::cin >> ans[i];
			while (true)
			{
				if (ans[i] == theAnswerIs)
				{
					std::cout << "You are correct" << std::endl;
					break;
				}
				else
				{
					std::cout << "Incorrect, the correct answer is: " << theAnswerIs << std::endl;
					break;
				}
			}
		}
		for (int i = 0; i < numquestions; i++)
		{
			delete myQuestions[i];//dealocate
		}
	}
}
Hi,

I just saw your other topic. Please don't start new topics about the same subject - it is ultimately a time waster for those who reply. Just add another question on the end of the existing topic, and it will be bumped to the top of the list of topics.

http://www.cplusplus.com/forum/beginner/211438/

I am using Visual Studio and using the start debugging option. Is that what you mean?


Yes, you should be able to step through the code 1 line at a time, keeping an eye on the values of the variables with a watch-list ; deduce where things go wrong.

What do you mean by "type of myQuestions"?


I see now, it's an array of pointer to the base class questions.

You shouldn't need to use pointers or new, just use a std::vector - it will make life much easier all around. Edit: Another option is smart pointers like std::unique_ptr.

getValue() returns an int, but ans is an array of std::string. Did you mean get Question on line 101 ?

Another thing, avoid having protected data in classes - it is just as bad as having public data.

Instead of having functions like setQuestion , consider having constructors with member initialization lists, and look into emplace_back .

Last edited on
Some more things:

If a member function does not alter the value of any member variable (the class state) then it should be marked const:

67
68
69
70
int getValue() const //gets the point value of the question
	{
		return value;
	}


Consider splitting the code into header files (I like *.hpp) and cpp files. Your IDE should have a wizard for making classes, which should automagically make both the header and cpp files.

IMO, there is too much code in the loadQuiz function, consider having call some private functions. The while and for loops could be functions of their own.

If you configure your editor to have indenting with spaces (4 say) and not tabs, it will display better on this site. This site converts tabs to 8 spaces - which gives excessive indenting.
Last edited on
The problem is I just starting out learning C++ I don't know much about vectors just yet, though I agree they are the better option. I have tried to get them to work in my program but kept failing so I used what I knew(somewhat lol). Also again I'm new to all of this so I'm not sure what you mean by "Instead of having functions like setQuestion , consider having constructors with member initialization lists, and look into emplace_back ." I barely know what I'm doing with what I have.

I'm not sure if getQustion is what i meant, however it works the way it is I just can't get the answer comparison to work at the moment. I will try changing it to getQuestion and see what happens though. I think I am honestly in over my head here haha. I am pretty lost right now. I thought I was doing good up until I had to allow a user to actually take the quiz, now I am trying to change stuff around to make everything work and getting lost in the process. Thanks for all your help so far though.
Perhaps you should review the tutorial at the top left of this page - there is a section on classes.

A constructor is a special function whose purpose is to initialize all the member variables.

A member initialization list is way of doing that with direct initialization instead of assignment.

emplace_back is way of calling a class constructor so as to set the value of an item in a container like std::vector. It is similar to push_back , but it calls the constructor.

I'm not sure if getQustion is what i meant, however it works the way it is I just can't get the answer comparison to work at the moment. I will try changing it to getQuestion and see what happens though.


Try to understand why it is a problem, rather than trying stuff (even though I suggested that was the problem). To that end, look in the debugger and you should see what I mean.
Okay I will review the tutorial on classes now. What do you mean by "look in the debugger" when I hit start debugging the console come up with the program running. Maybe i am missing something there ?
What do you mean by "look in the debugger" when I hit start debugging the console come up with the program running. Maybe i am missing something there ?


One can set a breakpoint on a particular line number; the execution will stop there. Then set up a watch list of the variables whose values you want to keep an eye on. Then step through the program 1 line at a time, see how the variables change.
Ahhhh. Okay I see the breakpoint stuff now. That's pretty cool I had no idea that was there thanks for showing me. So I reviewed both classes 1 and classes 2 tutorials and I kind of get what you were saying about constructors. I have rewrote my code on a new project (didn't want to mess up what I have now just in case) I used constructors (I'm pretty sure I did) after I got that working I tried to use vectors but couldn't get that quite yet. So I stuck with the array for now (i'll keep playing around with the vectors later). Now though the new program keep getting an stdthrow exception when I try to load the file. Which doesn't make sense why because I pretty much copy and pasted it minus changing some stuff due to extra header and cpp files. I was trying out the breakpoint you showed me and the exception seems to be thrown on the quiz.cpp file on line 83. Which to me doesn't make sense since it is exactly the same on my other project. I will post the quiz.h and quiz.cpp and maybe you can see something i can't. In any case changing all of this still doesn't help me with my original question pertaining to why I can't get the right output based on comparing what the user enter vs the answer from the file. Anyways thanks for your help so far you have made me learn quite a bit of stuff that I did not know before.

P.s. although I have rewrote my code in a different project I am still using my original code for now since I am a bit more familiar with it. I'm just playing around with the way you suggested so I can learn more.

this is my Quiz.h :
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
#pragma once
#include "questions.h"
#include "mcQuestions.h"
#include "tfQuestions.h"
#include <vector>
#include <fstream>
#include <cstdlib>
#include <limits>
#include <string>
using namespace std;
class Quiz :
	public questions
{
private:
	std::string fileName;
	//vector<string> *myQuestions = new vector<string> (10);
	//questions *myQuestions;
	questions *myQuestions[10];
	int numberOfQuestions;
	std::string theAnswer;
public:
	int loadQuiz();
	void displayQuizQuestions(int numquestions);

};




And here is my Quiz.cpp :

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
#include "Quiz.h"
using namespace std;



int Quiz::loadQuiz()
{
	ifstream infile;
		infile.clear();
		//checking to see if user specified file exist, as well as making sure the user has used the correct format
		while (!infile.is_open())
		{
			cout << "Please enter the .txt file name: " << endl;
			getline(std::cin, fileName);
			//fileName += ".txt";
			infile.open(fileName.c_str(), ios::in | ios::binary);
			if (infile)
				break;
			cout << "Either the file doesn't exist or you forgot the .txt extension. ";
			cout << "Please try again. " << endl << endl;//promt user to try again if the file name is invalid
		}


		//std::ifstream infile("quiz.txt");
		streambuf *cinbuf = cin.rdbuf();       //save old buf
		cin.rdbuf(infile.rdbuf());             //redirect std::cin to infile.txt!

		string line;
		string theQuestion;
		string questiontype;
		//std::string theAnswer;
		int  questionvalue;

		while (true)//error check to ensure the file has an acceptable amount of questions
		{

			//get the number of questions from the first line in the file
			//std::getline(infile, line);
			getline(cin, line);
			numberOfQuestions = atoi(line.c_str());
			if (numberOfQuestions != 6)
			{
				cout << "Error! The quiz has too many or too litte questions. The desired amount is \'6\'" << endl;
				cout << "Please check the file to ensure there is an acceptable amount of questions." << endl;
				throw;
			}
			else
			{
				break;
			}
		}
		for (int count = 0; count < numberOfQuestions; count++)
		{
			//std::getline(infile, line);
			getline(cin, line);
			//get the next line with the question type and the value of the question
			int npos = line.size();
			int prev_pos = 0;
			int pos = 0;
			while (line[pos] != ' ')
				pos++;
			questiontype = line.substr(prev_pos, pos - prev_pos);
			prev_pos = ++pos;
			questionvalue = atoi(line.substr(prev_pos, npos - prev_pos).c_str()); // Last word

			if (questiontype == "TF")//do this if questiontype = TF
			{
				myQuestions[count] = new tfQuestions;
				//std::getline(infile, line);
				getline(cin, theQuestion);
				(myQuestions[count])->setQuestion(theQuestion, questionvalue);
			}

			if (questiontype == "MC")//do this if questiontype = MC
			{
				myQuestions[count] = new mcQuestions;
				//std::getline(infile, line);
				getline(cin, theQuestion);
				(myQuestions[count])->setQuestion(theQuestion, questionvalue);
			}

		}
		cin.rdbuf(cinbuf);//restore cin to standard input//exception get thrown here!
		infile.close();//close the file stream
		return numberOfQuestions;
}

void Quiz::displayQuizQuestions(int numquestions)
{
	std::string qtype;
	std::string theAnswerIs;
	std::string ans[6];
	for (int i = 0; i < numquestions; i++)
	{
		qtype = myQuestions[i]->getQuestionType();
		std::cout << qtype << " " << myQuestions[i]->getValue() << "\n";
		myQuestions[i]->printOptions();
		theAnswerIs = myQuestions[i]->getAnswer();
		std::cout << "\n";
		std::cout << "Please enter your answer: ";
		std::cin >> ans[i];

		if (ans[i].compare(theAnswerIs) != 0)
		{
			std::cout << ans[i] << " is the correct answer" << theAnswerIs << '\n';
		}
		else
		{
			std::cout << "wrong";
		}
	}
}



Thanks again for your help!
Why are you using rdbuf ? Why can't you just use normal file I/O ?

Here's how to have a std::vector of pointer to base:

std::vector<questions*> myQuestions;

Why you don't need to use new anywhere:

Some false motivations for using new are: to obtain a pointer; and store that variable on the heap (as opposed to the stack). But STL containers already store their data with an internal pointer to the heap. The STL containers do all their own memory management. The biggest problem with new is that if an exception is thrown somewhere, the delete is never reached, so there is a memory leak.

Your original code didn't have a single delete that I could see.

Somethings you are making overly difficult. The line variable is really the number of questions, so why not make it an unsigned int or std::size_t ? That will make it easier to see if there are an acceptable amount of questions.

the size functions return a type of std::size_t , so you need to reflect that in the types of the variables that you assign that value to.
Last edited on
@TheIdeasMan, I will try what you suggested with vector and see if I can get it to work. Also for some reason if I take out the rdbuf and just use std::getline(infile, line); my program gives me errors I will give it another go then re post my updated code. Also in my original code I had one delete( delete myQuestions[i];) was I not using delete the right way there? Also the line variable gets the number of questions the question type and the point value of the questions if I'm not mistaken and reading my code right (this is the first time I have used rdbuf and all this
1
2
3
4
5
6
7
8
int npos = line.size();
		int prev_pos = 0;
		int pos = 0;
		while (line[pos] != ' ')
			pos++;
		questiontype = line.substr(prev_pos, pos - prev_pos);
		prev_pos = ++pos;
		questionvalue = atoi(line.substr(prev_pos, npos - prev_pos).c_str());

So I am a little confused on how its working. In any case I am going to try and see if I can follow your suggestions and hopefully I can get everything to work right. That being said I think I found the problem to what my original question was, though I can't figure out how to fix it. So I did this code:
 
std::cout << ans[i].size() << "\n" << theAnswerIs.size() << std::endl;

to see if the output of the strings were atleast the same size. The first question has an answer of false, so I ran the program entered false for the first question the above code printed out the size of ans[0] first which is what I typed in and had a size of 5, which is correct since false has 5 letters. But when the answer from the file is printed out from theAnswerIs.size it prints out a size of 6 even though the answer is false so it should be 5. This goes for every question, each answer when doing theAnswerIs.size() always has one more than it should so therefore when I try to compare between what the user entered as the answer and what the answer in the file is it never evaluates to equal. How can I go about fixing this problem? I have already checked and double checked the file to see if there were any extra spaces or characters and there aren't. Thanks for all your help so far I am learning a lot!
I figured out why the size of the strings were different. At the end of theAnsweris the was a trailing \r for a return character. I did this to make everything work
1
2
if (!theAnswerIs.empty() && theAnswerIs[theAnswerIs.size() - 1] == '\r')
				theAnswerIs.erase(theAnswerIs.size() - 1);


So that fixed my original problem. I also have the new project working with the header and .cpp files so I am working on that project now trying to get more familiar with code like that instead of all in one cpp file. It does seem a lot more manageable and easier to read. I tried again to get the regular i/o working instead of rdbuf and I keep getting std throw exception and sometime different errors. I am going to stick with what I have on this project and then my next one when I start from scratch I'll use the regular i/o stream. I think I am just getting lost when trying to change it and missing stuff elsewhere in the program that needs to be changed as well. Not sure. I am going to work on the vector after I get my next class working. I need to create a student class to keep track of who is taking the quiz and what their score is and create a function to display the results of the quiz after it is taken. Thanks for all your help again!
... was I not using delete the right way there?


That is OK, but you could use delete[] to delete the whole array in one go. But I am trying to encourage you not to use new or delete at all. But I gather that may cause other things to go wrong, I understand how it can be hard to get a grip on everything at once.

If you want to use pointers, then you can just use the address of operator & .

myQuestions[count] = &tfQuestion;

If myQuestions was a std::vector<questions*> then this should implicitly store the data on the heap.

Btw, in this case one shouldn't have plurals in the name of a class. The class questions is actually a single question, so it's name should reflect that.

When using std::getline , one can specify a delimiter char, which doesn't appear in the result. So this should help in not having to write code to fix that sort of thing.
http://en.cppreference.com/w/cpp/string/basic_string/getline

Also look at std::stringstream , one can use it to read in several values of different types, rather than go through all this drama:
1
2
3
4
5
6
7
8
int npos = line.size();
		int prev_pos = 0;
		int pos = 0;
		while (line[pos] != ' ')
			pos++;
		questiontype = line.substr(prev_pos, pos - prev_pos);
		prev_pos = ++pos;
		questionvalue = atoi(line.substr(prev_pos, npos - prev_pos).c_str());


http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt

So I did this code:
std::cout << ans[i].size() << "\n" << theAnswerIs.size() << std::endl;


The debugger should tell you that. One of the problems of poor person's debugging is that one has to remember to go back and delete the "debug" code.

When I tried to implement the vector it kept throwing an exception while trying to load into the vector. I know this is probably due to me trying to load it wrong. Soon as I get this student class working I will attempt the vector again and post my code here and maybe you can see what I am doing wrong. I reviewed the link you posted and I looks like it is less code doing it that way I will have to do some more research on that subject so I can better understand it. You are right the debugger is how I found out there was \r at the end on each answer from the file but it wasn't until after I did the cout to see the sizes. I am still learning to use the debugger since I just found out how it could be used from you yesterday. But it has already helped me tremendously especially being able to set the breakpoints and go through the code line by line. Now I am able to figure out exactly where in the code the program fails. I can't thank you enough for that haha. I may need some help with this new student class if you wouldn't mind? I'm not sure yet though I am still trying to figure it out on my own so I can learn but if I get completely stuck and can't figure it out I will post the code. Thanks again.
When I tried to implement the vector it kept throwing an exception while trying to load into the vector. I know this is probably due to me trying to load it wrong.


Start with something minimal, and get that working, then add in more things.

Look at the example here, although that won't be appropriate if you want pointers:
http://en.cppreference.com/w/cpp/container/vector/emplace_back
Ahh I can already see what I did wrong with the vector. I was trying to load the same way as an array. I forgot about emplace_back. I will try that a bit later. I am having some trouble with my student class. I am trying to get the total possible points based on the point value of the question in the file. Which I got to work in my displayQuizQuestions function in the Quiz class. But now I cant get it to work in my student class. I keep trying different stuff but nothing is working. You think you could give me some advice on what I'm doing wrong?

Here is what I have now:
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
class Quiz : public questions
{
private:
	std::string fileName;
	//questions *myQuestions[10];
	//int numberOfQuestions;
	std::string theAnswer;
	std::string qtype;
	std::string theAnswerIs;
	std::string ans[6];
protected:
	int score = 0;
	questions *myQuestions[6];
	int numberOfQuestions;
	int pointValue[6];
	int totalPossiblePoints = 0;

public:
	int loadQuiz()
	{
		std::ifstream infile;
		infile.clear();
		//checking to see if user specified file exist, as well as making sure the user has used the correct format
		while (!infile.is_open())
		{
			std::cout << "Please enter the .txt file name: " << std::endl;
			std::getline(std::cin, fileName);
			//fileName += ".txt";
			infile.open(fileName.c_str(), std::ios::in | std::ios::binary);
			if (infile)
				break;
			std::cout << "Either the file doesn't exist or you forgot the .txt extension. ";
			std::cout << "Please try again. " << std::endl << std::endl;//promt user to try again if the file name is invalid
		}


		//std::ifstream infile("quiz.txt");
		std::streambuf *cinbuf = std::cin.rdbuf();       //save old buf
		std::cin.rdbuf(infile.rdbuf());             //redirect std::cin to infile.txt!

		std::string line;
		std::string theQuestion;
		std::string questiontype;
		//std::string theAnswer;
		int  questionvalue;

		while (true)//error check to ensure the file has an acceptable amount of questions
		{

			//get the number of questions from the first line in the file
			//std::getline(infile, line);
			std::getline(std::cin, line);
			numberOfQuestions = atoi(line.c_str());
			if (numberOfQuestions != 6)
			{
				std::cout << "Error! The quiz has too many or too litte questions. The desired amount is \'6\'" << std::endl;
				std::cout << "Please check the file to ensure there is an acceptable amount of questions." << std::endl;
				throw;
			}
			else
			{
				break;
			}
		}
		for (int count = 0; count < numberOfQuestions; count++)
		{
			//std::getline(infile, line);
			std::getline(std::cin, line);
			//get the next line with the question type and the value of the question
			int npos = line.size();
			int prev_pos = 0;
			int pos = 0;
			while (line[pos] != ' ')
				pos++;
			questiontype = line.substr(prev_pos, pos - prev_pos);
			prev_pos = ++pos;
			questionvalue = atoi(line.substr(prev_pos, npos - prev_pos).c_str()); // Last word

			if (questiontype == "TF")//do this if questiontype = TF
			{
				myQuestions[count] = new tfQuestion;
				//std::getline(infile, line);
				std::getline(std::cin, theQuestion);
				myQuestions[count]->setQuestion(theQuestion, questionvalue);
			}

			if (questiontype == "MC")//do this if questiontype = MC
			{
				myQuestions[count] = new mcQuestion;
				//std::getline(infile, line);
				std::getline(std::cin, theQuestion);
				myQuestions[count]->setQuestion(theQuestion, questionvalue);
			}

		}
		std::cin.rdbuf(cinbuf);//restore cin to standard input
		infile.close();//close the file stream
		return numberOfQuestions;
	}

	void displayQuizQuestions(int numquestions)
	{
		//print out the questions that have been processed
		for (int i = 0; i < numquestions; i++)
		{
			qtype = myQuestions[i]->getQuestionType();
			std::cout << qtype << " " << myQuestions[i]->getValue() << "\n";
			myQuestions[i]->printOptions();
			theAnswerIs = myQuestions[i]->getAnswer();
			std::cout << theAnswerIs << std::endl;
			std::cout << "\n";
			std::cout << "Please enter your answer: ";
			std::cin >> ans[i];
			std::cout << ans[i].size() << "\n" << theAnswerIs.size() << std::endl;

			if (!theAnswerIs.empty() && theAnswerIs[theAnswerIs.size() - 1] == '\r')
				theAnswerIs.erase(theAnswerIs.size() - 1);
			std::cout << ans[i].size() << "\n" << theAnswerIs.size() << std::endl;
			if (theAnswerIs == ans[i])
			{
				std::cout << "You are correct, the answer is: " << theAnswerIs << '\n';
				score++;
				std::cout << "Your current score is " << score << std::endl;
			}
			else
			{
				std::cout << "Incorrect, the correct answer is: " << theAnswerIs << std::endl;
				std::cout << "Your score is " << score << std::endl;
			}
		}
		/*		for (int i = 0; i < numquestions; i++)
				{
					pointValue[i] = myQuestions[i]->getValue();
				}
				for (int i = 0; i < 6; i++)
				{
					std::cout << "the point value of each question is " << pointValue[i] << std::endl;
					totalPossiblePoints += pointValue[i];
					std::cout << "The total possible points are: " << totalPossiblePoints << std::endl;

				}
				std::cout << totalPossiblePoints << std::endl;
				//delete myQuestions[10];
			}
*/
		//Above works if I uncomment it to display the total points possible based on what the question value is in the file 
		//but I cant get it to work when trying to implement in the student class...
/*
			void getPointValue(int numquestions)
			{
				for (int i = 0; i < numquestions; i++)
				{
					pointValue[i] = myQuestions[i]->getValue();
				}
				for (int i = 0; i < 6; i++)
				{
					std::cout << "the point value of each question is " << pointValue[i] << std::endl;
					totalPossiblePoints += pointValue[i];
					std::cout << "The total possible points are: " << totalPossiblePoints << std::endl;

				}
				std::cout << totalPossiblePoints << std::endl;
			}
		*// above I was trying to create a function to call from student class which I couldn't get to work either
	}
};
class Student : Quiz
{
private:
	int pointsPossible = 0;
	int pointsScored = 0;
	std::string name;
	int yourScore = 0;

	//int totalScore;
public:
	void student()
	{
		pointsPossible = getPointValue();
		std::cout << "Please enter your name" << std::endl;
		std::getline(std::cin, name);
		std::cout << "Hello " << name << std::endl
			<< "The total possible points are: " << pointsPossible << std::endl
			<< "You scored " << score << " points out of " << pointsPossible << " possible points." << std::endl;

	}
	int Score()
	{
		yourScore = Quiz::score;
		return score;
	}
	int getPointValue()
	{
		for (int i = 0; i < numberOfQuestions; i++)
		{
			Quiz::pointValue[i] = Quiz::myQuestions[i]->getValue();
		}
		for (int i = 0; i < 6; i++)
		{
			std::cout << "the point value of each question is " << pointValue[i] << std::endl;
			totalPossiblePoints += pointValue[i];
			std::cout << "The total possible points are: " << totalPossiblePoints << std::endl;

		}
		std::cout << totalPossiblePoints << std::endl;
		return totalPossiblePoints;
	}
	
}; 


at the current state my program outputs


Enter selection: 3
You selected - Show quiz results.
the point value of each question is -858993460
The total possible points are: -858993460
the point value of each question is -858993460
The total possible points are: -1717986920
the point value of each question is -858993460
The total possible points are: 1717986916
the point value of each question is -858993460
The total possible points are: 858993456
the point value of each question is -858993460
The total possible points are: -4
the point value of each question is -858993460
The total possible points are: -858993464
-858993464
Please enter your name
Hello
The total possible points are: -858993464
You scored 0 points out of -858993464 possible points.
First of all, Student should not inherit from Quiz. Public inheritance implies an "IS A" relationship. There is also the concept Liscov Substitution Principle and SOLID:
https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)

The other types of relationship are "HAS A" and "USES A". The former means a class has a member of that type; the latter means a function has a an argument of that type, in other words it uses that type to do something.

So Quiz HAS A container (vector or array) of question and question can be several different types, so they are "IS A".

Ask yourself where the results of the Quiz are going to be stored? I would suggest not in the Quiz.

A Student can have lots of things: Name, Address, DOB, Drivers License etc. Ideally these would all be classes of their own (and Student inherits from Person, which is where most of that would reside), but to keep it simple they can just exist as member variables of appropriate type like std::string. What other thing can a student have? It relates to where they learn.

I should have asked this question at the start: Can you please post your assignment text verbatim?
Yeah here is my assignment.

Part 1: Begin with the running program from your Phase 4 Individual Project where the examination question class hierarchy was fully implemented in a menu-driven program. An exam class was developed to load the exam from a file and display each question to the screen.

Part 2: Modify the program from part 1 to change the menu to the following:

Load an exam
Take an exam
Show exam results
Quit
Choice 1: No functionality change. Load the exam based upon the user's prompt for an exam file.

Choice 2: The program should display a single question at a time and prompt the user for an answer. Based upon the answer, it should track the score based upon a successful answer. Once a user answers the question, it should also display the correct answer with an appropriate message (e.g., "Good job" or "Better luck next time") Upon completion of the exam, the program should return the user to the menu.

Choice 3: The program should display the total points available and the total points scored during that exam. A percentage score should also be displayed. (Optional: if you choose to track which problems were missed, you could display that information for the user.)

Choice 4: No change to this functionality from the Phase 4 IP.

You should consider creating an additional class Student that will track student's score through methods such as addPointsPossible, addPointsScored, getPointsPossible, and getPointsScored. You should also enhance your Exam class to include methods getPointValue and getAnswer. You may also want to add a method to only display one question at a time, such as displayQuestion.
Also I had Student inherit Quiz because I thought I would have to do that to retrieve the necessary information from the Quiz class. For example the score and the total points of the questions etc.. But I think I see what your saying since it is already public I can retrieve it with out inheritance? I was thinking that it would go out of scope so I would have to inherit to prevent it, is that wrong?
Pages: 12