Exam Program help

Hey guys, I need some help with a program I am working on. I am using an array to store the correct answers to a test. I ran into a problem when I try to have the user answer the questions. My for loop only shows Question 7. I want the user to have a screen similar to this.

1. Question
c //random answer input
2. Question
b
3. Question
a

etc..

Then at the end of all the questions I want to calculate the amount of questions answered correctly. I want to do this using my array to validate the answers. Am I thinking about this the right way?

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
  #include <iostream>
using namespace std;

void YourAnswers ()
{
	int x;
	char YourAnswer;
	cout << "Welcome to your exam!" << endl;
	cout << "This exam is made of 6 multiple choice questions" << endl;
	cout << "Answer the questions only using the multiple choices of a,b,c,d" << endl;
	cout << "\nLet's begin!" << endl;

for (x = 1; x < 7; x++);
{
	cout << x << ". Question" << endl;
	cin >> YourAnswer;
}

}

int main ()
{
	
	
	char RightAnswers [] = {'a', 'b', 'c', 'b', 'a', 'd'};
	char response;

	cout << "Hello welcome!" << endl;
	cout << "Are you ready to take your exam?" << endl;
	cout << "If you would like to proceed, press 'y'" << endl;
	cout << "If you would like to exit, press 'n'" << endl;
	cin >> response;

	while (response != 'y' && response != 'Y' && response != 'n' && response != 'N')
	{
		cout << "Invalid Request. Please enter 'y' or 'n'" << endl;
		cin >> response;
	}

	if (response == 'y' || response == 'Y')
		{
		cout << "Okay! Get ready to begin!" << endl;
		cout << "Now proceeding to exam..." << endl;
		}

	else if (response == 'n' || response == 'N')
		{
		cout << "Now exiting" << endl;
		exit (0);
		}

	YourAnswers ();	
	

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

}
Line 13: Look closely. Why do you have a semi-colon after the header of the for-loop? Its not a statement, so no semi-colon is needed. Remove it and the program should work.
Arslan7041,

Thank you so much. I must have missed seeing that when skimming over my code.
This solves what I was stuck on.
How can I validate my answers using the correct answers in my array?
Put the answers in a second array called userAnswers which is the same size as RightAnswers. Then check if both arrays match element by element.
Arslan7041,

How can I send the inputted answers to the second array called userAnswers?

In my code I put the user input array into my function "void YourAnswers ()". Is this proper?
To check them I assume if and else if statements would work fine using a similar example like this:

if (userAnswers [2] == RightAnswers [2])
cout << "This answer is correct" << endl;
else if (userAnswer [2] != RightAnswers [2])
cout << "This is incorrect" << endl;

Something like this?
Last edited on
Make the array in main. Then send it and its size to the YourAnswers function. In this function, put every answer the user inputs inside the array, like this:

1
2
3
4
5
for (x = 0; x < 6; x++) // array indices start at 0
{
    cout << x + 1 << ". Question" << endl; // notice, we print x+1, to start from 1
    cin >> userAnswers[x]; // put answer in x-th index of array
}


Once this function is done, it will return to main. From main, you should call another function called checkAnswer(). Send both arrays (rightAnswers and userAnswer) to this function as well as the size (size for both should be same).

Compare the contents of both arrays using a for-loop. I'll give the pseudocode, you should try to translate that to c++
1
2
3
4
    loop through array from 0 to size
           if i-th element of both arrays match, correct answer. 
           else, wrong answer. 
    end loop
Last edited on
Ok when doing the first part I put the UserAnswers array in main. But doing this makes UserAnswers undefined in my function.

In main I have that array looking like this.

UserAnswers [6];

EDIT: Never mind I missed a step. All good. I'm gonna keep going. I'll let you know if I run into any trouble.
Last edited on
Okay,

Where did I go wrong at? It doesn't matter what I input. All my answers are correct even with deliberate wrong answer inputs.
Thanks for your help. :-)

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
#include <iostream>
using namespace std;

void YourAnswers ()
{
	int x;
	char UserAnswers [6];
	cout << "Welcome to your exam!" << endl;
	cout << "This exam is made of 6 multiple choice questions" << endl;
	cout << "Answer the questions only using the multiple choices of a,b,c,d" << endl;
	cout << "\nLet's begin!" << endl;

for (x = 0; x < 6; x++)
{
	cout << x +1  << ". Question" << endl;
	cin >> UserAnswers[x];
}

cout << "You answered " << UserAnswers[0] << " for question 1" << endl;
cout << "You answered " << UserAnswers[1] << " for question 2" << endl;
cout << "You answered " << UserAnswers[2] << " for question 3" << endl;
cout << "You answered " << UserAnswers[3] << " for question 4" << endl;
cout << "You answered " << UserAnswers[4] << " for question 5" << endl;
cout << "You answered " << UserAnswers[5] << " for question 6" << endl;

}

void CheckAnswers ()
{
	int x;
	char UserAnswers [6];
	char RightAnswers [6];

for (x = 0; x < 6; x++)
{
	if (UserAnswers [x] == RightAnswers [x])
		cout << "You answered question " << x + 1 << " correctly" << endl;
	
	else if (UserAnswers [x] != RightAnswers [x])
		cout << "You answered question " << x + 1 << " incorrectly" << endl;
}
}
int main ()
{
	
	char UserAnswers [6];
	char RightAnswers [6] = {'a', 'b', 'c', 'b', 'a', 'd'};
	char response;

	cout << "Hello welcome!" << endl;
	cout << "Are you ready to take your exam?" << endl;
	cout << "If you would like to proceed, press 'y'" << endl;
	cout << "If you would like to exit, press 'n'" << endl;
	cin >> response;

	while (response != 'y' && response != 'Y' && response != 'n' && response != 'N')
	{
		cout << "Invalid Request. Please enter 'y' or 'n'" << endl;
		cin >> response;
	}

	if (response == 'y' || response == 'Y')
		{
		cout << "Okay! Get ready to begin!" << endl;
		cout << "Now proceeding to exam..." << endl;
		}

	else if (response == 'n' || response == 'N')
		{
		cout << "Now exiting" << endl;
		exit (0);
		}

	YourAnswers ();	
	CheckAnswers ();

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

}
closed account (E0p9LyTq)
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
#include <iostream>

void YourAnswers(char UserAnswers[])
{
   std::cout << "Welcome to your exam!\n";
   std::cout << "This exam is made of 6 multiple choice questions\n";
   std::cout << "Answer the questions only using the multiple choices of a,b,c,d\n";
   std::cout << "\nLet's begin!\n";

   for (int x = 0; x < 6; x++)
   {
      std::cout << x + 1 << ". Question: ";
      std::cin >> UserAnswers[x];
   }

   std::cout << "You answered " << UserAnswers[0] << " for question 1\n";
   std::cout << "You answered " << UserAnswers[1] << " for question 2\n";
   std::cout << "You answered " << UserAnswers[2] << " for question 3\n";
   std::cout << "You answered " << UserAnswers[3] << " for question 4\n";
   std::cout << "You answered " << UserAnswers[4] << " for question 5\n";
   std::cout << "You answered " << UserAnswers[5] << " for question 6\n";

}

void CheckAnswers(char UserAnswers[], char RightAnswers[])
{
   for (int x = 0; x < 6; x++)
   {
      if (UserAnswers[x] == RightAnswers[x])
      {
         std::cout << "You answered question " << x + 1 << " correctly\n";
      }
      else  // you really don't need to check for an incorrect answer
      {
         std::cout << "You answered question " << x + 1 << " incorrectly\n";
      }
   }
}


int main ()
{
   char UserAnswers[6];
   char RightAnswers[6] = {'a', 'b', 'c', 'b', 'a', 'd'};
   char response;

   std::cout << "Hello welcome!\n";
   std::cout << "Are you ready to take your exam?\n";
   std::cout << "If you would like to proceed, press 'y'\n";
   std::cout << "If you would like to exit, press 'n'\n";
   std::cin >> response;

   while (response != 'y' && response != 'Y' && response != 'n' && response != 'N')
   {
      std::cout << "Invalid Request. Please enter 'y' or 'n'\n";
      std::cin >> response;
   }

   if (response == 'y' || response == 'Y')
   {
      std::cout << "Okay! Get ready to begin!\n";
      std::cout << "Now proceeding to exam...\n";
   }
   else if (response == 'n' || response == 'N')
   {
      std::cout << "Now exiting\n";
      exit (0);
   }

   YourAnswers(UserAnswers);	
   CheckAnswers(UserAnswers, RightAnswers);
}


Hello welcome!
Are you ready to take your exam?
If you would like to proceed, press 'y'
If you would like to exit, press 'n'
y
Okay! Get ready to begin!
Now proceeding to exam...
Welcome to your exam!
This exam is made of 6 multiple choice questions
Answer the questions only using the multiple choices of a,b,c,d

Let's begin!
1. Question: a
2. Question: a
3. Question: c
4. Question: c
5. Question: a
6. Question: a
You answered a for question 1
You answered a for question 2
You answered c for question 3
You answered c for question 4
You answered a for question 5
You answered a for question 6
You answered question 1 correctly
You answered question 2 incorrectly
You answered question 3 correctly
You answered question 4 incorrectly
You answered question 5 correctly
You answered question 6 incorrectly
Last edited on
FurryGuy,

Your response did the trick. I appreciate your help!

Arslan7041,

Thanks for all your help!

Thank you guys! :-)
closed account (E0p9LyTq)
Do you understand the changes I made to your functions? And why I made the changes?

Passing by reference/pointer, especially with an array, can be a bit hard to understand if you have never been taught that concept.
FurryGuy,

I have learned this concept some time ago when I took a college class on intro programming and C++. But due to it being a online course I didn't grasp the concepts as well as I should have. I've been relearning the material myself and it's been going way smoother and faster than before. The reference/pointer and the changes to the functions inside of the parenthesis aren't exactly clear to me. I would love to learn this material. If you could explain the changes I would very greatly appreciate it!

Thanks! :-)

EDIT :

For example, explaining this
 
void CheckAnswers(char UserAnswers[], char RightAnswers[])


and

1
2
YourAnswers(UserAnswers);	
CheckAnswers(UserAnswers, RightAnswers);


Last edited on
closed account (E0p9LyTq)
I actually made some more changes:

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
#include <iostream>

void GetAnswers(char UserAnswers[], const int array_size)
{
   std::cout << "Welcome to your exam!\n";
   std::cout << "This exam is made of 6 multiple choice questions\n";
   std::cout << "Answer the questions only using the multiple choices of a,b,c,d\n";
   std::cout << "\nLet's begin!\n";

   for (int x = 0; x < array_size; x++)
   {
      std::cout << x + 1 << ". Question: ";
      std::cin >> UserAnswers[x];
   }

   for (int x = 0; x < array_size; x++)
   {
      std::cout << "You answered " << UserAnswers[x] << " for question " << x + 1 << "\n";
   }
}

void CheckAnswers(const char UserAnswers[], const char RightAnswers[], const int array_size)
{
   for (int x = 0; x < array_size; x++)
   {
      if (UserAnswers[x] == RightAnswers[x])
      {
         std::cout << "You answered question " << x + 1 << " correctly\n";
      }
      else
      {
         std::cout << "You answered question " << x + 1 << " incorrectly\n";
      }
   }
}


int main()
{
   const int array_size = 6;
   const char RightAnswers[array_size] = {'a', 'b', 'c', 'b', 'a', 'd'};
   char UserAnswers[array_size];
   char response;

   std::cout << "Hello welcome!\n";
   std::cout << "Are you ready to take your exam?\n";
   std::cout << "If you would like to proceed, press 'y'\n";
   std::cout << "If you would like to exit, press 'n'\n";
   std::cin >> response;

   while (response != 'y' && response != 'Y' && response != 'n' && response != 'N')
   {
      std::cout << "Invalid Request. Please enter 'y' or 'n'\n";
      std::cin >> response;
   }

   if (response == 'y' || response == 'Y')
   {
      std::cout << "Okay! Get ready to begin!\n";
      std::cout << "Now proceeding to exam...\n";
   }
   else if (response == 'n' || response == 'N')
   {
      std::cout << "Now exiting\n";
      exit (0);
   }

   GetAnswers(UserAnswers, array_size);	
   CheckAnswers(UserAnswers, RightAnswers, array_size);
}


Lines 40-42: I declared a constant used to create arrays.
1
2
3
const int array_size = 6;
const char RightAnswers[array_size] = {'a', 'b', 'c', 'b', 'a', 'd'};
char UserAnswers[array_size];

Now you have declared two arrays in main() you need to use in your functions.

Line 3: void GetAnswers(char UserAnswers[], const int array_size)
You pass the UserAnswers[] array to the function, as a reference, so you don't have a local copy being created. Any changes you make to UserAnswers[] is passed back to main() when the function returns.

const int array_size passes the size of your array to the function, and makes it impossible to change the value by declaring the value to be constant. You don't want to change it. Change array_size in the function and the compiler will flag the error.

void CheckAnswers(const char UserAnswers[], const char RightAnswers[], const int array_size)
You pass both of your array to the function (and the array size) and declare them constant because the function doesn't need to change the values. You are only making comparisons with prefetched values. If you accidentally do change any of the values the compiler will flag the function with an error.

1
2
GetAnswers(UserAnswers, array_size);	
CheckAnswers(UserAnswers, RightAnswers, array_size);

The function calls pass your locally created arrays and array_size variable into your functions.

Why this elaborate way of doing things? So you don't have to create the arrays as global. Creating variables at global scope can make it hard to track where the values are changed. By creating your variable in main() only and passing references to your functions you know exactly where things are changed. Declaring parameters with const is error-checking.

You were declaring arrays in main() and your functions. Each function had a local copy of the arrays that were not being passed around.

There are other ways to write a function declaration so you pass arrays without doing a local scope copy, I personally find the (char myArray[]) form to be easier to understand what is happening.
FurryGuy,

I really appreciate all your help! Thanks!
Topic archived. No new replies allowed.