我正在嘗試編寫一個簡單的演示程序,在輸入指定字符時會跳出一個循環。我能夠成功地做到這一點,直到我想添加額外的條件,我沒有超過我的數組。當我輸入指定的字符時,這段代碼不會跳出循環,我不知道爲什麼。代碼的邏輯和設置看起來很合理!如何通過終止字符打破循環?
#include <stdio.h>
char input[10];
char breakout = '!';
int idx;
int main(void)
{
printf("Input characters until %c character is used to break out", breakout);
for(idx = 0; input[idx] != breakout && idx < 10; idx++)
{
scanf("%c", &input[idx]);
}
printf("Program terminated:");
getchar();
return 0;
}
看來好像for循環的條件應該在進入'!'後評估錯誤。性格,但事實並非如此。謝謝你們
初始化'input'或由於第一循環測試 –