pointer-to-object type

I've been working on this for longer than I'd like to admit. I also have read up on https://www.tutorialspoint.com/cplusplus/cpp_return_arrays_from_functions.htm but that honestly did not help. Could someone point myself (heh get it... point) in the right direction? This is my first attempt as calling an array in a function and I think I got that down but I'm unsure how to then compare the two arrays in the grade function. As a side note, this is for school and I'm not allowed to definite arrays outside of main. The error, as described in the title, is on line 33 for both portions where counter is within the brackets.

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
#include <iostream>
using namespace std;
void grade(char studA, char corrA, int size);
const int testSize = 10;
int main()
{
	char correctAnswers[testSize] = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D' }, studentAnswers[testSize];

	for (int counter = 0; counter < testSize; counter++)
	{
		cout << "Enter student's answer to question " << counter + 1 << ": " << endl;
		cin >> studentAnswers[counter];
		if (studentAnswers[counter] != 'A' || 'B' || 'C' || 'D')
		{
			int exitCounter = 2;
			cout << "\nInvlaid data. A, B, C and D are the only valid inputs. " << exitCounter << " attempts remaining." << endl;
			exitCounter--;
			if (exitCounter == 0)
			{
				cout << "\nGOOD BYE" << endl << endl;
				return 0;
			}
		}
	}
	grade(studentAnswers[testSize], correctAnswers[testSize], testSize);
	return 0;
}
void grade(char studA, char corrA, int size)
{
	int correctCounter = 0, incorrectCounter = 0;
	for (int counter = 0; counter < size; counter++)
	{
		if (studA[counter] == corrA[counter])
			correctCounter++;
		else
			incorrectCounter++;
	}
	if (correctCounter >= 8)
	{
		cout << "Congratulations!" << endl;
		cout << "You have passed exam." << endl;
		cout << "Total number of correct answers: " << correctCounter<< endl;
		cout << "Total number of incorrect answers: " << incorrectCounter << endl << endl;
	}
	else
	{
		cout << "Sorry, you have not passed exam!" << endl;
		cout << "Total number of correct answers: " << correctCounter << endl;
		cout << "Total number of incorrect answers: " << incorrectCounter << endl << endl;
	}
}
Last edited on
> The error, as described in the title,
if you don't understand the error message, then don't paraphrase it
just post it verbatim
gcc 33: error: invalid types ‘char[int]’ for array subscript
clang 33: error: subscripted value is not an array, pointer, or vector

you have
1
2
3
void grade(char studA, char corrA, int size){
//...
   if (studA[counter] == corrA[counter])
`studA' is not an array, but just one character

1
2
3
4
5
6
7
8
void grade(char studA[], char *corrA, int size); //the function expect pointers, [] and * would be equivalent
int main(){
//...
   grade(studentAnswers, correctAnswers, testSize); //calling the function, passing the arrays
}
void grade(char studA[], char corrA[], int size){
//...
}


also studentAnswers[counter] != 'A' || 'B' would be evaluated as (studentAnswers[counter] != 'A') || ('B') which is always true
you need to write studentAnswers[counter] != 'A' and studentAnswers[counter] != 'B'
If your problem is passing arrays back and forth from a function I strongly suggest you put aside your assignment with all the peripheral menu stuff and concentrate on a small tester program and concentrate on that.

If the problem is comparing two arrays then concentrate on that.

When you have solved those, one other or both, then take that experience back to the program at large.
Here's a rough, largely untested, start to fixing it up. Your if statement OR's were wrong and the testSize can be determined from the number of answers.

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

using namespace std;

void grade(char* studA, char* corrA, int size);

int main()
{
    char correctAnswer[] = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D' };
    int testSize = sizeof(correctAnswer)/sizeof(correctAnswer[0]); // <--
    
    char studentAnswer[testSize];
    
    for (int question_no = 0; question_no < testSize; question_no++)
    {
        cout << "Enter answer to question " << question_no + 1 << ": ";
        cin >> studentAnswer[question_no];
        
        if(
           studentAnswer[question_no] != 'A' ||
           studentAnswer[question_no] != 'B' ||
           studentAnswer[question_no] != 'C' ||
           studentAnswer[question_no] != 'D'
           )
        {
            int exitCounter = 2;
            cout
            << "Invalid data. A, B, C and D are the only valid inputs. "
            << exitCounter
            << " attempts remaining.\n\n";
            
            exitCounter--;
            if (exitCounter == 0)
            {
                cout << "GOOD BYE\n\n";
                return 0;
            }
        }
    }
    grade(studentAnswer, correctAnswer, testSize);
    
    return 0;
}

void grade(char* studA, char* corrA, int size)
{
    int correctCounter = 0, incorrectCounter = 0;
    for (int counter = 0; counter < size; counter++)
    {
        if (studA[counter] == corrA[counter])
            correctCounter++;
        else
            incorrectCounter++;
    }
    if (correctCounter >= 8)
    {
        cout << "Congratulations!" << endl;
        cout << "You have passed exam." << endl;
        cout << "Total number of correct answers: " << correctCounter<< endl;
        cout << "Total number of incorrect answers: " << incorrectCounter << endl << endl;
    }
    else
    {
        cout << "Sorry, you have not passed exam!" << endl;
        cout << "Total number of correct answers: " << correctCounter << endl;
        cout << "Total number of incorrect answers: " << incorrectCounter << endl << endl;
    }
}
@ne555 that was verbatim the error message, so I'm really unsure what you mean, aside from the preface "expression must have...(title)." Also, I did eventually correct the A || B issue or catching it rather but thank you for the correction on that. I honestly still don't understand it. It works, but I'm going to have to continue to read and watch videos. My professor doesn't really teach and I'm decent at self-teaching, but this is slowly becoming more and more out of my scope.
A real error message contains more information - for instance, the file, line and column number on which the error was detected.

For example:
main.cpp: In function 'void grade(char, char, int)':
main.cpp:33:20: error: invalid types 'char[int]' for array subscript
   33 |   if (studA[counter] == corrA[counter])
      |                    ^
main.cpp:33:38: error: invalid types 'char[int]' for array subscript
   33 |   if (studA[counter] == corrA[counter])
      |                                      ^


The original post was almost working.
Last edited on
> that was verbatim the error message (...)
> aside from the preface "expression must have...(title)."
so it wasn't verbatim
the "preface" was really important. It was expecting a pointer, but you didn't give one
(`studA' was not a pointer but it was used as such)

> I honestly still don't understand it.
¿what part?
Last edited on
Topic archived. No new replies allowed.