C++ Programming Help - Have gone through all the learning materials for Exceptio
ID: 3859823 • Letter: C
Question
C++ Programming Help - Have gone through all the learning materials for Exception Handling in my course and I understand how "Try, Throw, Catch" works, but only on a very basic level and I am unable to apply it to my existing program the way I want it to work. My existing program pulls from a .txt file and has both T and F, plus multiple choice questions. As writen, it basically must have an absolutely correct answer in order for it to be correct "ELSE" it results in an incorrect answer response. I figured for the TRY "IF" would just need to indicate if the CIN (user input) is not equal (!=) to T or F (for the questionTF) or A, B, C, D, E, F (for the questionMC) it should throw and exception.
Here is a more specific description:
For questionTF:.... If the user enters anything other than "T" or "F", it should catch this and present an exception that states "Invalid Answer. Try Again".
For questionMC:... If the user enters anything other thant "A", "B", "C", "D", "E", or "F"; it should catch this and presens an exception that states "Invalid Answer, Try Again".
I tried to add the exception prototyping in the "questionTF" section, but cannot get the exception to work. There are issues with in IF within an IF when it involves using a TRY and my thoughts are that is an advance approach. In the TRY I used if(input !=T || !=F). Again, I could not make it work so did not bother to even attempt the same with the questionMC section, but figured it would have to be if(input !=A || !=B !=C || !=D !=E || !=F) for that section.
Not sure what else to try and I believe because I am inputting from a text file, it complicates the flow for the Try, Throw, Catch with the exception.
Can you provide example exception coding for me to study that can be applied to this code? Any help would be appreciated...
Below is my main.cpp code:
#include
#include
#include
#include // Added header for exception handling functions
using namespace std;
int main()
{
int totalQuestion;
int totalPoints = 0, i=0;
string input, type, question, answer;
int totalOptions;
string c;
int points;
std::cout <<" Jeff's Solar System Quiz " << std::endl;
// 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(i
{
std::cout << "Question: "<< (i+1)<< std::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
std::cout << "Question: " << question << std::endl;
std::cout << "Input answer (True or False): ";
std::cin >> input;
if(input == answer)
{
std::cout << " Great Job, Correct Answer ";
totalPoints = totalPoints + points;
}
else
std::cout << " Sorry, Incorrect Answer ";
}
// If not a TF question "else if" a MC question
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
std::cout << "Question: " << question << std::endl;
for (int j = 0; j < totalOptions; ++j)
{
getline(inFile , options);
std::cout << options << std::endl;
}
// Ask for input MC option choice
inFile >> answer;
std::cout << "Enter your choice(A-F): ";
std::cin >> c;
inFile.ignore();
if(c == answer)
{
std::cout << " Great Job, Correct Answer ";
totalPoints = totalPoints + points;
}
else
std::cout << " Sorry, Incorrect Answer ";
}
if(inFile.eof())
break;
i++;
}
inFile.close();
}
else std::cout << "Error - Unable to open file";
// Print Total Points after inFile closes due to EOF or if Error
std::cout << "Total Points for this Quiz: " << totalPoints << std::endl;
return 0;
}
Here is my inputQA text file:
7
QuestionTF 10
There are over 100 billion planets in the Milky Way galaxy?
T
QuestionMC 20
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 10
Earth is the brightest planet in our solar system (T or F)?
F
QuestionMC 20
Which planet is closest to Earth?
7
A. Pluto
B. Neptune
C. Saturn
D. Venus
E. Mercury
F. Mars
D
QuestionTF 10
Venus is known as Earths "Sister Planet"?
T
QuestionMC 20
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
C
QuestionTF 10
The asteroid belt is known as the "Kuiper Belt (T or F)?
F
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string.h>
#include <exception> // Added header for exception handling functions
using namespace std;
int main()
{
int totalQuestion;
int totalPoints = 0, i=0;
string input, type, question, answer;
int totalOptions;
string c;
int points;
std::cout <<" Jeff's Solar System Quiz " << std::endl;
// 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(i<totalQuestion)
{
std::cout << "Question: "<< (i+1)<< std::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
std::cout << "Question: " << question << std::endl;
std::cout << "Input answer (True or False): ";
std::cin >> input;
if(input == answer)
{
std::cout << " Great Job, Correct Answer ";
totalPoints = totalPoints + points;
}
else
std::cout << " Sorry, Incorrect Answer ";
}
// If not a TF question "else if" a MC question
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
std::cout << "Question: " << question << std::endl;
for (int j = 0; j < totalOptions; ++j)
{
getline(inFile , options);
std::cout << options << std::endl;
}
// Ask for input MC option choice
inFile >> answer;
std::cout << "Enter your choice(A-F): ";
std::cin >> c;
inFile.ignore();
if(c == answer)
{
std::cout << " Great Job, Correct Answer ";
totalPoints = totalPoints + points;
}
else
std::cout << " Sorry, Incorrect Answer ";
}
if(inFile.eof())
break;
i++;
}
inFile.close();
}
else std::cout << "Error - Unable to open file";
// Print Total Points after inFile closes due to EOF or if Error
std::cout << "Total Points for this Quiz: " << totalPoints << std::endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.