【发布时间】:2017-04-14 00:01:46
【问题描述】:
我有以下代码:
#include <stdio.h>
#include <stdlib.h>
int main (){
int s1 = 0;
int s2 = 0;
int frstEscolha;
do{
printf("\n\n WELCOME \n\n");
printf(" 1- JOGAR \n");
printf(" 2- SAIR \n");
scanf ("%d", &frstEscolha);
printf ("%d %d",s1,s2);
switch (frstEscolha) {
system ("cls");
int sndEscolha;
case (1):
s1 = 1;
printf ("\n\n NUMBER OF PLAYERS \n\n");
printf ("1- ONE PLAYER \n");
printf ("2- TWO PLAYERS \n");
scanf ("%d", & sndEscolha);
system ("cls");
do{
switch (sndEscolha) {
char *trdENome , *trdENome1, *trdENome2;
case (1):
s2 = 1;
printf ("\nPLAYER NAME: \n");
scanf ("%s", &trdENome);
printf("\nGOOD GAME %s \n\n", &trdENome);
case (2):
s2 = 1;
printf ("\nPLAYER 1 NAME \n");
scanf ("%s", &trdENome1);
printf ("\nPLAYER 2 NAME \n");
scanf ("%s", &trdENome2);
printf("\nGOOD GAME %s e %s \n\n", &trdENome1, &trdENome2);
default :
printf ("Invalid character, try again!!");
s2 = 0;
}
}
while (s2==0);
case (2): exit (0);
default :
printf ("Invalid character, try again!!");
s1 = 0;
}
}
while (s1 == 0);
return 0;
}
这应该打印一个菜单并让您选择导航不同菜单的选项,while 用于在插入的字符无效时重复该过程,但是当这种情况发生时控制台开始闪烁并且程序崩溃.这是如何引起的,我该如何解决? 谢谢
【问题讨论】:
-
当您在英文网站上发布代码时,请翻译代码中的字符串,这样人们更容易理解和阅读。
-
我发现了你的错误。如果您在第一个
scanf语句中输入一个字符,则代码会不停地运行并且会自行中断。 @SelmanGenç -
这不是一个教程网站,但你必须学习和理解指针。发布的代码不会为字符串
trdENome、trdENome1和trdENome2分配内存。这些应该是数组,也应该在main()的范围内声明。地址运算符&使用错误;break语句应在switch语句中使用。