Help: C++ Multiple Choice Questions and answers

Here is the file: http://www.adrive.com/public/H5fXqh/QuestionsAndAnswers2.txt
Here is the file with answer marked: http://www.adrive.com/public/22U4rh/QNA.txt

I just wonder is there any other way to make it neat and readable?
I am new to c++ in this semester. Any suggest will be great :)


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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

const int SIZE_OF_QUESTION = 125;
string questions[SIZE_OF_QUESTION];
//Global Variables
string question, answer;
int i = 0;
double yourScore = 0, totalScore = 25, percentage;
const int ONEQUESTION = 5, x = 1;
char userAnswer[25] = {};
char answerKey[SIZE_OF_QUESTION] =
                        //              1,                  2,                  3
                      {'x','x','x','x','C','x','x','x','x','C','x','x','x','x','A',
                        //              4,                  5,                  6
                       'x','x','x','x','A','x','x','x','x','C','x','x','x','x','A',
                        //              7,                  8,                  9
                       'x','x','x','x','B','x','x','x','x','A','x','x','x','x','B',
                        //              10,                 11,                 12
                       'x','x','x','x','B','x','x','x','x','B','x','x','x','x','B',
                        //              13,                 14,                 15
                       'x','x','x','x','D','x','x','x','x','C','x','x','x','x','C',
                       //               16,                 17,                 18
                       'x','x','x','x','D','x','x','x','x','B','x','x','x','x','A',
                       //               19,                 20,                 21
                       'x','x','x','x','C','x','x','x','x','A','x','x','x','x','A',
                       //               22,                 23,                 24
                       'x','x','x','x','C','x','x','x','x','B','x','x','x','x','C',
                       //               25
                       'x','x','x','x','B'};

//Functions Header
string getQuestion();
string checkAnswer();
void result();

int main()
{
    cout << "========================================================="<<endl;
    cout << "NOTE: Please enter all your answer an UPPERCASE LETTER!!!"<<endl;
    cout << "\nThis is your exam with 25 multiple choice questions."<<endl;
    cout << "=========================================================\n"<<endl;
    //cout << "---------------------------------------";
    //cout << "-----------------------------------------";
    getQuestion();
    //checkAnswer();
    result();

}

string getQuestion()
{
    int i = 0, line = 0, questionNumber = 0;
    ifstream inFile;


    inFile.open("QuestionsAndAnswers2.txt");
    //Check if it opened, if not end the program
    if(inFile.fail())
    {
       cout << "Unable to open the file.\n";
       exit(0);
    }
    if(inFile.good())
    {
        cout << "---------------------------------------";
        cout << "-----------------------------------------";
        questionNumber++;
        if(questionNumber % x == 0)
        {
            cout << "Question "<<questionNumber<<":"<<endl;
        }
    //Step 1: Read in the list of questions and answers into an array of strings
        for(int i = 0; i < 125; i++)
        {
            getline(inFile, questions[i]);
            question = questions[i];
            cout << question<<endl;

    //Step 2: Read the first five lines which equal one question
    //      line 0 --> onequestion = 5, which mean read line 1-5
            line++;
            if(line % ONEQUESTION == 0)
            {
    //Step 3: Get the user input and stored into array userAnswer[i]
                cout << "\nPlease enter your answer: " ;
                cin >> userAnswer[i];
                //Check for answer entered properly
                if(userAnswer[i] == 'A' || userAnswer[i] == 'B' ||
                   userAnswer[i] == 'C' || userAnswer[i] == 'D')
                {
                    //leave it blank because user input UPPERCASE LETTER
                }
                else
                {
                    while(!(userAnswer[i] == 'A' || userAnswer[i] == 'B' ||
                    userAnswer[i] == 'C' || userAnswer[i] == 'D'))
                    {
                    cout << "\nINVALID INPUTE: Please enter (A-D) an UPPERCASE LETTER!!!.\a" <<endl;
                    cout << "Please try again: ";
                    cin >> userAnswer[i];
                    }
                }

                if(userAnswer[i] == answerKey[i])
                {
                    cout << "CORRECTED"<<endl;
                    yourScore++;
                }
                else
                {
                    cout << "INCORRECTED \nThe CORRECT answer is "<<answerKey[i]<<endl;
                }
                cout << "---------------------------------------";
                cout << "-----------------------------------------";
                //count question number increment each question
                questionNumber++;
                if(questionNumber % x == 0)
                {
                    //avoid last input
                    if(questionNumber == 26)
                    {
                        break;
                    }
                    cout << "Question "<<questionNumber<<":"<<endl;
                }
            }
        }
    }
    inFile.close();

    return question;
}//end getQuestion function

string checkAnswer()
{

}

void result()
{
    cout << "You answered "<<yourScore<<"/"
    <<totalScore<< " questions correctly on this exam."<<endl;
    percentage = (yourScore/totalScore)*100;
    cout << "\nThat is a score of "<<percentage<<"%,";

    if(percentage >= 90)
        cout << " which is a A."<<endl;

    else if(percentage >=80)
        cout << " which is a B."<<endl;

    else if(percentage >=70)
        cout << " which is a C."<<endl;

    else if(percentage >=60)
        cout << " which is a D."<<endl;

    else
        cout << " which is a F."<<endl;
}
Topic archived. No new replies allowed.