Function parameter error with passing array

The code worked, and then I tried to work with functions for more practice. The first code is the code without the function and the second is with the function. How do I get code 1 to work with the function that is in code 2?

Without function:
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
// asg7a.cpp
// Written by 
// Fall 2010
// Purpose: Proper Words

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    const int size = 20; // Array size
    char    answers[size] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D',
                            'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'},
        studentAnswers[size];// Holds the line of user input
    int count = 0;
    int sCount = 0;
    bool space = true;

    for (count = 0; count < size; count++)
    {
        if  (!space) // extra code to help with spacing
        {
            cout << "\n";
        }

        cout <<  "What is your score for test " << (count + 1) << " ? ";
        cin >> studentAnswers[count];

        while (studentAnswers[count] != 'A' && studentAnswers[count] != 'B' && 
                studentAnswers[count] != 'C' && studentAnswers[count] != 'D')
        {
            cout << "You must enter A, B, C, or D. ";
            cin >> studentAnswers[count];
            space = false;
        }
    }

    int incorrect = 0;
    for (sCount = 0; sCount < size; sCount++)
    {
        if (studentAnswers[sCount] != answers[sCount])
        {
            incorrect++;
        }
    }

    cout << incorrect;


    system("pause");
    return 0;
}



With function:
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
// asg7a.cpp
// Written by 
// Fall 2010
// Purpose: Proper Words

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void processAnswers(char, char, int);

int main()
{
    const int size = 20; // Array size
    char    answers[size] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D',
                            'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'},
        studentAnswers[size];// Holds the line of user input
    int count = 0;
    int sCount = 0;
    int incorrect = 0;
    bool space = true;

    for (count = 0; count < size; count++)
    {
        if  (!space) // extra code to help with spacing
        {
            cout << "\n";
        }

        cout <<  "What is your score for test " << (count + 1) << " ? ";
        cin >> studentAnswers[count];

        while (studentAnswers[count] != 'A' && studentAnswers[count] != 'B' && 
                studentAnswers[count] != 'C' && studentAnswers[count] != 'D')
        {
            cout << "You must enter A, B, C, or D. ";
            cin >> studentAnswers[count];
            space = false;
        }
    }

    processAnswers(studentAnswers[count], answers[size], size);

    system("pause");
    return 0;
}

void processAnswers(char studentAnswers[], char answers[], int size)
{
    int incorrect = 0;
    for (int sCount = 0; sCount < size; sCount++)
    {
        if (studentAnswers[sCount] != answers[sCount])
        {
            cout << incorrect++;
        }
    }
}


Your prototype and defintion don't match up for 1, 2nd you don't need studentAnsers[count] in your call, you just need studentAnswers, same goes for answers.

Just copy what you have in your definition into your prototype, also if you don't change either of the arrays I would go char &studentAnswers, char &answers just because its good coding practice.
Last edited on
I tried to look around, but I'm unfamiliar with the ampersand in front of the arrays
Using an ampersand would pass a reference of the array to your function, as opposed to making a complete new copy of it. This way if you have

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void editing(int &i)
{
	i = 10;
}

int main()
{
	int x;
	editing(x);
	cout << x << endl; //it prints 10, even though 10 wasn't assigned to that value in main.

	system("pause");
	return 0;
}


whereas if you have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void editing(int i) //no ampersand
{
	i = 10;
}

int main()
{
	int x = 3;
	editing(x);
	cout << x << endl;

	system("pause");
	return 0;
}

it will output three, because when passing x to the editing function it copied x into a new int container.
Topic archived. No new replies allowed.