*signs* I'm near tears here :cry:
I've edited the program and this is what I come up so far.
[...]
my program, however, is just stubborn and won't compile! It said "strcmp cannot convert parameter 1 to 'const char*' ". What did I do wrong now? did I intialize the pos function correctly?
Can you guys check my counter code please?
thanks in advance,
Karen
Well, you need to check how to use strcmp correctly. You need to pass it a pointer to a char, a char array, or another string for it to work correcly.
Also, you've got some really weird calls.
//Driver's License Exam
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
//prototypes
char StudentAns(int );
//global variable
char choice;
int main()
{
char key[20] = { 'B','D', 'A','A', 'C','A', 'B', 'A', 'C', 'D', 'B', 'C',
'D', 'A', 'D', 'C', 'B', 'B', 'D', 'A'};
int i;
char answer [20];
for(i = 0; i < 20; i++)
{
answer[i] = StudentAns(i);
}
//return 0; //This exits main before anything else below it is called. //You're losing a lot of your code.
int pos = 0; // initialize pos to point to the 0th index.
if (!strcmp(choice, key[pos])) // You still need to fix this by making choice a pointer.
cout << "This question is answered incorrectly."<<endl;
else
{
choice =0; // initialize counter
while (choice <=20)
{
cout << "you have answered " << choice++ << "correctly.\n"; //no idea what you're trying to do here; this will always read 'You have answered 1 correctly', because choice never increments.
}
}
if (choice++ >= 15)
{
cout << "you have passed the Driver's License Exam.\n";
cout << "Congratuations!\n";
}
else
{
cout << "You have failed the Driver's License Exam.\n";
cout << "Please try again next time.\n";
}
}
char StudentAns(int i)
{
char choice;
/* cout << "Please enter your choice for Question " << i+1 << endl;
cin >> choice;
choice = toupper(choice); */ //this can be condensed in your loop.
while(!(choice =='A'||choice=='B'||choice=='C'||choice=='D'))
{
cout << "For Question: "<<i+1<<"\n Answer with A, B, C, or D.\n";
cin >> choice;
choice = toupper(choice);
}
//This should work, saves a bit of code, and makes sure that you don't forget the question you are on.
return choice;
}
That's what I can figure out. But for some of your code, I don't know what you're trying to do.
Let's get to basics. Before you write code, you should be aware of what you want the code to do.
In simple sentences, describe your program first, like so:
*****
This program conducts and grades a multiple choice driver's exam.
It will need places to store:
An answer key with 20 answers.
The test-taker's answer(s)
The question number.
It will ask the student to enter answers until every question has a valid response, even if the answer is wrong.
It will then compare the student's answers to the answer key, and if the student has more than 15 of 20 answers correct, they pass. Otherwise, they fail.
*********
That should help you organize your thoughts.
If you're still confused, I'll show you how I'd do it once you give it another try.
I'd suggest you start over with your main function, and this time, have a character array for the student's answers of the same size as your answer key, grading the entire array at once with a simple while loop at the end of the program. That should save you a headache and tears.