-
March 22nd, 2017, 08:52 PM
#1
[RESOLVED] Need help figuring out why I long random negative number in output
Hi I am new here I am have some trouble with my program. I can seem to figure out why I am getting this long random negative number when 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. Could anyone take a look at my code and give me some advice on what I'm doing wrong? Here is what I have right now, sorry for the mess I am changing stuff over and over and trying different things to get it to work.
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <limits>
int score;
class questions//base class
{
protected://using protected rather than private for inheritance to subclasses
std::string question;
std::string answer;
int value;
std::string questionType;
public:
questions::questions()
{
}
virtual questions::~questions()
{
}
std::string getQuestion()//gets the question
{
return question;
}
virtual int getValue() //gets the point value of the question
{
return value;
}
virtual std::string getQuestionType()// gets the type of question
{
return questionType;
}
virtual void setQuestion(std::string answer, int value)
{
}
virtual void setNewQuestion(std::string answer, int value)
{
}
virtual void printOptions()
{
}
virtual std::string getAnswer()
{
return answer;
}
};
class tfQuestion : public questions// subclass of questions class for true and false questions
{
private:
std::string options;
std::string a1;
public:
tfQuestion::tfQuestion()
{
}
tfQuestion::~tfQuestion()
{
}
void setQuestion(std::string theQuestion, int pointValue)
{
std::string theAnswer;
questionType = "TF";
question = theQuestion;
value = pointValue;
options = "true/false";
//get the answer from the file
std::getline(std::cin, theAnswer);
answer = theAnswer;
}
/*void setNewQuestion(std::string theQuestion, int pointValue)
{
std::string theAnswer;
questionType = "TF";
question = theQuestion;
value = pointValue;
options = "true/false";
//get the answer from user
std::cout << "Enter answer true/false\n";
std::getline(std::cin, theAnswer);
answer = theAnswer;
}*/
int getValue() //gets the point value of the question
{
return value;
}
std::string getQuestionType()// gets the type of question
{
return questionType;
}
std::string getQuestion()
{
return question;
}
void printOptions()//prints the options for that question
{
std::cout << question << std::endl;
//std::cout << answer << std::endl;//this stops output of answer for TF
}
std::string getAnswer()//outputs the answer for that question
{
return answer;
}
};
class mcQuestion : public questions//subclass of questions class for multiple choice questions
{
private:
int numberOfOptions;
std::string mcAnswers[6];
public:
mcQuestion::mcQuestion()
{
}
mcQuestion::~mcQuestion()
{
}
void setQuestion(std::string theQuestion, int pointValue)
{
std::string line;
questionType = "MC";
std::getline(std::cin, line);
numberOfOptions = atoi(line.c_str());
question = theQuestion;
value = pointValue;
//get the individual choice lines and load to options array
for (int count = 0; count < numberOfOptions; count++)
{
std::getline(std::cin, line);
mcAnswers[count] = line;
}
//get the answer from the file and load into answer
std::getline(std::cin, line);
answer = line;
}
/* void setNewQuestion(std::string theQuestion, int pointValue)
{
std::string line;
questionType = "MC";
//get the number of choices from the user
/*std::cout << "Enter the number of choices: ";
std::getline(std::cin, line);
numberOfOptions = atoi(line.c_str());
question = theQuestion;
value = pointValue;
//get the individual choice lines and load to options array
for (int count = 0; count < numberOfOptions; count++)
{
std::cout << "\nEnter next option: ";
std::getline(std::cin, line);
mcAnswers[count] = line;
}
//get the answer from the user and load into answer
std::cout << "\nEnter Answer: ";
std::getline(std::cin, line);
answer = line;
} */
void printOptions()// prints the questions, options, and answer
{
char first = 'A';
std::cout << question << std::endl;
for (int count = 0; count < numberOfOptions; count++)
{
std::cout << "(" << first++ << "): " << mcAnswers[count] << "\n";
}
//std::cout << answer << "\n";//This stops output of answer on MC
}
int getValue() //gets the point value of the question
{
return value;
}
std::string getQuestionType()// gets the type of question
{
return questionType;
}
std::string getAnswer()// prints the answer
{
return answer;
}
};
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:
questions *myQuestions[6];
int numberOfQuestions;
int pointValue[6];
int totalPossiblePoints = 0;
public:
Quiz::Quiz()
{
}
Quiz::~Quiz()
{
}
void setloadQuiz(int num)
{
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;
}
int getloadQuiz()
{
return numberOfQuestions;
}
void setdisplayQuizQuestions(int num)
{
score = 0;
//print out the questions that have been processed
for (int i = 0; i < num; 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;
}
}
}
int getdisplayQuizQuestions()
{
return 0;
}
/* 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 uncommented 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...
int getPointValue()
{
for (int i = 0; i < numberOfQuestions; 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;
return totalPossiblePoints;
}
void setPointValue(int num)
{
for (int i = 0; i < numberOfQuestions; 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;
}
};
class Student
{
private:
int pointsPossible;
int pointsScored = 0;
std::string name;
int yourScore = 0;
//int totalScore;
public:
Student::Student()
{
}
Student::~Student()
{
}
/* void student()
{
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 getPointsPossible()
{
return pointsPossible;
}
void setPointsPossible(int num)
{
Quiz obj;
num = 0;
obj.setPointValue(num);
num = obj.getPointValue();
//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 = score;
return yourScore;
}
int getPointValue()
{
for (int i = 0; i < n; 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;
*/
int menu()
{
int selection = 0;
std::cout << "Please select an option from the list below." << std::endl;
//display the menu
std::cout << "Menu";
std::cout << "======" << std::endl;
std::cout << "1 - Load quiz." << std::endl;
std::cout << "2 - Take quiz." << std::endl;
std::cout << "3 - Show quiz results." << std::endl;
std::cout << "4 - Press 4 to exit." << std::endl << std::endl;
std::cout << "Enter selection: ";
// read user selection
while (true)//while loop to ensure user selects an option on the menu
{
std::cin >> selection;
if (std::cin.fail())//if() to make sure an endless loop doesn't happen if a char is entered.
{
std::cerr << "Please enter an integer number." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
if (selection == 1 || selection == 2 || selection == 3 || selection == 4)//if() to make sure only the options on the menu are avaible to select
{
break;
}
std::cout << "The options are: \'1\'\'2\'\'3\' or \'4\': ";
}
std::cin.ignore();
return selection;
}
int main()
{
Student Sobj;
Quiz obj;
int num = 0;
int selection = 0;
//std::string quizName = "quiz.txt";
while ((selection = menu()) != 4)
switch (selection)
{
case 1:
std::cout << "You selected - Load quiz." << std::endl;
obj.setloadQuiz(num);
num = obj.getloadQuiz();
break;
case 2:
std::cout << "You selected - Take quiz." << std::endl << std::endl;
std::cout << "========== Quiz: ==========" << std::endl;
obj.setdisplayQuizQuestions(num);
num = obj.getdisplayQuizQuestions();
break;
case 3:
std::cout << "You selected - Show quiz results." << std::endl;
//need to call a function that displays the results after taking the quiz
Sobj.setPointsPossible(num);
num = Sobj.getPointsPossible();
break;
case 4:
break;
//if 1, 2, or 3 is not selected then go to the default case
default: std::cout << "Invalid selection. Please try again.\n";
// no break in the default case
}
std::cout << std::endl << std::endl;
std::cout << "Thank you for using this program. Goodbye.";
std::cin.get();
return 0;
}
On this line pointValue[i] = myQuestions[i]->getValue(); the myQuestions[i] array says myQuestions is undefined on the watch list in the debugger. Also sometimes after I try something different it says:
Code:
myQuestions[i]->getValue a pointer to a bound function may only be used to call the function
the output on the console for both errors are the same the long random negative numbers. Which I am guessing is why I am getting these random negative number instead of whats in the file. I keep trying to figure out how to fix it but nothing seems to work. The only way I can get it to work is if I take the code in the new int setPointValue() function, eliminate the two new functions I have at the end of the quiz class and put the code inside of the void displayQuizQuestions(int numquestions). I think I understand why it works there and not in the new function. Because myQuestions[i] is no longer in scope since it left the function and moved to a different one. I can't figure out how to retrieve the data that I need from outside the scope of that function.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|