Question: Need Help with C++ code. Listed below is my work. I did a program main that reads test questions from a text file and user

Need Help with C++ code. Listed below is my work. I did a program "main" that reads test questions from a text file and user responds to 6 questions with assigned points (3 true or false and 3 multiple choice). After the questions answered, display total points. After creating the code, when I run it, the program goes through all 6 questions properly. However, the IF statement does not break after the 6th question and does not print the total. Instead, it keeps asking the last question (#6) and labels it Question: 1.

Tried to troubleshoot and cannot figure out what I am doing wrong. Please help

Here is the C++ code (in .cpp):

main.cpp

#include

#include // For file input and file output (allows the .txt file) //

#include

#include

using namespace std;

int main()

{

int totalQuestion;

int totalPoints = 0;

string input, type, question, answer;

int totalOptions;

string c;

int points;

// ifstream to read questions from file input and file output (allows the .txt file) //

ifstream inFile ("inputQA.txt");

if (inFile.is_open())

{

inFile >> totalQuestion;

while(true)

{

for (int i = 0; i < totalQuestion; ++i)

{

cout << "Question: "<< (i+1) << endl;

inFile >> type >> points;

// This is the TF Question Class. Read type of question from .txt file

if(type == "QuestionTF")

{

// Get the question from the .txt file

inFile.ignore();

getline(inFile, question);

// Get the answer from the .txt file

inFile >> answer;

// Print the question and ask for input True or False

cout << "Question: " << question << endl;

cout << "Input answer (True or False): ";

cin >> input;

if(input == answer)

{

cout << "Correct Answer ";

totalPoints = totalPoints + points;

}

else

cout << "Incorrect Answer ";

}

// This is the MC Question Class. Not a TF question, ELSE IF a MC Question. Read type of question from .txt file

else if(type == "QuestionMC")

{

string options;

inFile.ignore();

getline(inFile,question);

// Get the answer from the .txt file

inFile >> totalOptions;

// Print the question and MC options

cout << "Question: " << question << endl;

for (int j = 0; j < totalOptions; ++j)

{

getline(inFile , options);

cout << options << endl;

}

// Ask for input MC option choice

inFile >> answer;

cout << "Enter your choice(A-F): ";

cin >> c;

inFile.ignore();

if(c == answer)

{

cout << "Correct Answer ";

totalPoints = totalPoints + points;

}

else

cout << "Incorrect Answer ";

}

if(inFile.eof())

break;

}

}

inFile.close();

}

else cout << "Error - Unable to open file";

// Print Total Points after inFile closes due to EOF or if Error

cout << "Total Points for this Quiz: " << totalPoints << endl;

return 0;

}

-------------------------------------------------------------------------------------------------------------

Here is the text file this pulls the 6 questions from named "inputQA.txt":

6

QuestionTF 5

There are over 100 billion planets in the Milky Way galaxy?

True

QuestionMC 10

What is the largest non-gas planet in our solar system?

7

A. Pluto

B. Neptune

C. Saturn

D. Mars

E. Mercury

F. Earth

F

QuestionTF 5

Earth is the brightest planet in our solar system (True or False)?

False

QuestionMC 10

Which planet is closest to Earth?

7

A. Pluto

B. Neptune

C. Saturn

D. Venus

E. Mercury

F. Mars

D

QuestionTF 5

Venus is known as Earths "Sister Planet"?

True

QuestionMC 10

Between which two planets are asteroids mainly found?

7

A. Saturn and Uranus

B. Earth and Mars

C. Mars and Jupiter

D. Jupiter and Saturn

E. Uranus and Pluto

F. Venus and Saturn

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!