hi, I know it's me again.
It's HW, and I'm supposed to write a program that grades an example of 20 questions. I'm supposed to store the correct answers in an array. And then ask the user to enter the student's answers for each of the 20 questions, whic should be stored in another array. Then if the student got more than 15 questions right, I have to display a message that says "PASS". Moreover, if the choices they enter is not A, B, C, or D, I have to tell them to enter it again.
So far, I only got to the part where student has to enter his answer, but I don't know how to do it, since it a two dimensational array.

can anyone help?

this is my code so far

//driver's license Exam

#include <iostream.h>

void StudentAns(char [20][2]);
char choice, A, B, C, D;

void main(void)
{
    char key[20][2] = { {1, 'B'}, {2, 'D'}, {3, 'A'}, {4, 'A'},
    {5, 'C'}, {6, 'A'}, {7, 'B'}, {8, 'A'}, {9, 'C'}, {10, 'D'},
    {11, 'B'}, {12, 'C'}, {13, 'D'}, {14, 'A'}, {15, 'D'}, {16, 'C'}, 
    {17, 'B'}, {18, 'B'}, {19, 'D'}, {20, 'A'} };

    StudentAns(key);
}

void StudentAns(char answer[20][2])
{
        for (int quest = 1; quest <=20; quest++)
        {
            cout << "Please enter your choice for Question " << quest << endl;
            cin >> choice;
            do
            {
                cout << "You must enter A, B, C, or D.\n";
                cin >> choice;
            }while (choice !='A'|| choice != 'B'|| choice != 'C' || choice != 'D');
        }
}

Recommended Answers

All 15 Replies

why do you need to have this as a 2d array?
if you just want the numbers 1-20, you can change the function StudentAns to:

void StudentAns(char answer[20])
{
    for (int quest = 1; quest <=20; quest++)
    {
        cout << "Please enter your choice for Question " << quest << endl;
        cin >> choice;
        do
        {
            cout << "You must enter A, B, C, or D.\n";
            cin >> choice;
        }while (choice !='A'|| choice != 'B'|| choice != 'C' || choice != 'D');
        answer[quest-1] = choice;
    }
}

and remeber to make your char key[20][2] to char key[20], and do the nessasery changes in it.

and: main shuld return int, and use iostream instead for iostream.h :)

Hey
Ok I've thought of this before...you know to make it a 1-D array not 2-D array. the thing is my answer key, which is in the main function, is 2-D. How the heck am I supposed to compare 1-D array to a 2-D array. I have to grade the Student Answers!
and what do you mean "main should use int"?
This is my code now:

// Project #7 Driver's License Exam

#include <iostream.h>

void StudentAns(char [20]);
char choice, A, B, C, D;

void main(void)
{
    char key[20][2] = { {1, 'B'}, {2, 'D'}, {3, 'A'}, {4, 'A'},
    {5, 'C'}, {6, 'A'}, {7, 'B'}, {8, 'A'}, {9, 'C'}, {10, 'D'},
    {11, 'B'}, {12, 'C'}, {13, 'D'}, {14, 'A'}, {15, 'D'}, {16, 'C'}, 
    {17, 'B'}, {18, 'B'}, {19, 'D'}, {20, 'A'} };

    StudentAns(answer);
}

void StudentAns(char answer[20])
{
    for (int quest = 1; quest <=20; quest++)
    {
        cout << "Please enter your choice for Question " << quest << endl;
        cin >> choice;
        do
        {
            cout << "You must enter A, B, C, or D.\n";
            cin >> choice;
        }while (choice !='A'|| choice != 'B'|| choice != 'C' || choice != 'D');
        answer[quest-1] = choice;
    }
}

make the key 1d?
like:

char key[20]=  {'B', 'D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','B', 'B','D','A'};

remove : "change answer[quest-1] = choice;" and put:

if (choise == answer[quest])
{
// do things that happends when he have the right answer
}

and last change StudentAns(answer); to StudentAns(key);

hey, thanks a bunch...your advice gives me an idea to the esolution.
one last question (hopefully), did I do something wrong with this part of my code?

void StudentAns(char answer[20])
{
    for (int quest = 1; quest <=20; quest++)
    {
        cout << "Please enter your choice for Question " << quest << endl;
        cin >> choice;
        do
        {
            cout << "You must enter A, B, C, or D.\n";
            cin >> choice;
        }while (choice !='A'|| choice != 'B'|| choice != 'C' || choice != 'D');
     }

because when I try to execute it, it keeps on popping the sentence "you must enter A, B, C, or D." even if I did enter the right choice. in other words, it keeps iterates.

Do you know what the problem is?

hey, thanks a bunch...your advice gives me an idea to the esolution.
one last question (hopefully), did I do something wrong with this part of my code?

void StudentAns(char answer[20])
{
    for (int quest = 1; quest <=20; quest++)
    {
        cout << "Please enter your choice for Question " << quest << endl;
        cin >> choice;
        do
        {
            cout << "You must enter A, B, C, or D.\n";
            cin >> choice;
        }while (choice !='A'|| choice != 'B'|| choice != 'C' || choice != 'D');
     }

because when I try to execute it, it keeps on popping the sentence "you must enter A, B, C, or D." even if I did enter the right choice. in other words, it keeps iterates.

Do you know what the problem is?

Yes. You're using logical 'ors'. What you've got is a logic error in the do/while loop.
Whenever you do logical operations, read what you've written aloud.
What your code says is:
"Repeat while the choice is not A or B or C or D."
This is faulty logic for loop termination. Even if you pick A, B,C, or D, A!=B!=C!=D. Ergo, unless you enter something that's simultaneously equivalent to A,B,C, and D, one of your conditions will be positive, resulting in the infinite loop you're noticing.

There are two ways to resolve this.
Try switching from || to &&.
or...
use XOR (exclusive or), ^.
Then, your logical statement becomes:
"Repeat while the choice is not A and not B and not C and not D"
or
"Repeat while the choice is not one of the following answers: A, B, C or D"
respectively.


XOR is best thought of as meaning "One or the other, but not both(more than one, if there are multiple entries)." It's very handy for things like this, and is supposedly a major part of some of the most efficient algorithms known.

For now, you should probably just stick with the &&.

One last consideration is to use DeMorgan's Law (google it yourself)
In this, you could say:

while(!(choice =='A'||choice=='B'||choice=='C'||choice=='D'))

which is equivalent to your current code with && substituted in.
It reads, by the way, as
"While it is not the case that choice is A, B, C, or D."

*signs* I'm near tears here :cry:

I've edited the program and this is what I come up so far.

//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;

	int pos = 0;    // initialize pos to point to the 0th index.
	if (!strcmp(choice, key[pos]))
			cout << "This  question is answered incorrectly.\n";
	else 
	{ choice =0;  // initialize counter
	while (choice <=20)
	{
		cout << "you have answered " << choice++ << "correctly.\n";
	}
	}

	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);
		
	while(!(choice =='A'||choice=='B'||choice=='C'||choice=='D'))
	{
		cout << "You must enter A, B, C, or D.\n";
		cin >> choice;
	}

	return choice;
}

<< moderator edit: added [code][/code] tags -- learn to use them >>


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

*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.

To see it simpler this way in terms of schematics, here we are:

There are two phases in your program. The test-taking phase, and the test-grading phase. Both can be done in a loop, as you have 20 questions to ask, and then 20 to grade, both of which can be done via repetitive action.

Using choice as a global variable is not necessary - it's only needed in asking the questions, which you can put in a method - you already have. You can assign the answers straight to the answers array, which you also already have.

Grading the test should not require strcmp. As both the answer and the key are characters, they should be able to function under the equivalence operator just fine. So here's a little skeleton to go off of in 'pseudo-pseudocode'.

char ask(int i) {
   declare holder for the input
   console output the prompt for them
   do {
      ask them for input, inserted into the holder
   } while the input within the holder isn't satisfactory
   return the input in form of the holder
}

void main() {
   declare array, fill with your answer key
   declare empty array to store answers
   declare an int to hold the number of right answers
   for(int i = 0; i < 20; i++) {
            fill the empty array slot by slot according to i, using your asking method
   }
   for(int j = 0; j < 20; j++) {
            check each answer against its respective slot in the answer key
               if right, add 1 to right answers, otherwise add 0
   }
   check number of right answers
       if more than 15, they pass, else they fail
   return 0;
}

More or less what I was thinking, but I'll still do a complete solution if you need it.

Actually, it's more efficient to check each answer as it goes, so only one loop is needed. Then again, that depends on the nature of grading one wants. I mean, you could do neat things like automatically fail them when they get 6 wrong, or auto-pass at 15 right. ;)

OK, I gave it one more try, And this is my last solution, I have no idea how else to do this

//Driver's License Exam;
//This program first enter the answer key with 20 answers
//then it will ask the student to enter his/her answer
//The program then totals up the number of questions answered correctly and incorrectly
//The student must answer at least 15 questions right in order to pass the exam.

#include <iostream>
#include <stdio.h>
#include <string.h>


using namespace std;

char StudentAns(int );

int main()
{
int i, correct = 0, incorrect =0, total1, total2;

char *key = "BDAACABACDBCDADCBBDA"; //THE ANSWER KEY

char answer[20]; //DECLARE THE STUDENT ANSWER ARRAY

//STUDENT ENTER ANSWERS.
for(i = 0; i < 20; i++)
	{
		answer[i] = StudentAns(i);		
	}
	
//SHOW RESULT;
	cout << "THIS IS YOUR RESULT.\n";
	for(i = 0; i < 20; i++)
	{
		if(answer[i] == key[i])
		{
			cout << "this answer is correct.\n";
			correct++; //INTITIALIZE COUNTER FOR CORRECT ANSWER
		}
		else
		{
			cout << answer [i] << " is an incorrect choice.\n";
				incorrect++; //INNITIALIZE COUNTER FOR INCORRECT ANSWER
		}
	}

	total1 += correct++; //TOTAL UP THE CORRECT ANSWERS
	total2 += incorrect++; //TOTAL UP THE INCORRECT ANSWERS

	cout << "you have answered " << total1 << " questions correctly.\n";
	cout << "You have answered " << total2 << " questions incorrectly.\n";

	if (total1 >= 15)
	{
		cout << "\nCONGRATUATIONS! YOU HAVE PASSED THE DRIVER'S LICENSE TEST.\n";
	}
	else 
	{
		cout << "\nSorry. You have failed the Driver's License Exam.\n";
		cout << "\nYou must answer at least 15 questions right in order to pass the exam.\n";
	}

	return 0;
}


char StudentAns(int i)
{
	
	char choice;
		
	cout << "Please enter your choice for Question " << i+1 << endl;
	cin >> choice;
	choice = toupper(choice);
		
	while (choice != 'A' && choice != 'B'&& choice != 'C' && choice != 'D')
	{
		cout << "You must enter A, B, C, or D.\n";
		cin >> choice;
	}

	return choice;
}

I tried to compile this and it worked out pretty well except for the totaling up the correct answers part. Can you guys please check it?

OMG. I think I'm fainting!

//Driver's License Exam;
//This program first enter the answer key with 20 answers
//then it will ask the student to enter his/her answer
//The program then totals up the number of questions answered correctly and incorrectly
//The student must answer at least 15 questions right in order to pass the exam.

#include <iostream>
#include <stdio.h>
//#include <string.h>


using namespace std;

char StudentAns(int );

int main()
{
int i, correct = 0, incorrect =0; //, total1, total2;

char *key = "BDAACABACDBCDADCBBDA"; //THE ANSWER KEY

char answer[20]; //DECLARE THE STUDENT ANSWER ARRAY

//STUDENT ENTER ANSWERS.
for(i = 0; i < 20; i++)
	{
		answer[i] = StudentAns(i);		
	}
	
//SHOW RESULT;
	cout << "THIS IS YOUR RESULT.\n";
	for(i = 0; i < 20; i++)
        {
            cout<<"Question "<<(i+1)<<":"<<endl;
		if(answer[i] == key[i])
		{
			cout << "was answered is correct.\n";
			correct++; //INTITIALIZE COUNTER FOR CORRECT ANSWER
		}
		else
		{
		cout << answer [i] << " is an incorrect choice.\n";
		incorrect++; //INNITIALIZE COUNTER FOR INCORRECT ANSWER
		}
	}

//	total1 += correct++; //TOTAL UP THE CORRECT ANSWERS
//	total2 += incorrect++; //TOTAL UP THE INCORRECT ANSWERS

	cout << "you have answered " << correct << " questions correctly.\n";
	cout << "You have answered " << incorrect << " questions incorrectly.\n";

	if (correct >= 15)
	{
		cout << "\nCONGRATUATIONS! YOU HAVE PASSED THE DRIVER'S LICENSE TEST.\n";
	}
	else 
	{
		cout << "\nSorry. You have failed the Driver's License Exam.\n";
		cout << "\nYou must answer at least 15 questions right in order to pass the exam.\n";
	}

	return 0;
}


char StudentAns(int i)
{
	
	char choice;
		
	cout << "Please enter your choice for Question " << i+1 << endl;
	cin >> choice;
	choice = toupper(choice);
		
	while (choice != 'A' && choice != 'B'&& choice != 'C' && choice != 'D')
	{
		cout << "You must enter A, B, C, or D.\n";
		cin >> choice;
                choice = toupper(choice);
	}

	return choice;
}

I tried to compile this and it worked out pretty well except for the totaling up the correct answers part. Can you guys please check it?

OMG. I think I'm fainting!

Try that edit. Looks pretty good to me, but I didn't run it myself.

One problem was that on lower-case answers that weren't valid (for example: e), you didn't convert the case to make a lower-case a upper case, which made your code inconsistent.
But mainly: You had too many variables. If you're incrementing correct and incorrect, why do you need total1 and total2?

HEY GUYS I FIGURED IT ALL OUT!!!!!!! YAY FOR ME!!! :mrgreen:

I'm posting this online anyway, for any beginner, like myself, that have programs that are similar to mine. :) you guys can use this as a guide. I find an example a lot easier to understand than explanation :lol:

//Driver's License Exam;
//This program first enter the answer key with 20 answers
//then it will ask the student to enter his/her answer
//The program then totals up the number of questions answered correctly and incorrectly
//The student must answer at least 15 questions right in order to pass the exam.

#include <iostream>
#include <stdio.h>
#include <string.h>


using namespace std;

char StudentAns(int );

int main()
{
int i, correct = 0, incorrect =0;

char *key = "BDAACABACDBCDADCBBDA"; //THE ANSWER KEY

char answer[20]; //DECLARE THE STUDENT ANSWER ARRAY

//STUDENT ENTER ANSWERS.
for(i = 0; i < 20; i++)
    {
        answer[i] = StudentAns(i);      
    }

//SHOW RESULT;
    cout << "THIS IS YOUR RESULT.\n";
    for(i = 0; i < 20; i++)
    {
        if(answer[i] == key[i])
        {
            cout << "this answer is correct.\n";
            correct++; //update COUNTER FOR CORRECT ANSWER
        }
        else
        {
            cout << answer [i] << " is an incorrect choice.\n";
                incorrect++; //update COUNTER FOR INCORRECT ANSWER
        }
    }

    cout << "You have answered " << correct << " questions correctly.\n";
    cout << "You have answered " << incorrect << " questions incorrectly.\n";

    if (correct >= 15)
    {
        cout << "\nCONGRATUATIONS! YOU HAVE PASSED THE DRIVER'S LICENSE TEST.\n";
    }
    else 
    {
        cout << "\nSorry. You have failed the Driver's License Exam.\n";
        cout << "\nYou must answer at least 15 questions right in order to pass the exam.\n\n";
    }

    return 0;
}


char StudentAns(int i)
{

    char choice;

    cout << "Please enter your choice for Question " << i+1 << endl;
    cin >> choice;
    choice = toupper(choice);

    while (choice != 'A' && choice != 'B'&& choice != 'C' && choice != 'D')
    {
        cout << "You must enter A, B, C, or D.\n";
        cin >> choice;
    }

    return choice;
}

Thank you all who have been really patient with me throughout this HW question. Your advices were an immense help. I'm so grateful I discovered this forums. you guys are THE BEST!!!!! :mrgreen:

THANKS AGAIN!

HEY, DROWZEE
that was the exact thing I did! wow, without help, too! I figured it out right before i check this website for, like, the hundredth time! Man, don't I sound arrogant! i'm just so darn happy right now. I've been on that project for like 4 days.
anyway, thanks for all your help Drowzee, you were great!

Karen

No problem.
Hah. Just got another 'reminder' that I need to become much better at C/C++ from one of the employees here.
No matter how much you learn, there's always someone better than you to draw on...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.