我在学校使用Rock Paper Scissors游戏时遇到了麻烦,我不断收到错误,我不知道如何处理它们。到目前为止,这是我的代码。
#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;
}
}
}
我不断收到错误,例如
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);
任何帮助都会很棒!
答案 0 :(得分:2)
在C和C ++中,函数声明如下:
void getPlayerTurn(int selection) {
// Some code
}
第一部分指定'返回类型'(在本例中为void
,表示它不返回任何内容)。下一部分是函数名称,后跟参数列表。
在定义中,您必须指明参数的类型,因此在上面的代码段中int selection
表示参数selection
是int
- 一个整数。
要调用该函数,您不需要声明参数类型,因为编译器已经知道它是什么。所以就像这样打电话
getPlayerTurn(someVariable);