我遇到了這個問題:運行時檢查失敗#2 - 變量「檢查」周圍的堆棧在Visual Studio 12中損壞。我也在代碼塊中嘗試這個,但面臨同樣的問題。我也在ideone.com上運行我的代碼,它顯示運行時錯誤。它的工作原理用於Y,但亙古不變的作品對於N運行時檢查失敗#2 - 圍繞變量「檢查」的堆棧已損壞
INT主要(){
int led=0;
int ohm=0;
char check;
int flag=0;
while (led < 1 || led > 3){
printf("Enter the number of switch you want to close: \n\n");
printf(" ******************** Press 1 for switch (LED) 1 ********************\n");
printf(" ******************** Press 2 for switch (LED) 2 ********************\n");
printf(" ******************** Press 3 for switch (LED) 3 ********************\n");
printf("Switch: ");
scanf("%d", &led);
}
printf("\n\n");
while (ohm < 1 || ohm > 3){
printf("Enter the resistance of Rheostat: \n\n");
printf(" ******************** Press 1 for 10 ohm resistance ********************\n");
printf(" ******************** Press 2 for 20 ohm resistance ********************\n");
printf(" ******************** Press 3 for 30 ohm resistance ********************\n");
printf("Resistance: ");
scanf("%d", &ohm);
}
while (flag == 0)
{
//LED-1
if(led== 1 && ohm== 1)
{
printf("LED-1 is blinking 2 times\n");
}
if(led== 1 && ohm== 2)
{
printf("LED-1 is blinking 4 times\n");
}
if(led== 1 && ohm== 3)
{
printf("LED-1 is blinking 6 times\n");
}
//LED-2
if(led== 2 && ohm== 1)
{
printf("LED-2 is blinking 2 times\n");
}
if(led== 2 && ohm== 2)
{
printf("LED-2 is blinking 4 times\n");
}
if(led == 2 && ohm == 3)
{
printf("LED-2 is blinking 6 times\n");
}
//LED-3
if(led == 3 && ohm == 1)
{
printf("LED-3 is blinking 2 times\n");
}
if(led == 3 && ohm == 2)
{
printf("LED-3 is blinking 4 times\n");
}
if(led == 3 && ohm == 3)
{
printf("LED-3 is blinking 6 times\n");
}
printf("Do you want to continue Yes (Y) or No (N): ");
scanf("%s", &check);
if(check =='Y' || check =='y')
{
led = 0;
ohm = 0;
while (led < 1 || led > 3){
printf("Enter the number of switch you want to close on: ");
scanf("%d", &led);
}
while (ohm < 1 || ohm > 3){
printf("Enter the resistance of Rheostat: ");
scanf("%d", &ohm);
}
}
if(check=='N' || check=='n')
{
printf("Thanks for using the program");
flag = 1;
}
}
return 0;
}
'scanf(「%s」,&check);'爲什麼你要將字符串加載到'char'中? [看看什麼是'什麼'和'c'是](http://msdn.microsoft.com/en-us/library/6ttkkkhh(v = vs.80).aspx)。 –
請不要打擾使用'scanf'。 http://www.c-faq.com/stdio/scanfprobs.html – jamesdlin