有几个错误。不知道怎么改。

luolai 2008-03-16 10:08:06

#include <iostream>
using namespace std;
enum objectType{ Rock, Paper, Scissors};
void displayRules();
objectType retrievePlay(char selection);
bool validSelection(char selection);
void convertEnum(objectType object);
objectType winningObject(objectType play1, objectType play2);
void gameResult(objectType play1, objectType play2, int& winner);
void displayResults(int gCount, int wCount1, int wCount2);

int main()
{
int gameCount;
int winCount1;
int winCount2;
int gamewinner;
char response;
char selection1;
char selection2;
objectType play1;
objectType play2;
gameCount = 0;
winCount1 = 0;
winCount2 = 0;
displayRules();
cout<<"Enter Y/y to play the game: ";
cin>>response;
cout<<endl;

while (response == 'Y' // response == 'y')
{
cout<<"Player 1 enter your choice: ";
cin>>selection1;
cout<<endl;

cout<<"Player2 enter your choice: ";
cin>>selection2;
cout<<endl;

if (validSelection(selection1) && validSelection(selection2));
{
play1 = retrievePlay(selection1);
play2 = retrievePlay(selection2);
gameCount++;
gameResult(play1, play2, gamewinner);
if (gamewinner == 1)
winCount1++;
else
if (gamewinner == 2)
winCount2++;
}
cout<<"Enter Y/y to play the game:";
cin>>response;
cout<<endl;
}
displayResults(gameCount, winCount1, winCount2);
return 0;
}
void displayRules()
{
cout<<" Welcome to the game of Rock, Paper, and Scissors."<<endl;
cout<<" This is agame for two players.For each game, each"<<endl;
cout<<" player selects one of the objiects, Rock, Paper, or"
<<" Scissors."<<endl;

cout<<" The rules for winning the game are:"<<endl;
cout<<"1. If both players select the same object, it is a"
<<" tie."<<endl;
cout<<"2. Rock Breaks Scissors: So the player who selects Rock"
<<" wins."<<endl;
cout<<"3. Paper covers Rock: So the player who selects Scissors"
<<" ins."<<endl;
cout<<"4. Scissors cut Paper: So the player who selects Scissors"
<<" wins."<<endl<<endl;
cout<<"Enter R or r to select Rock, P or p to select Paper, and "
<<" S or s to select Scissors."<<endl;
}

bool validSelection(char selection)
{
switch (selection)
{
case 'R': case 'r':
case 'S': case 's':
case 'P': case 'p': return true;
default: return false;
}
}
objectType retrievePlay(char selection)
{
objectType object;
switch (selection)
{
case 'R': case 'r': object = Rock;
case 'P': case 'p': object = Paper;
case 'S': case 's': object = Scissors;
}
return object;
}
void converEnum(objectType object)
{
switch (object)
{
case Rock: cout<<"Rock";
break;
case Paper: cout<<"Paper";
break;
case Scissors: cout<<"Scissors";
}
}
objectType winningObject(objectType play1, objectType play2)
{
if ((play1 == Rock && play2 == Scissors) // (play2 == Rock && play1 == Scissors))
return Rock;
else
if ((play1 == Rock && play2 == Paper)
// (play2 == Rock && play1 == Paper))
return Paper;
else
return Scissors;
}
void gameResult(objectType play1, objectType play2, int& winner)
{
objectType winnerObject;

if(play1 == play2)
{
winner = 0;
cout<<"Both players selected";
converEnum(play1);
cout<<". This game is a tie."<<endl;
}
else
{
winnerObject = winningObject(play1, play2);

cout<<"Player 1 selected ";
convertEnum(play1);
cout<<" and player 2 selected ";
convertEnum(play2);
cout<<". ";
if (play1 == winnerObject)
winner = 2;
cout<<"Player "<<winner<<" wins this game."<<endl;
}
}
void displayResults(int gCount, int wCount1, int wCount2)
{
cout<<"The total number of plays: "<<gCount<<endl;
cout<<"The number of plays won by player 1:"<<wCount1<<endl;
cout<<"The number of plays won by player 2:"<<wCount2<<endl;
}
这是个简单游戏。有几个地方不知道怎么改。
...全文
56 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
luolai 2008-03-16
  • 打赏
  • 举报
回复
g++.exe -x c++ -c C:\DOCUME~1\LUOLAI~1.LUO\MYDOCU~1\262.cpp -o C:\DOCUME~1\LUOLAI~1.LUO\MYDOCU~1\262.o -Wall -fpermissive -Wno-sign-compare -g
C:\DOCUME~1\LUOLAI~1.LUO\MYDOCU~1\262.cpp: In function `int main()':
C:\DOCUME~1\LUOLAI~1.LUO\MYDOCU~1\262.cpp:32: error: expected `)' before '{' token
C:\DOCUME~1\LUOLAI~1.LUO\MYDOCU~1\262.cpp: In function `objectType winningObject(objectType, objectType)':
C:\DOCUME~1\LUOLAI~1.LUO\MYDOCU~1\262.cpp:115: error: expected `)' before "return"
C:\DOCUME~1\LUOLAI~1.LUO\MYDOCU~1\262.cpp:119: error: expected `)' before "return"
Failure
一样的代码。。。。。我很郁闷。。。。
baihacker 2008-03-16
  • 打赏
  • 举报
回复

编译器: Default compiler
执行 g++.exe...
执行结束
成功编译
#include <iostream>
using namespace std;
enum objectType{ Rock, Paper, Scissors};
void displayRules();
objectType retrievePlay(char selection);
bool validSelection(char selection);
void convertEnum(objectType object);
objectType winningObject(objectType play1, objectType play2);
void gameResult(objectType play1, objectType play2, int& winner);
void displayResults(int gCount, int wCount1, int wCount2);

int main()
{
int gameCount;
int winCount1;
int winCount2;
int gamewinner;
char response;
char selection1;
char selection2;
objectType play1;
objectType play2;
gameCount = 0;
winCount1 = 0;
winCount2 = 0;
displayRules();
cout <<"Enter Y/y to play the game: ";
cin>> response;
cout <<endl;

while (response == 'Y') // response == 'y')
{
cout <<"Player 1 enter your choice: ";
cin>> selection1;
cout <<endl;

cout <<"Player2 enter your choice: ";
cin>> selection2;
cout <<endl;

if (validSelection(selection1) && validSelection(selection2))//;
{
play1 = retrievePlay(selection1);
play2 = retrievePlay(selection2);
gameCount++;
gameResult(play1, play2, gamewinner);
if (gamewinner == 1)
winCount1++;
else
if (gamewinner == 2)
winCount2++;
}
cout <<"Enter Y/y to play the game:";
cin>> response;
cout <<endl;
}
displayResults(gameCount, winCount1, winCount2);
return 0;
}
void displayRules()
{
cout <<" Welcome to the game of Rock, Paper, and Scissors." <<endl;
cout <<" This is agame for two players.For each game, each" <<endl;
cout <<" player selects one of the objiects, Rock, Paper, or"
<<" Scissors." <<endl;

cout <<" The rules for winning the game are:" <<endl;
cout <<"1. If both players select the same object, it is a"
<<" tie." <<endl;
cout <<"2. Rock Breaks Scissors: So the player who selects Rock"
<<" wins." <<endl;
cout <<"3. Paper covers Rock: So the player who selects Scissors"
<<" ins." <<endl;
cout <<"4. Scissors cut Paper: So the player who selects Scissors"
<<" wins." <<endl <<endl;
cout <<"Enter R or r to select Rock, P or p to select Paper, and "
<<" S or s to select Scissors." <<endl;
}

bool validSelection(char selection)
{
switch (selection)
{
case 'R': case 'r':
case 'S': case 's':
case 'P': case 'p': return true;
default: return false;
}
}
objectType retrievePlay(char selection)
{
objectType object;
switch (selection)
{
case 'R': case 'r': object = Rock;
case 'P': case 'p': object = Paper;
case 'S': case 's': object = Scissors;
}
return object;
}
void convertEnum(objectType object)
{
switch (object)
{
case Rock: cout <<"Rock";
break;
case Paper: cout <<"Paper";
break;
case Scissors: cout <<"Scissors";
}
}
objectType winningObject(objectType play1, objectType play2)
{
if (play1 == Rock && play2 == Scissors) // (play2 == Rock && play1 == Scissors))
return Rock;
else
if (play1 == Rock && play2 == Paper)
// (play2 == Rock && play1 == Paper))
return Paper;
else
return Scissors;
}
void gameResult(objectType play1, objectType play2, int& winner)
{
objectType winnerObject;

if(play1 == play2)
{
winner = 0;
cout <<"Both players selected";
convertEnum(play1);
cout <<". This game is a tie." <<endl;
}
else
{
winnerObject = winningObject(play1, play2);

cout <<"Player 1 selected ";
convertEnum(play1);
cout <<" and player 2 selected ";
convertEnum(play2);
cout <<". ";
if (play1 == winnerObject)
winner = 2;
cout <<"Player " <<winner <<" wins this game." <<endl;
}
}
void displayResults(int gCount, int wCount1, int wCount2)
{
cout <<"The total number of plays: " <<gCount <<endl;
cout <<"The number of plays won by player 1:" <<wCount1 <<endl;
cout <<"The number of plays won by player 2:" <<wCount2 <<endl;
}



luolai 2008-03-16
  • 打赏
  • 举报
回复
我用GCC 不行啊。。
C:\DOCUME~1\LUOLAI~1.LUO\MYDOCU~1\262.cpp: In function `int main()':
C:\DOCUME~1\LUOLAI~1.LUO\MYDOCU~1\262.cpp:32: error: expected `)' before '{' token
C:\DOCUME~1\LUOLAI~1.LUO\MYDOCU~1\262.cpp: In function `objectType winningObject(objectType, objectType)':
C:\DOCUME~1\LUOLAI~1.LUO\MYDOCU~1\262.cpp:115: error: expected `)' before "return"
C:\DOCUME~1\LUOLAI~1.LUO\MYDOCU~1\262.cpp:119: error: expected `)' before "return"
Failure
baihacker 2008-03-16
  • 打赏
  • 举报
回复

//编译已通过
--------------------Configuration: test2 - Win32 Debug--------------------
Compiling...
main.cpp

main.obj - 0 error(s), 0 warning(s)

#include <iostream>
using namespace std;
enum objectType{ Rock, Paper, Scissors};
void displayRules();
objectType retrievePlay(char selection);
bool validSelection(char selection);
void convertEnum(objectType object);
objectType winningObject(objectType play1, objectType play2);
void gameResult(objectType play1, objectType play2, int& winner);
void displayResults(int gCount, int wCount1, int wCount2);

int main()
{
int gameCount;
int winCount1;
int winCount2;
int gamewinner;
char response;
char selection1;
char selection2;
objectType play1;
objectType play2;
gameCount = 0;
winCount1 = 0;
winCount2 = 0;
displayRules();
cout <<"Enter Y/y to play the game: ";
cin>> response;
cout <<endl;

while (response == 'Y') // response == 'y')
{
cout <<"Player 1 enter your choice: ";
cin>> selection1;
cout <<endl;

cout <<"Player2 enter your choice: ";
cin>> selection2;
cout <<endl;

if (validSelection(selection1) && validSelection(selection2))//;
{
play1 = retrievePlay(selection1);
play2 = retrievePlay(selection2);
gameCount++;
gameResult(play1, play2, gamewinner);
if (gamewinner == 1)
winCount1++;
else
if (gamewinner == 2)
winCount2++;
}
cout <<"Enter Y/y to play the game:";
cin>> response;
cout <<endl;
}
displayResults(gameCount, winCount1, winCount2);
return 0;
}
void displayRules()
{
cout <<" Welcome to the game of Rock, Paper, and Scissors." <<endl;
cout <<" This is agame for two players.For each game, each" <<endl;
cout <<" player selects one of the objiects, Rock, Paper, or"
<<" Scissors." <<endl;

cout <<" The rules for winning the game are:" <<endl;
cout <<"1. If both players select the same object, it is a"
<<" tie." <<endl;
cout <<"2. Rock Breaks Scissors: So the player who selects Rock"
<<" wins." <<endl;
cout <<"3. Paper covers Rock: So the player who selects Scissors"
<<" ins." <<endl;
cout <<"4. Scissors cut Paper: So the player who selects Scissors"
<<" wins." <<endl <<endl;
cout <<"Enter R or r to select Rock, P or p to select Paper, and "
<<" S or s to select Scissors." <<endl;
}

bool validSelection(char selection)
{
switch (selection)
{
case 'R': case 'r':
case 'S': case 's':
case 'P': case 'p': return true;
default: return false;
}
}
objectType retrievePlay(char selection)
{
objectType object;
switch (selection)
{
case 'R': case 'r': object = Rock;
case 'P': case 'p': object = Paper;
case 'S': case 's': object = Scissors;
}
return object;
}
void converEnum(objectType object)
{
switch (object)
{
case Rock: cout <<"Rock";
break;
case Paper: cout <<"Paper";
break;
case Scissors: cout <<"Scissors";
}
}
objectType winningObject(objectType play1, objectType play2)
{
if (play1 == Rock && play2 == Scissors) // (play2 == Rock && play1 == Scissors))
return Rock;
else
if (play1 == Rock && play2 == Paper)
// (play2 == Rock && play1 == Paper))
return Paper;
else
return Scissors;
}
void gameResult(objectType play1, objectType play2, int& winner)
{
objectType winnerObject;

if(play1 == play2)
{
winner = 0;
cout <<"Both players selected";
converEnum(play1);
cout <<". This game is a tie." <<endl;
}
else
{
winnerObject = winningObject(play1, play2);

cout <<"Player 1 selected ";
convertEnum(play1);
cout <<" and player 2 selected ";
convertEnum(play2);
cout <<". ";
if (play1 == winnerObject)
winner = 2;
cout <<"Player " <<winner <<" wins this game." <<endl;
}
}
void displayResults(int gCount, int wCount1, int wCount2)
{
cout <<"The total number of plays: " <<gCount <<endl;
cout <<"The number of plays won by player 1:" <<wCount1 <<endl;
cout <<"The number of plays won by player 2:" <<wCount2 <<endl;
}

64,653

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧