Displaying correct answer from a text file

Hello, I just need help with a quiz grading program I am writing. I only need to display the correct answer from a text file upon an incorrect user input here:
if (ans == questions[i].getCorrectNumber())
{
cout << "Correct!" << endl;
nRight++;
}
else
{
cout << "Sorry that's Incorrect. The correct answer is: " << endl;
//????
//Need help displaying correct answer
nWrong++;
}



here is the rest of my code:


.cpp:
#include <iostream>
#include <string>
#include <fstream>
#include "Question.h"
using namespace std;

void initQuestions(Question* q, int num_q)
{
ifstream f;
f.open("Questions.txt");
if (!f.is_open()) {


cout << "File did not open!" << endl;
exit(1);
}
int i = 0;
while (i < num_q) {
q[i] = Question();

string text;
getline(f, text);
q[i].setQuestionText(text);
getline(f, text);
q[i].setPossAnswer1(text);
getline(f, text);
q[i].setPossAnswer2(text);
getline(f, text);
q[i].setPossAnswer3(text);
getline(f, text);
q[i].setPossAnswer4(text);
getline(f, text);
q[i].setPossAnswer5(text);
getline(f, text);
int correct = stoi(text);
q[i].setCorrect(correct);


i++;
}

}

void displayQuestion(Question q)
{
cout << q.getQuestion() << endl;
cout << "ANSWERS:" << endl;
cout << "1." << q.getPossAnswer1() << endl;
cout << "2." << q.getPossAnswer2() << endl;
cout << "3." << q.getPossAnswer3() << endl;
cout << "4." << q.getPossAnswer4() << endl;
cout << "5." << q.getPossAnswer5() << endl;
cout << "Enter the Correct Answer: \n";
}


int main()
{

cout << "Welcome to My Awesome Quiz-o-Matic!" << endl;

int ans = 0;
int nWrong = 0;
int nRight = 0;
const int num_of_questions = 5;
Question questions[num_of_questions];
cout << "Question for Student\n";
cout << "____________________\n";

//Call initQuestions() function to load array with questions.
initQuestions(questions, num_of_questions);
//Use a loop to prompt student with question and read answer.
for (int i = 0; i < num_of_questions; i++)
{
//Call displayQuestion to display question.
displayQuestion(questions[i]);

//use cin >> ans to read student answer.
cin >> ans;

//If the answer is correct, display correct
// refer to specific question object
if (ans == questions[i].getCorrectNumber())
{
cout << "Correct!" << endl;
nRight++;

}
else
{
cout << "Sorry that's Incorrect. The correct answer is: " << endl;
//????
//Need help displaying correct answer
nWrong++;
}

}

cout << "Drill over. Here are the points: " << endl;
cout << "Student Score in points: " << nRight << endl;
cout << "Student correct score percentage: " << int((nRight*1.0 / num_of_questions) * 100) << endl;
system("Pause");
return 0;
}

Question.h:
#pragma once
#ifndef QUESTION_H
#define QUESTION_H

#include<string>


using namespace std;
class Question
{
private:
int correctAnswer;
string questionText;
string possibleAnswers[5];

public:

string getCorrectAnswer();
int getCorrectNumber();
string getPossAnswer1();
string getPossAnswer2();
string getPossAnswer3();
string getPossAnswer4();
string getPossAnswer5();
string getQuestion();

Question();
void setCorrect(int);
void setPossAnswer1(string);
void setPossAnswer2(string);
void setPossAnswer3(string);
void setPossAnswer4(string);
void setPossAnswer5(string);
void setQuestionText(string);
};

Question::Question() // default constructor
{
questionText = "";
correctAnswer = 0;
}

void Question::setQuestionText(string question)
{
questionText = question;
}

void Question::setPossAnswer1(string ans)
{
possibleAnswers[0] = ans;
}

void Question::setPossAnswer2(string ans)
{
possibleAnswers[1] = ans;
}

void Question::setPossAnswer3(string ans)
{
possibleAnswers[2] = ans;
}

void Question::setPossAnswer4(string ans)
{
possibleAnswers[3] = ans;
}

void Question::setPossAnswer5(string ans)
{
possibleAnswers[4] = ans;
}

void Question::setCorrect(int n)
{
correctAnswer = n;
}

string Question::getCorrectAnswer()
{
int rightIndex = correctAnswer - 1;
return possibleAnswers[rightIndex];
}

int Question::getCorrectNumber()
{
return correctAnswer;
}

string Question::getPossAnswer1()
{
return possibleAnswers[0];
}

string Question::getPossAnswer2()
{
return possibleAnswers[1];
}

string Question::getPossAnswer3()
{
return possibleAnswers[2];
}

string Question::getPossAnswer4()
{
return possibleAnswers[3];
}

string Question::getPossAnswer5()
{
return possibleAnswers[4];
}

string Question::getQuestion()
{
return questionText;
}
#endif
1
2
3
4
5
6
7
8
if (ans == questions[i].getCorrectNumber())
{
   cout << "Correct!" << endl;
   nRight++;
}
else
{
   cout << "Sorry that's Incorrect. The correct answer is: " << endl;
let me see if I understand.
so if the user doesn't input `questions[i].getCorrectNumber()' you tell him that his answer is incorrect, because he should have put `questions[i].getCorrectNumber()' instead
so you want to tell him that the correct is `questions[i].getCorrectNumber()' but you don't know where the value of `questions[i].getCorrectNumber()' is stored
¿is that your problem?
Last edited on
Topic archived. No new replies allowed.