Expected Primary Expression Before Token

Pages: 12
The following code won't compile, I keep getting
Expected primary-expression before ‘[’ token
. Removing the asterisk changes the error to
Invalid conversion from int* to int
and
Too few arguments
. Removing what's in the brackets gives me the same error and
Expected primary-expression before ‘]’ token
.

1
2
3
4
5
6
7
if (compFirst == true) { lastNum = compTurn (poisonNum, max, lastNum, enteredNum, stepStones* [poisonNum / max] ) ; }
	while (gameOver == false) {
		if (gameOver == false) { lastNum = userTurn (poisonNum, max, lastNum, enteredNum, stepStones* [poisonNum / max] ) ; }
		else { break ; }
		if (gameOver == false) { lastNum = compTurn (poisonNum, max, lastNum, enteredNum, stepStones* [poisonNum / max] ) ; }
		else { break ; }
		}
Last edited on
I guess you want round brackets in place of the square ones
That just changes it to
Expected primary-expression before ‘{’ token
Last edited on
Round brackets are ( )
Invalid operands of type 'int*' and 'int' to binary 'operator*'
Last edited on
You have to say what line the error is occurring on, and if it is one of the lines above, provide
declarations for all the variables and functions.
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
using namespace std ;

int main (int argc, char * const argv [] ) {
	// Method declarations
	void pseudoMain () ;
	
	copyright () ;
	pseudoMain () ;
	
	// Return before end of script
	cout << endl << endl ;
	return 0 ;
	}

void pseudoMain () {
	// Variable declarations
	char compFirstChar ;
	bool compFirst ;
	bool gameOver ;
	int poisonNum ;
	int max ;
	int lastNum ;
	int midlNum ;
	int enteredNum = 1 ;
	
	// Method declarations
	void restart () ;
	void error (int errorCode) ;
	int determineStepStones (int stepStones [] , int poisonNum, int max) ;
	int display (int lastNum, int enteredNum) ;
//f	void play (int enteredNum) ;
	int compTurn (int poisonNum, int max, int lastNum, int midlNum, int enteredNum, int stepStones [] ) ;
	int userTurn (int poisonNum, int max, int lastNum, int midlNum, int enteredNum, int stepStones [] ) ;
	
	// Set the variables
	cout << "Would you like the computer to go first (y|n) ?	" ;
	cin >> compFirstChar ;
	switch (compFirstChar) {
		case 'y' :
			compFirst = true ;
			break ;
			
		case 'n' :
			compFirst = false ;
			break ;

		default:
			error (1) ;
			break ;
		}
	cout << "Please enter a positive number to be the “poison” number:	" ;
	cin >> poisonNum ;
	cout << "Please enter a positive number to be the maximum number of numbers that a player may go through at any one time:	" ;
	cin >> max ;
	int* stepStones [poisonNum / max] ;
	determineStepStones (stepStones [poisonNum / max] , poisonNum, max) ;
	
	// Start the game
	system ( "clear" ) ;
	
	if (compFirst == true) { lastNum = compTurn (poisonNum, max, lastNum, enteredNum, stepStones* [poisonNum / max] ) ; }
	while (gameOver == false) {
		if (gameOver == false) { lastNum = userTurn (poisonNum, max, lastNum, enteredNum, stepStones* [poisonNum / max] ) ; }
		else { break ; }
		if (gameOver == false) { lastNum = compTurn (poisonNum, max, lastNum, enteredNum, stepStones* [poisonNum / max] ) ; }
		else { break ; }
		}
	}

void restart () {
	cout << endl << "Restarting script…" << endl ;
	system ( "clear" ) ;
	copyright () ;
	pseudoMain () ;
	}

void error (int errorCode) {
	switch (errorCode) {
		case 0:
			cout << "Error 0 (983-853) : an unknown error has occured." ;
			restart () ;
			break ;
			
		case 1:
			cout << "Error 1 (96-483.23) : you did not enter a lowercase 'y' (for yes) or a lowercase 'n' (for no) when I asked you if you wanted the computer to go first.  Remember, computers are picky, so you must be pseudoperfect!"  << endl ;
			restart () ;
			break ;

		case 2:
			cout << "Error 2 (46-3662) : the number you have entered is not acceptable.  You must enter a number between the two numbers given." ;
			break ;
			
		default:
			error (0) ;
			break ;
		}
	}

int determineStepStones (int stepStones [] , int poisonNum, int max) {
	// This is backwards, but the resulting array makes more sense.
	// Sets the last “stepping stone” number to what it is (the “poison” number minus one)
	stepStones [poisonNum / max] = poisonNum - 1 ;
	// Sets all of the rest of the “stepping stone” numbers to what they are (the previous “stepping stone” number minus max)
	for (int i = poisonNum / max ; i >= 0 ; i -- ) { stepStones [i] = stepStones [i - 1] - (max + 1) ; }
	}

int display (int lastNum, int enteredNum) {
	cout << endl ;
	for (int i = lastNum ; i <= enteredNum ; i ++ ) {
		cout << i << ", " ;
		}
	cout << enteredNum << endl ;
	}

int compTurn (int poisonNum, int max, int lastNum, int midlNum, int enteredNum, int* stepStones [] ) {
	enteredNum = midlNum ;
	
	for (int i = enteredNum ; i < enteredNum + max ; i ++ ) {
		for (int j = 0 ; j < poisonNum / max ; j ++ ) {
			if (i == stepStones [j] ) {
				display (lastNum, enteredNum) ;
				break ;
				}
			}
		}
	
	midlNum = lastNum ;
	return lastNum ;
	}

int userTurn (int poisonNum, int max, int lastNum, int midlNum, int enteredNum, int* stepStones [] ) {
	enteredNum = midlNum ;
	
	cout << "Enter a number above " << lastNum << " and below " << lastNum + max + 1 << " (exclusive) :	" ;
	cin >> enteredNum ;
	if (lastNum < enteredNum <= lastNum + max) { display (lastNum, enteredNum) ; }
	else {
		error (2) ;
		midlNum = enteredNum ;
		lastNum = userTurn (poisonNum, max, lastNum, midlNum, enteredNum, stepStones* [poisonNum / max] ) ;
		return lastNum ;
		}

	midlNum = lastNum ;
	return lastNum ;
	}
lines 62, 64, 66, and 141:
Expected primary-expression before ‘[’ token
No matter how many levels down you go with expressions, it will ALWAYS use the () parenthesis.
For example:a = (this * (this + that - (something / somethat) * somestuff));
Right…
So, that is your problem. You're using [] instead of () for the second level of expressions.
lastNum = userTurn (poisonNum, max, lastNum, midlNum, enteredNum, stepStones* [poisonNum / max] ) ;

is what is giving me the

Expected primary-expression before ‘[’ token

error. Changing it to

lastNum = userTurn (poisonNum, max, lastNum, midlNum, enteredNum, stepStones* (poisonNum / max) ) ;

gives me the

Invalid operands of type 'int*' and 'int' to binary 'operator*'

error.
You are declaring an array with a variable as sine on line 56, that's not valid C++
And you are using the array wrongly almost everywhere it appears
maybe you should read the tutorial: http://www.cplusplus.com/doc/tutorial/arrays/
I've read the tutorial, and I still don't understand why I can't declare an array with a variable.
Because the array size must be a constant. And if you try to use a pointer that's even worse. Arrays are evil. If you want to use an expandable/resizable/whatever array-like thing, use std::vector. #include <vector>
I guess using a vector would work as long as I can set the size to a value stored in a variable.
MyVector.resize(NewSize); ;)
Last edited on
This topic intrigues me. Two quick questions. I'm not trying to contradict what L B is saying, and I certainly agree with him that arrays are bad. But if its not possible to declare a variable sized array then why will the following compile?
1
2
3
4
5
6
7
8
9
int main()
{
    for (int i = 0; i < 10; ++i)
        {
            int x[i];
        }

}

I guess this isn't a variably sized array in that you can't modify its size once its been declared, but I've always wondered why people tell me not to do this. I use this all the time.


My second question is WTF??? How in the heck did you manage to declare functions inside main()?
why will the following compile?


It won't in standards compliant compilers.

Some compilers allow it as an extension, but it's not legal C++. If you do it, you run the risk of your code not compiling for other people.
It's legal in C99 but not in C++
Pages: 12