我有下面的代码:如何在重新启动时停止菜单的闪烁不正常?
#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;
}
这应该是打印菜单,让你选择你的选择来浏览不同的菜单,用于重复的过程中,而当插入字符不是有效,但是当这种情况发生时,控制台开始闪烁,程序崩溃。这是如何造成的,我该如何解决? 谢谢
当您在英文网站上发布代码时,请翻译代码中的字符串,这会让人们更容易理解和阅读。 –
我发现你的错误。如果你在第一个'scanf'语句中输入一个字符,代码就会不停地运行,并且会自行破坏。 @SelmanGenç –
这不是一个教程网站,但你必须学习和理解指针。发布的代码不会为字符串'trdENome','trdENome1'和'trdENome2'分配内存。这些应该可能是数组,也应该在'main()'的范围内声明。地址运算符“&”使用不正确; switch语句应该使用'break'语句。 –