I need help with my code PLEASE

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 need help write this code to text file. Output the their (user) name and score and letter grade.


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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

//Global Variables
string question, userAnswer, answer;
double yourScore = 0, totalScore = 25, percentage;
const int ONEQUESTION = 5, x = 1;
int i = 0;
string answerKey[125] =
    //              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"};

string correctAnswers[125] =
    //               1,                         2,                              3
  {"x","x","x","x","(C. *20)","x","x","x","x","(C. *Fortran)","x","x","x","x","(A. *RAM)",
    //              4,                                  5,                          6
   "x","x","x","x","(A. *Encapsulation)","x","x","x","x","(C. *third)","x","x","x","x","(A. *verbose error messages)",
    //               7,                                        8,                          9
   "x","x","x","x","(B. *particle renderer)","x","x","x","x","(A. *SDK)","x","x","x","x","(B. *second)",
    //              10,                                    11,                                  12
   "x","x","x","x","(B. *program editor)","x","x","x","x","(B. *Authenticate)","x","x","x","x","(B. *Tim Berners-Lee)",
    //              13,                              14,                                    15
   "x","x","x","x","(D. *compiler)","x","x","x","x","(C. *Formal Methods)","x","x","x","x","(C. *Morris worm)",
   //               16,                            17,                                     18
   "x","x","x","x","(D. *Pascal)","x","x","x","x","(B. *object-oriented)","x","x","x","x","(A. *C++)",
   //               19,                              20,                                21
   "x","x","x","x","(C. *circuits)","x","x","x","x","(A. *Psuedocode)","x","x","x","x","(A. *procedural)",
   //               22,                                                 23,                            24
   "x","x","x","x","(C. *keep your firewall deployed)","x","x","x","x","(B. *object)","x","x","x","x","(C. *call)",
   //                25
    "x","x","x","x","(B. *sequential)"};

//Functions Header
string generateQuestions(string questions[]);
void getQuestionsAndInputs(string questions[]);
void result();

int main()
{
    cout << "========================================================="<<endl;
    cout << "NOTE: Please enter all your answer an UPPERCASE LETTER!!!\n"<<endl;
    cout << "This is your exam with 25 multiple choice questions.\n"<<endl;
    cout << "Good Luck on the exam!"<<endl;
    cout << "---------------------------------------------------------"<<endl;
    cout << "No Sound = Correct Answer\nOne Sound = Incorrect Answer"<<endl;
    cout << "Two Sounds = Invilid Input\nThree Sounds = Need an UPPERCASE LETTER"<<endl;
    cout << "========================================================="<<endl;
    string questions[125] = {};

    generateQuestions(questions);
    getQuestionsAndInputs(questions);
    result();

    //system("pause");
    return 0;
}

string generateQuestions(string questions[])
{
    int i = 0, line = 0, questionNumber = 0;
    ifstream inFile, inFile2;

    inFile.open("QuestionsandAnswers.txt");
    //Check if it opened, if not end the program
    if(inFile.fail())
    {
       cout << "Unable to open the file.\n";
       exit(0);
    }

   while(!inFile.eof())
    {
        //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]);
        }
    }
    inFile.close();

    return questions[i];
}

void getQuestionsAndInputs(string questions[])
{
    int i = 0, line = 0, questionNumber = 0;

    cout << "---------------------------------------";
    cout << "-----------------------------------------";
    questionNumber++;
    if(questionNumber % x == 0)
    {
        cout << "Question "<<questionNumber<<":"<<endl;
    }
    string letter;
    //Step 2: Read the first five lines which equal one question
    for(int i = 0; i < 125; i++)
    {
        question = questions[i];
        //Assign case 1-4 with letter indicated for ABCD
		if(i % 5 == 0)
        	cout << question <<endl;
        else
        {
			switch(i % 5)
			{
				case 1: letter = "A."; break;
				case 2: letter = "B."; break;
				case 3: letter = "C."; break;
				case 4: letter = "D."; break;
			}
			cout << letter<< " "<<question <<endl;
		}
    //Set i = 0 (i is increment), ONEQUESTION = 5, which mean read line 1-5
    //Read 0%5=0 1st time false because not == 4,
    //     1%5=1 2nd time false false because not == 4...
    //     4%5=4->true(but the line already read line 0-4 = 1 question
        if(i % ONEQUESTION == 4)
        {
    //Step 3: Get the user input and set to userAnswer
            cout << "\nPlease enter your answer: ";
            cin >> userAnswer;

            //Check if user entered an UPPERCASE LETTER
            //if yes, do nothing, else give user re-enter a proper letter
            while((userAnswer == "a" || userAnswer == "b"||
                   userAnswer == "c" || userAnswer == "d"))
            {
                cout <<"\nPlease enter (A-D) an UPPERCASE LETTER.\a\a\a"<<endl;
                cout << "Please try again: ";
                cin >> userAnswer;
            }
            //Check for entered beside below letters then give the user a new try
            while(!((userAnswer == "A" || userAnswer == "B" ||
                     userAnswer == "C" || userAnswer == "D")))
            {
                cout << endl;
                cout <<userAnswer<<" is not fall in the range (A-D).\a\a"<<endl;
                cout << "Please try again: ";
                cin >> userAnswer;
            }

            //After done checking above then now output is the userAnswer correct or incorrect
            if(userAnswer == answerKey[i])
            {
                cout << "Corrected!"<<endl;
                yourScore++;
            }
            else
            {
                cout << "Incorrected!\a"<<endl;

                cout << "The Correct Answer is "<< correctAnswers[i]<<endl;
            }
            cout <<"------------------------------------------------";
            cout <<"--------------------------------";
            //Count the question after each question and increment the questionNum
            questionNumber++;
            if(questionNumber % x == 0)
            {
                //avoid last input
                if(questionNumber == 26)
                {
                    break;
                }
                cout << "Question "<<questionNumber<<":"<<endl;
            }
        }
    }
}

void result()
{
    //Step 4: output the user score that answer correctly and his/her letter grade
    cout << "===================================================="<<endl;
    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;
    cout << "===================================================="<<endl;
}
Topic archived. No new replies allowed.