Here's a simple C example, should work just fine but not really tested.
Code:
#include <stdio.h>
/* You had this set to 20 but I believe
* you only had 18 answers actually set
*/
#define NUM_ANSWERS 18
#define MIN_CORRECT 15
void print_title()
{
printf(" =====================================\n");
printf(" DMV Driver's License Exam \n");
printf(" =====================================\n");
}
int tally(char * key, char * answers)
{
int correct = 0;
int i;
for(i = 0; i < NUM_ANSWERS; i++)
if(key[i] == answers[i])
correct++;
return correct;
}
int main(void)
{
int i = 0;
int e = 0;
int correct;
char buf[4];
char ans[NUM_ANSWERS];
char key[] = "BDAACABACDBCDCCBDA";
char * p;
print_title();
while(i++ < NUM_ANSWERS)
{
if(e == 0)
printf("Please enter answer %d in CAPITAL LETTERS.\n>", i);
p = fgets(buf, 3, stdin);
if(p[1] != '\n')
{
printf("Invalid input - too many characters. Please re-enter answer %d in CAPITAL LETTERS.\n>", i);
e = 1;
i--;
while(p[0] != '\n') /* I'm on linux... not a portable check */
p[0] = getchar();
continue;
} else if(*p < 'A' || *p > 'D') {
printf("Invalid input. Please re-enter answer %d in CAPITAL LETTERS.\n>", i);
e = 1;
i--;
continue;
}
e = 0;
ans[i-1] = *p;
}
correct = tally(key, ans);
if(correct >= MIN_CORRECT)
printf("\nCongratulations! You passed the driving exam!\n");
else
printf("\nWe're sorry, but you have not passed the driving exam.\n");
printf("You answered %d questions correctly and %d incorrectly.\n", correct, NUM_ANSWERS-correct);
printf("\nThis is the end of the DMV Driving Exam. Goodbye!!\n");
return 0;
}