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.