我有下面的代碼:如何在重新啓動時停止菜單的閃爍不正常?
#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'語句。 –