[問題] switch加上for迴圈之後,印出錯誤結果
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
dev c++
問題(Question):
各位板上先進好
小弟想問一個簡單的問題
單純只用switch的時候 結果正確
但是當我加上for迴圈之後
會多印出default裡面的結果
預期的正確結果(Expected Output):
在沒有for迴圈的情況下
當我給a時,很正確的印出a
錯誤結果(Wrong Output):
但是加上for迴圈之後
當我給a時
卻跑出default裡面的結果,如下:
a
e
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char input;
int i;
for(i=1;i<=10;i++)
{
scanf("%c",&input);
switch(input)
{
case'a':
printf("a\n",i);
break;
case'b':
printf("b\n",i);
break;
case'c':
printf("c\n",i);
break;
case'd':
printf("d\n",i);
break;
default:
printf("e\n",i);
break;
}
}
system("PAUSE");
return 0;
}
補充說明(Supplement):
我有試著把printf順便加上i的結果
發現後來印出default的結果是第2次迴圈的
例如:
給a時... 印出:
a i=1
e i=2
不知道問題出在哪...
懇請前輩們指點迷津~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.46.159.166
推
08/14 12:46, , 1F
08/14 12:46, 1F
→
08/14 12:47, , 2F
08/14 12:47, 2F
→
08/14 12:47, , 3F
08/14 12:47, 3F
→
08/14 13:13, , 4F
08/14 13:13, 4F
→
08/14 13:13, , 5F
08/14 13:13, 5F
→
08/14 13:13, , 6F
08/14 13:13, 6F
→
08/14 14:03, , 7F
08/14 14:03, 7F
推
08/14 15:38, , 8F
08/14 15:38, 8F
→
08/14 15:41, , 9F
08/14 15:41, 9F
推
08/06 12:22, , 10F
08/06 12:22, 10F