Kardan Kardan - 1 month ago 24
C++ Question

Rock Paper Scissors Errors

I am having trouble with a Rock Paper Scissors game for school and I keep getting errors and I have no idea what to to do with them. Here is my code thus far.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cctype>
#include <iostream>
using namespace std;

void playGame(int& pscore, int& cscore);
void getPlayerTurn (char selection);
int doComputerTurn (int comp);
void showOutcome (int result, int pturn, int cturn);

int main() {
int pscore = 0;
int cscore = 0;
char answer;
srand(time(0));
cout << "*** Welcome to RPS ***" << endl;
cout << endl;
do {
playGame(pscore, cscore);
cout << "Play again? ";
cin >> answer;
} while(tolower(answer) == 'y');
cout << "*** Thanks for playing ***" << endl;
}


void playGame(int& pscore, int& cscore) {

int pturn = getPlayerTurn(int selection);
int cturn = doComputerTurn(int comp);
showOutcome(pscore, cscore, pturn);
cout << "Player : " << pscore << endl;
cout << "Computer : " << cscore << endl;
}

void getPlayerTurn(char selection) {
cout << "Select (R)ock, (P)aper or (S)cissors: ";
cin >> char selection;
tolower(selection);
char r = 1;
char p = 2;
char s = 3;
return;
}

int doComputerTurn(int comp) {
comp = rand() % 3 +1;
if (comp == 1){
cout << "Computer selects Rock." << endl;
return(1);}
else if (comp == 2){
cout << "Computer selects Paper." << endl;
return(2);}
else{
cout << "Computer selects Scissors." << endl;
return(3);}
}

void showOutcome (int& pscore, int& cscore, int comp, int selection) {
if(selection==1){
if(comp==1){
cout << "Tie!" << endl;
}
else if(comp==2){
cout << "Paper beats Rock. Computer Wins!" << endl;
cscore =+ 1;
}
else {
cout << "Rock beats Scissors. Player Wins!" << endl;
pscore =+ 1;
}
}
if(selection==2){
if(comp==1){
cout << "Paper beats Rock. Player Wins!" << endl;
pscore =+ 1;
}
else if(comp==2){
cout << "Tie!" << endl;
}
else {
cout << "Scissors beats Paper. Computer Wins!" << endl;
cscore =+ 1;
}
}
if(selection==3){
if(comp==1){
cout << "Rock beats Paper. Computer Wins!" << endl;
cscore =+ 1;
}
else if(comp==2){
cout << "Scissors beats Paper. Player Wins!" << endl;
pscore =+ 1;
}
else {
cout << "Tie!" << endl;
}
}
}


I keep getting errors such as

rps.cpp: In function 'void playGame(int&, int&)':
rps.cpp:33:28: error: expected primary-expression before 'int'
int pturn = getPlayerTurn(int selection);
^
rps.cpp:34:29: error: expected primary-expression before 'int'
int cturn = doComputerTurn(int comp);


Any help at all would be amazing!

Answer

In C and C++, functions are declared like this:

void getPlayerTurn(int selection) {
    // Some code
}

The first part specifies the 'return type' (In this case void, indicating it returns nothing). The next part is the function name, followed by a list of parameters.

In a definition you have to indicate what type the parameter is, so in the code snippet above int selection indicates that the parameter selection is an int - an integer.

To call the function you don't need to declare the parameter type, because the compiler already knows what it is. So just call like this

getPlayerTurn(someVariable);