I'm making a proof of concept program for what will basically be a base model for shops in a game I plan to make. I ran into an issue when I attempted to pass an array that holds the quantity of items the player own. However, doing so caused an error that states: "expected primary-expression before ']' token". This is found in lines 16 and 17. These lines are where I call the functions in the main function. Upon attempting to research this issue, I would only get solutions for instances of this error when there are simple syntax errors(i.e. a semi colon misplaced after a '}').
#include <iostream>
#include <string>
#include <cstdlib> //This library is needed to prevent a compiler error with Code Blocks when system("PAUSE"); is used.
usingnamespace std;
void shopPrototype_stuff();
void shopPrototype_pinv(int pInv[]);
void shopPrototype_buy(int pInv[]);
bool shopPrototype_return(bool isPlaying);
int main(){
int pInv[3] = { }; //Player inventory is stored in an array. Will be changed to list duplicates as a "stack"
bool isPlaying; //variables that checks if the player wants to continue shopping
while(isPlaying == true){ //code is run continuously until the player specifies not to.
shopPrototype_stuff();
shopPrototype_buy(pInv[]); //error caused by a supposed need for a primary expression
shopPrototype_pinv(pInv[]); //same error as the previous line
shopPrototype_return(isPlaying);} //function to determine whether or not to continue the loop
system("pause"); //prevents the program from terminating early
return 0;}
void shopPrototype_stuff(){ //this prints the items in the shop
string item[3] = {"A rock...just a rock.", "OP Gun of Noobness", "Bass Cannon"};
cout<<"Welcome to the shop!\nHere is what we have in stock:"<<endl;
for(int i = 0; i < 3; i++){
cout<<i +1<<") "<<item[i]<<endl;}
cout<<endl;}
void shopPrototype_pinv(int pInv[]){
cout<<"===== Your Inventory ====="<<endl<<endl;
for(int j = 0; j < 3; j++){ //function prints the values in the array as the player's inventory
cout<<j + 1<<") "<<pInv[j]<<endl;}
}
void shopPrototype_buy(int pInv[]){ //select an item to buy by typing the number
int buy;
cin>> buy;
switch(buy){ //switch statement to determine purchase
case 1:
pInv[0] += 1;
break;
case 2:
pInv[1] += 1;
break;
case 3:
pInv[2]++; //incriment operator was used here to test if both methods actually work. I'm pretty sure they do.
break;
default: //impromtu check for invalid input
cout<<"Invalid input"<<endl;
break;}
}
bool shopPrototype_return(bool isPlaying){ /*asks the player if they want to play again and either continues the loop
or ends the program by exiting the loop.*/
string play; //string used instead of chars to hastily alieviate errors with comparison
cout<<"Would you like to keep shopping?\n Y/N"<<endl<<endl;
cin>> play;
if(play == "Y" || play == "y"){
isPlaying = true;
return isPlaying;}elseif(play == "N" || play == "n"){
isPlaying = false;
return isPlaying;}else{ //will eventually prompt the player with the question again
cout<<"Invalid input"<<endl;}
}
This resolved the issue. Thanks for the advice, I greatly appreciate it. I was aware of the possibility of using a constant instead of merely typing the length in each one manually, but decided to revise that once the error was resolved.