【发布时间】:2014-07-21 10:08:05
【问题描述】:
我还在和我叔叔一起学习 C,我在尝试编译我们一起编写的玩石头剪刀布的程序时遇到了另一个问题。我在程序上还有更多工作要做,但我什至无法让大部分事情正常工作。
我使用 Make 命令从编译器收到的错误
main.c: in function 'start_play':
main.c:79:7: error: format '%s' expects argument type 'char *', but argument 2 has type 'int' [-werror=format=]
scanf("%1s", human_hand[0]);
main.c:82:11: error: format '%s' expects argument of type 'char *', but argument 3 has type 'int' [-Werror=format=]
fprintf( stderr, "Player 1 has entered an invalid hand choice, %s.", player_1);
main.c:104:1: error: ISO C forbids nested functions [-Werror=pedantic]
int choice_to_int(const char a)
main.c:104:1: error: ISO C90 forbids mixed declarations and code [-Werror=pedantic]
main.c 132:1: error: expected declaration or statement at end of input
}
main.c:132:1: error: expected declaration or statement at end of input
main.c:60:6: error: unused variable 'choice' [-Werror=unused-variable]
enum choices choice;
这是 main.c 文件的内容,我似乎无法正确理解。
/*
Programmer: Tim Bowen
Creation Date: 2014/05/26
Last Updated: 2014/05/27
Project Name: RPS(Rock/Paper/Scissors)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int gen_hand( );
void start_play();
int choice_to_int(const char a);
int main ( int argc, char *arga[], char **env)
{
start_play( );
return 0;
} /*end main*/
/*
Precondition: Srand must but initialized before usage of gen_hand
Post condition: function returns either 0, 1 or 2.
*/
int gen_hand( ) {
return((rand() % 3 ));
}
void start_play( ) {
enum choices { ROCK, PAPER, SCISSORS };
enum choices choice;
const char *rps[] = {"Rock", "Paper", "Scissors"};
int player_1, player_2;
char answer[2];
char human_hand[2];
srand(time(NULL));
printf("%s\n", "This program plays Rock, Paper, Scissors.");
printf("%s\n", "Version 0.2");
do{
printf("%s\n", "Do you want to play against the computer? (Y/N):");
printf("%s\n", "Any other character to quit.:");
scanf("%1s", answer);
printf("%s\n", answer);
break;
if(answer[0] == 'y' || answer[0] == 'Y')
{
printf( "%s\n", "Please enter your hand(R/P/S):");
scanf("%1s", human_hand[0]);
player_1=choice_to_int(human_hand[0]);
if(player_1==3){
fprintf( stderr, "Player 1 entered an invalid hand choice, %s", player_1 );
/* err_hand();
break;} */
player_2=gen_hand( );
printf( "Player one => %d\n %2s", player_1, rps[player_1]);
printf( "player two => %d\n %2s", player_2, rps[player_2]);
}
else if (answer[0] == 'n' || answer[0] == 'N' )
{
player_1=gen_hand( );
player_2=gen_hand( );
printf( "Player one => %d\n %2s", player_1, rps[player_1]);
printf( "player two => %d\n %2s", player_2, rps[player_2]);
}
else {
break;
}
while (1);
return;
}
/*
precondition: user passes in either, Rr, Pp, Ss, or another incorrect response.
post condition: function returns either 0, 1, or 2 for correct responses, or 3 for erroneous ones.
*/
int choice_to_int(const char a)
{
int r;
switch ( r ) {
case 'R':
case 'r':
r=0;
break;
case 'P':
case 'p':
r=1;
break;
case 'S':
case 's':
r=2;
break;
default:
r=3;
break;
}
return (r);
}
对于几乎过多的额外行,我深表歉意,但我这样做是为了(希望)更容易阅读。我不确定这次我做错了什么,因为我几乎完全从课堂上的笔记中复制了这一点。我无法继续尝试添加我应该添加的部分,因为我无法让我们一起编码的部分甚至正常工作。我叔叔编译的例子中没有出现这些错误。
编辑:到目前为止,给出的答案已经帮助清除了大部分涉及的错误,除了一个新错误:
main.c:58:5 错误:格式“%s”预期参数类型为“char ”,但参数 2 的类型为“char ()[2]”[-Werror=format ]
它仍然在抱怨我没有使用变量“选择”,但老实说,我不确定如何使用选择。 我可能不清楚这一点,但我没有直接“写”这个。我建议我叔叔写的时候,然后在“上课”结束后我在我的机器上重写它。我并没有真正声称完全理解我在这里所做的事情......该程序应该是显而易见的(对于具有丰富编码经验的人......)但我想我没有得到它。
它应该选择玩石头/纸/剪刀,并提供让计算机自己玩的选项(else if语句),我们使用枚举来指定选项,然后使用choice_to_int来获取用户输入并将其转换回与枚举匹配的值,并允许我根据这些比较创建“赢/输/画”语句。我可能建议了一些愚蠢的方法来实现这个想法,但这就是想法。我还没有达到可以创建赢/输/平局语句的地步,或者如果用户决定放入不适合游戏格式的东西(比如 Z 或其他东西),则可以处理错误。
感谢您迄今为止的帮助,感谢您在这方面的帮助,也许是石头/纸/剪刀的不必要的复杂实现......
【问题讨论】:
-
我最近看到的最好的标题。
-
查看了您的个人资料,说您是作家。只是好奇,如果您不打算在较低级别上编程,为什么是 C :)?
-
@Andro47 我认为这是一个三方面的答案,首先,大多数作家不以写作为生,至少不要开始,其次,我写的小说,我最终想分支进入游戏等特许经营工作,因此学习编写 C 和其他语言将有助于我与直接处理该方面的任何人合作的能力,第三部分是这并不完全是我的想法。我请叔叔教我编程,他认为这是最好的。到目前为止,我发现很难理解...
-
很遗憾我不能勾选多个答案,因为几个答案有助于清除最初的错误,但我还没有完全工作......