Error Trapping and Exception Handling

So my original code is added, now we have to change the code to allow Write a C++ program to allow the user to create a test bank of questions. The program should first ask the user how many questions he or she wishes to create. This quantity will be the first line in the test bank. The user should now be prompted for all information for each question, and then that question is written out to the test bank in the exact format specified in the Phase 2 Individual Project.

For each question, the following information should be gathered:
•Question type: Multiple choice (MC) or True/False (TF)
•Question value
•Actual question
•If the question is TF, then get answer
•If the question is MC, then get the number of options followed by each option and finally the answer
This program should be robust and allow users who make mistakes the ability to correct them (exception handling).

Can someone point me in the direction I need to take to get these results

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
  #include <iostream>
#include <string>

using namespace std;


string & makeLowerCase ( string & String );

int main ( )
{
    string catchGarbage; // This is used to exit.

    string userInput = ""; 

    const int NQS = 4;

    string userAnswers[NQS];
    string quizQuestions[NQS];
    string quizAnswers[NQS];

    // Question 1
    quizQuestions[0] =
            "There is 4 questions total\n"
			"Question 1:\n"
            "TF 25\n\n"
            "There exist birds that cannot fly?\n\n"
            "T\n"
            "F\n";

    quizAnswers[0] = "T";

    // Question 2
    quizQuestions[1] =
            "Question 2:\n"
            "MC 25\n\n"
            "Who was the President of the USA in 1991?\n\n"
            "6\n\n"
            "A Richard Nixon\n"
            "B Gerald Ford\n"
            "C Jimmy Carter\n"
            "D Ronald Reagon\n"
            "E George Bush Sr.\n"
            "F Bill Clinton\n";

    quizAnswers[1] = "E";

    // Question 3
    quizQuestions[2] =
            "Question 3:\n"
            "TF 25\n\n"
            "The city of Boston hosted the 2004 Summer Olympics?\n\n"
            "T\n"
            "F\n";

    quizAnswers[2] = "F";
    
    //just to even out the scoring system i put four
      // Question 4
    quizQuestions[3] =
            "Question 1:\n"
            "TF 25\n\n"
            "There is nine planets?\n\n"
            "T\n"
            "F\n";

    quizAnswers[3] = "T";



    // Other variables
    int a = 0; // set these right away to 0.
    int s = 0;

    cout << "Type 'Start' to begin the quiz, 'Help' for instructions, or 'Exit' to"
            "quit\n" << endl;
    cin >> userInput;
    cout << "\n" << endl;

    if ( makeLowerCase ( userInput ) == "Exit" )
    {
       

        cout << "Thanks for taking the quiz! Press Enter to exit\n";
        //        cin.ignore ( );
        //        cin.get ( );
        getline ( cin, catchGarbage );
       
        //return 0;
       
        // evaluate to true after this.
    }

    if ( makeLowerCase ( userInput ) == "Help" )
    {
        cout << "All questions require a letter as an answer."
                << "For Multiple choice: What color is the sky. A Blue B Green C Red In this"
                " case, you would type 'A' for your chose"
                << "For True or False"
                " You will use a T or F"
                << "Good luck on your quiz\n" << endl;


        cout << "Type 'Start' to begin the quiz or 'Exit' to quit\n" << endl;
        cin >> userInput;
        cout << "\n" << endl;
    }

    if ( makeLowerCase ( userInput ) == "Start" )
    {
        int currentQuestion;

        for ( currentQuestion = 0; currentQuestion < NQS; currentQuestion++ )
        {
            cout << quizQuestions[currentQuestion] << endl;
            cin >> userInput;
            cout << endl << endl;

            // check for "exit" right away.
            if ( makeLowerCase ( userInput ) == "Exit" )
            {
                // now we can check if userInput is == "exit".
                cout << "Thanks for taking the quiz! Press Enter to exit\n";
                getline ( cin, catchGarbage );
                break;
            }

            userAnswers[currentQuestion] = userInput;
            if ( userAnswers[currentQuestion] == quizAnswers[currentQuestion] )
            {
                a++; // you can increment the score like this.
            }
        }

        // If all the questions were successfully asked and answered, display
        // the score. Otherwise, the program exits.
        if ( currentQuestion == NQS )
        {
            s = a * 25;

            if ( s == 100 )
                cout << "Congratulations! You got 100% of the answers right!"
                    "\n" << endl;

            else
                cout << "You answered " << s << "% of questions correct!\n"
                    << endl;
        }
    }

    return 0;
   
}

string & makeLowerCase ( string & String )
{
   

    for ( int index = 0; index < String.length ( ); index++ )
    {
        tolower ( String[index] );
    }

    // After all the letters are converted, return the modified version.
    return String;
}
Last edited on
Topic archived. No new replies allowed.