// Saliha Babar CS1A Chapter 7, Page 446, #9
//
/***************************************************************************
* GRADE MULTIPLE CHOICE QUESTIONS
* _________________________________________________________________________
* This program accepts answers for 20 multiple choice questions, in form of
* A, B, C, or D. Then it calculates total correct answers, total incorrect
* answers as well as incorrect questions list.
*
* There is no specific formula for this program
* ___________________________________________________________________________
* INPUT
* TOTAL_QUESTIONS : total question in an exam
* userAns : user answer for questions
* schemeAns : correct answer for questions (marking scheme)
* OUTPUT
* totalCorrect : total correct answer entered by user
* *************************************************************************/
#include <iostream>
using namespace std;
char getUserAns( int QuestionCount ) ;
void compareAnswers ( const char user [ ] , const char scheme[ ] , int TOTAL_QUESTIONS) ;
int main( ) {
int const TOTAL_QUESTIONS = 20 ; // INPUT - total question in exam
char userAns[ TOTAL_QUESTIONS] ; // INPUT - user input
char schemeAns[ TOTAL_QUESTIONS] = { 'B' , 'D' , 'A' , 'A' , 'C' ,
'A' , 'B' , 'A' , 'C' , 'D' ,
'B' , 'C' , 'D' , 'A' , 'D' ,
'C' , 'C' , 'B' , 'D' , 'A' } ;
// Get the user answer for 20 questions
for ( int i = 0 ; i < 20 ; i++ )
{
userAns[ i] = getUserAns( i) ;
}
// Call the void function to make decision
compareAnswers ( userAns, schemeAns, TOTAL_QUESTIONS ) ;
return 0 ;
}
char getUserAns( int QuestionCount )
{
char input;
cout << "Enter the answer for Question " << ( QuestionCount + 1 ) ;
cin >> input;
while ( input ! = 'A' && input ! = 'B' && input ! = 'C' && input ! = 'D' )
{
cout << "Only enter letters A,B,C or D. Enter your answer again" ;
cin >> input;
}
cout << endl;
return input;
}
void compareAnswers ( const char user [ ] , const char scheme[ ] , int TOTAL_QUESTIONS)
{
int totalCorrect = 0 ; // OUTPUT - total correct answer entered
for ( int i = 0 ; i < TOTAL_QUESTIONS ; i++ )
{
if ( user[ i] == scheme[ i] )
{
totalCorrect + = 1 ;
}
else
{
cout << "Incorrect Answer for question #" << ( i+ 1 ) << endl;
}
}
cout << "Total correct answer(s) is " << totalCorrect << "/20\n " ;
cout << "Total incorrect answer(s) is " << ( TOTAL_QUESTIONS - totalCorrect) << "/20\n " ;
if ( totalCorrect >= 15 )
{
cout << "Congrats, you passed the test.\n " ;
}
else
{
cout << "Sorry, you failed the test.\n " ;
}
}
Ly8gU2FsaWhhICBCYWJhciAgICAgICAgICAgICAgICBDUzFBICAgICAgICAgICAgICAgQ2hhcHRlciA3LCBQYWdlIDQ0NiwgIzkKLy8KLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKgogKiBHUkFERSBNVUxUSVBMRSBDSE9JQ0UgUVVFU1RJT05TCiAqIF9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KICogVGhpcyBwcm9ncmFtIGFjY2VwdHMgYW5zd2VycyBmb3IgMjAgbXVsdGlwbGUgY2hvaWNlIHF1ZXN0aW9ucywgaW4gZm9ybSBvZgogKiBBLCBCLCBDLCBvciBELiBUaGVuIGl0IGNhbGN1bGF0ZXMgdG90YWwgY29ycmVjdCBhbnN3ZXJzLCB0b3RhbCBpbmNvcnJlY3QgCiAqIGFuc3dlcnMgYXMgd2VsbCBhcyBpbmNvcnJlY3QgcXVlc3Rpb25zIGxpc3QuCiAqIAogKiBUaGVyZSBpcyBubyBzcGVjaWZpYyBmb3JtdWxhIGZvciB0aGlzIHByb2dyYW0KICogX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCiAqIElOUFVUCiAqICAgICBUT1RBTF9RVUVTVElPTlMgICAgICA6IHRvdGFsIHF1ZXN0aW9uIGluIGFuIGV4YW0KICogICAgIHVzZXJBbnMgICAgICAgICAgICAgIDogdXNlciBhbnN3ZXIgZm9yIHF1ZXN0aW9ucyAKICogICAgIHNjaGVtZUFucyAgICAgICAgICAgIDogY29ycmVjdCBhbnN3ZXIgZm9yIHF1ZXN0aW9ucyAobWFya2luZyBzY2hlbWUpCiAqIE9VVFBVVAogKiAgICAgdG90YWxDb3JyZWN0ICAgICAgICAgOiB0b3RhbCBjb3JyZWN0IGFuc3dlciBlbnRlcmVkIGJ5IHVzZXIKICogKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKi8KI2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKCmNoYXIgZ2V0VXNlckFucyggaW50IFF1ZXN0aW9uQ291bnQgKTsKdm9pZCBjb21wYXJlQW5zd2VycyAoIGNvbnN0IGNoYXIgdXNlciBbXSwgY29uc3QgY2hhciBzY2hlbWVbXSAsIGludCBUT1RBTF9RVUVTVElPTlMpOwoKaW50IG1haW4oKSB7CglpbnQgY29uc3QgVE9UQUxfUVVFU1RJT05TID0gMjA7ICAgICAgICAgICAvLyBJTlBVVCAtIHRvdGFsIHF1ZXN0aW9uIGluIGV4YW0KCWNoYXIgdXNlckFuc1tUT1RBTF9RVUVTVElPTlNdOyAgICAgICAgICAgIC8vIElOUFVUIC0gdXNlciBpbnB1dAoJY2hhciBzY2hlbWVBbnNbVE9UQUxfUVVFU1RJT05TXSA9IHsnQicsICdEJywgJ0EnLCAnQScsICdDJywKCQkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgJ0EnLCAnQicsICdBJywgJ0MnLCAnRCcsCgkJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICdCJywgJ0MnLCAnRCcsICdBJywgJ0QnLAoJCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAnQycsICdDJywgJ0InLCAnRCcsICdBJ307CgkKCS8vIEdldCB0aGUgdXNlciBhbnN3ZXIgZm9yIDIwIHF1ZXN0aW9ucwoJZm9yIChpbnQgaSA9IDAgOyBpIDwgMjAgOyBpKyspCgl7CgkJdXNlckFuc1tpXSA9IGd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