作者Waterpig (waterpig)
看板C_and_CPP
標題[問題] 請問哪裡出錯了...
時間Fri Aug 21 08:13:13 2009
下面這段程式碼
哪裡有問題阿...
我對照書打...
#include<stdio.h>
#include<stdlib.h>
int main (void)
{
char pass[4];
int i=0;
do {
printf("Input password :");
for (i=0;i<=3;i++)
{
pass[i]=getch(); <====卡在這行
printf("*");
}
if (!((pass[0]=='1') && (pass[1]=='2')
&& (pass[2]=='3') && (pass[3]=='4')))
printf(" Wrong password!");
printf("\n");
}
while (!((pass[0]=='1') && (pass[1]=='2')
&& (pass[2]=='3') && (pass[3]=='4')));
printf("You input the right password! \n");
system("pause");
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.160.42.223
1F:→ abcabcabc:getchar() 08/21 08:21
2F:→ Waterpig:感謝...看來是書錯了...我一直在找我哪裡跟他不一樣...囧 08/21 08:26
可是...照書上寫的執行效果是
Input password :**** Wrong password! (輸入非1234時)
Input password :**** (輸入1234時)
You input the right password!
可是我的是
Input password :2345
**** Wrong password!
Input password :*
在第一次輸入錯誤後會變成這個樣子...
※ 編輯: Waterpig 來自: 118.160.42.223 (08/21 08:39)
3F:推 suhorng:前面加一行 #include <conio.h> 08/21 08:56
4F:→ Waterpig:感謝~沒問題了! 不錯這個標頭擋書還沒提到=_=... 08/21 08:58
5F:→ suhorng:只能 google 囉XD 08/21 08:59
6F:→ suhorng:可能是舊版的 stdio.h 有引入這個函式庫:) 08/21 08:59
7F:→ Waterpig:了解~感謝解答~ 08/21 09:07
8F:推 VictorTom:應該是書上或原po漏了, 就算是以前TC2.0, getch()也沒有 08/21 09:13
9F:→ VictorTom:在stdio.h, 都要include conio.h才有得用的我記得@_@" 08/21 09:14
10F:→ Waterpig:如果這樣那就是書漏了...害我看這段程式碼看到頭快燒起來 08/21 09:15
11F:→ VictorTom:這樣你compile到那行應該就error了, 你應該先看看error 08/21 09:57
12F:→ VictorTom:訊息告訴你什麼, 應該會是getch() undefined之類的, 再 08/21 09:58
13F:→ VictorTom:google找看看; 如果error看不懂, 至少應該把訊息一起貼 08/21 09:59
14F:→ VictorTom:出來; 雖然這例子很短, 知道的很容易找到就是:) 08/21 09:59