我首先想感謝您檢視我的問題。在Visual Studio 2013中,「運行時檢查失敗#2 - 圍繞變量'b'的堆棧已損壞。」
自從我開始使用C語言編寫代碼以來,我一直在使用代碼塊進行編碼,最近我不得不切換到使用Visual Studio進行大學實驗室作業。本週我們做了一件非常簡單的任務,但我似乎一直在遇到這個錯誤,這個錯誤只會在使用Visual Studio時彈出,而不會在任何其他IDE中彈出。我想知道如果有人能幫我解決這個問題,並告訴我我做錯了什麼?我將在下面附上我的代碼。
太謝謝你了!
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
void main()
{
srand(time(NULL));
int a, n;
char b ='y';
while (b == 'y')
{
n = rand() % 3000 + 1; // 1-3000
puts("I have generated a number between 1 and 3000. Can you guess mynumber?\nPlease type your first guess (0-3000):");
scanf("%d", &a);
while (a != n)
{
if (a > n)
printf("Too high. Please try again.\n");
if (a < n)
printf("Too low. Please try again.\n");
scanf("%d", &a);
}
printf("Excellent! You guessed the number!\n");
printf("Would you like to play again? (y or n)\n");
scanf("%s", &b);
}
printf("Have a nice day.\n");
system("PAUSE");
}
此代碼旨在生成1-3000的隨機數(ish),並讓用戶猜測它。用戶然後可以選擇再次播放或不播放。當用戶鍵入'n'結束while while循環時發生錯誤。再次
謝謝!
說真的......即使你懶得搜索,當你將問題標題輸入框中時,肯定會彈出幾十個答案。 http://stackoverflow.com/search?q=%22Run-Time+Check+Failure+%232+-+Stack+around+the+variable%22+scanf –
可能的重複[Run-Time Check Failure#2 - Stack圍繞變量'char'被破壞](http://stackoverflow.com/questions/27935712/run-time-check-failure-2-stack-around-the-variable-char-was-corrupted) –