Trending News
c++程式用法
請問一下如果我要做一個密碼程式用case來做
請問為什麼假設我輸入了open the door
則不會顯示下列解密成功
有什麼方法能打密碼就顯示解密成功呢?
switch(a)
case 'open the door':
printf("解密成功")
2 Answers
- ?Lv 51 decade agoFavorite Answer
switch ( ) 內要是個整數值。
你的例子,a 要是個整數值
a 不能是個 'open the door' 或 "open the door"
不然,你要來個
enum 或 #define open_the_door
switch (a)
{ case open_the_door: ___; break;
}
就可以了
2007-12-06 08:48:27 補充:
ㄚ旺 是對的。
我只是懶得寫那部份。
不過,ㄚ旺 應該還是對的:
如果你的問題是連 ' 和 " 都分不清,那部份我是不應該偷懶的。
你也可以像下面那樣寫。
它適合更多的字串;
但,密碼不會是一堆字串就是了!應該是唯一的字串。
char *pass[] = {"open the door", "close the door", "paint the door", "destroy the door"};
enum { open, close, paint, destroy }; // 程式師習慣這裡用大寫就是了
int N = sizeof(pass) / sizeof(*pass);
2007-12-06 08:48:38 補充:
...
gets(key);
...
for (a=0; i
2007-12-06 08:48:55 補充:
...
gets(key);
...
for (a=0; i<N; i++)
{ if (!strcmp(pass, key)) break;
}
switch (a)
{ case open: ... break;
case close: ... break;
case paint: ... break;
case destroy: ... break;
default: printf("沒這密碼");
}
看不懂再問吧!
2007-12-06 08:51:57 補充:
再想想,ㄚ旺還是對的!(老公,別吃醋耶!)
應該要把完整的程式寫出來,版大可能才會懂!
不過,版大,
您就努力一下,把我的和ㄚ旺的綜合一下,把它弄出來,
您的功力一定會進步的!
- ㄚ旺Lv 51 decade ago
//字串要用比對的
#include < stdlib.h >
#include < string.h >
int main()
{
char *s="open the door";
char p[50];
printf("輸入密碼:");
gets(p);
if (!strcmp(s,p))
puts("解密成功\n");
else
puts("解密不成功\n");
return(system("pause>nul|echo 按 [Any key] 繼續..."));
}