Cannot split a void function into two separate void functions

Hi guys! This is my first post here, I am in need of assistance. I am a beginner taking a basic C++ programming class. I made a program that does what I want, which is compare user input answers to an array of correct answers to determine if the user passed an exam. The thing is, I only used one void function to accomplish this when I actually need to use two. I need to split up my checkAnswers function and create another called displayResult. The displayResult function should contain the number of incorrect and correct questions and also display whether or not the user passed the exam. When I tried to pass the code for those two expressions into a new void function (displayResult) it would not work properly and would display an error. I am using Xcode on a Mac. I am pretty sure it has to do with calling the function. I appreciate any help or insight I can get, please use simple terms or examples if you decide to help me, that is how I learn best.

Here is my code:

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

using namespace std;

void checkAnswers(char[], char[], int, int); // Prototype for the check answers function.

int main()
{
    const int numQuestions = 20; // Setting up labeled variables.
    const int minCorrect = 15;
    char examAnswers[numQuestions] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'D', 'D', 'A'};
    char inputAnswers[numQuestions]; // User input Array, above is the correct answer array.
    
    cout << "Welcome to the Florida State Driver's License Exam!\n";
    cout << "There is a total of 20 questions on this exam. You must answer at least 15 questions correctly to pass.\n";
    cout << "Good luck!\n\n";
    
    for (int response = 0; response < numQuestions; response++) // Loop for the user's input.
    {
        cout << "Question #" << (response + 1) << ": ";
        cin >> inputAnswers[response];
        inputAnswers[response] = toupper(inputAnswers[response]);
        
        while (inputAnswers[response] != 'A' && inputAnswers[response] != 'B' && inputAnswers[response] != 'C' && inputAnswers[response] != 'D')
        {
            cout << "You must enter A, B, C, or D." << endl; // The while loop is used for user input validation.
            cout << "Question #" << (response + 1) << ": ";
            cin >> inputAnswers[response];
            inputAnswers[response] = toupper(inputAnswers[response]);
        }

    }
    
    cout << "\n";
    
    checkAnswers(examAnswers, inputAnswers, numQuestions, minCorrect); // Checks the users answer as a function.
    
    cin.ignore(1);
    return 0;
}

void checkAnswers(char examAnswers[], char inputAnswers[], int numQuestions, int minCorrect) // Function to check exam and user answers.
{
    int correctAnswers = 0;
    
    for (int i = 0; i < numQuestions; i++)  // Checks the user responses against the correct answers.
    {
        if (examAnswers[i] == inputAnswers[i])
            correctAnswers++;
    }
    
    cout << "The list below shows the numbers of the incorrect questions:\n";  // Displays the incorrect question numbers.
    for (int i = 0; i < numQuestions; i++)
    {
        if (examAnswers[i] != inputAnswers[i])
            cout << "Question #" << i + 1 << " is incorrect." << endl;
    }
    
    
    if (correctAnswers >= minCorrect)  // Determines if the exam is a pass or fail, displays the results of the exam.
    {
        cout << "\nYou passed the exam!\n\n";
    }
    else
    {
        cout <<"\nYou failed the exam, please come back and test at a later date.\n\n";
    }
    
    cout << "Correct Answers = " << correctAnswers << endl;  // Shows both the correct and incorrect question count.
    cout << "Incorrect Answers = " << numQuestions - correctAnswers << endl;
    cout << "\n";

}
Last edited on
Hi,
Could you show us the new function you were trying to create... we can point out your mistakes and help you learn so...

PS: welcome to cplusplus.com
It was something along these lines, I deleted it because it didn't work, I should have kept it I apologize.

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>
#include <iomanip>
#include <cstring>

using namespace std;

void checkAnswers(char[], char[], int, int); // Prototype for the check answers function.
void displayResult(int correctAnswers);

int main()
{
    const int numQuestions = 20; // Setting up labeled variables.
    const int minCorrect = 15;
    char examAnswers[numQuestions] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'D', 'D', 'A'};
    char inputAnswers[numQuestions]; // User input Array, above is the correct answer array.
    
    cout << "Welcome to the Florida State Driver's License Exam!\n";
    cout << "There is a total of 20 questions on this exam. You must answer at least 15 questions correctly to pass.\n";
    cout << "Good luck!\n\n";
    
    for (int response = 0; response < numQuestions; response++) // Loop for the user's input.
    {
        cout << "Question #" << (response + 1) << ": ";
        cin >> inputAnswers[response];
        inputAnswers[response] = toupper(inputAnswers[response]);
        
        while (inputAnswers[response] != 'A' && inputAnswers[response] != 'B' && inputAnswers[response] != 'C' && inputAnswers[response] != 'D')
        {
            cout << "You must enter A, B, C, or D." << endl; // The while loop is used for user input validation.
            cout << "Question #" << (response + 1) << ": ";
            cin >> inputAnswers[response];
            inputAnswers[response] = toupper(inputAnswers[response]);
        }

    }
    
    cout << "\n";
    
    checkAnswers(examAnswers, inputAnswers, numQuestions, minCorrect); // Checks the users answer as a function.

displayResult(correctAnswers);
    
    cin.ignore(1);
    return 0;
}

void checkAnswers(char examAnswers[], char inputAnswers[], int numQuestions, int minCorrect) // Function to check exam and user answers.
{
    int correctAnswers = 0;
    
    for (int i = 0; i < numQuestions; i++)  // Checks the user responses against the correct answers.
    {
        if (examAnswers[i] == inputAnswers[i])
            correctAnswers++;
    }
    
    cout << "The list below shows the numbers of the incorrect questions:\n";  // Displays the incorrect question numbers.
    for (int i = 0; i < numQuestions; i++)
    {
        if (examAnswers[i] != inputAnswers[i])
            cout << "Question #" << i + 1 << " is incorrect." << endl;
    }

}

void displayResult(int correctAnswers)
{

if (correctAnswers >= 15)  // Determines if the exam is a pass or fail, displays the results of the exam.
    {
        cout << "\nYou passed the exam!\n\n";
    }
    else
    {
        cout <<"\nYou failed the exam, please come back and test at a later date.\n\n";
    }
    
    cout << "Correct Answers = " << correctAnswers << endl;  // Shows both the correct and incorrect question count.
    cout << "Incorrect Answers = " << 20 - correctAnswers << endl;
    cout << "\n";
}
Where in main() did you declare a variable named correctAnswers?

You should either call the displayResult() function from the checkAnswers() function or you need to return the computed correctAnswers from the function to be passed into your displayResult() function.
can you show me how to do that in the actual code?
The simplest (not necessarily the best) solution is to put the call to displayResults() at line 63.
Why is it not the best? Will the program still run as it should?

is that all I would code? just "displayResult()" or does anything need to go in the parentheses
s that all I would code? just "displayResult()" or does anything need to go in the parentheses

DisplayResult() requires the number of correct answers as it's argument.

Why is it not the best?

The best solution has already been suggested by jlb. I did not suggest it as it requires more changes.

correctAnswers belongs as a variable in main. You must then find a way to return the number of correct answers from checkAnswers() to main. When you can do that, then you can call displayResults() from main.
You can use pointers for the stuff suggested by AbstractionAnon...
Topic archived. No new replies allowed.