Re: [問題] C語言新手...如何中斷WHILE
想說的不長不短
請原諒我騙文章數
問問題的原po呢~
我建議您先去看看stdio函數的正確使用方式
包括他到底要輸入什麼最後又return什麼
http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
以下的資料都來自上面網站
int scanf ( const char * format, ... );
為什麼是return int?
不覺得很奇怪嗎?
我所知道要return char卻是會retun int 的時機有一個
就是要裝EOF這個整數
來判斷是否到了EOF
還有沒有其他的case我不知道~因為我也學不久
但是這裡的情形完完全全不一樣
他整個就不是拿來return你想要的東西
請看說明
Return Value
On success, the function returns the number of items succesfully read.
This count can match the expected number of readings or fewer,
even zero, if a matching failure happens.
In the case of an input failure before any data could be successfully read,
EOF is returned.
英文好繁瑣
小弟幫大家翻譯一下
他是說
如果你scanf的函數使用過程
沒有出錯的話
他return的是"成功讀取的項目數"!!!
所以原po的code
會一直return 1(也就是成功讀取grade這一個項目)
所以當然永遠不會等於Z囉
我也有實際測試過
的確是這樣
cout<< scanf("%c%c",&c,&d)<<endl;
的確就是2
雖然我猜原PO已經用別的寫法把他弄好了
但是就大家參考用吧
希望對大家有用
有錯也請大家指正
如果是我的話
我會while( scanf("%c",&c), c !='Z' )
最後送大家一個笑臉:)
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.4.234
※ 編輯: flax00298 來自: 140.112.4.234 (08/23 15:36)
推
08/23 19:33, , 1F
08/23 19:33, 1F
推
08/23 20:50, , 2F
08/23 20:50, 2F
→
08/23 20:53, , 3F
08/23 20:53, 3F
→
08/23 20:54, , 4F
08/23 20:54, 4F
→
08/23 22:18, , 5F
08/23 22:18, 5F
※ 編輯: flax00298 來自: 140.112.251.212 (08/23 22:18)
→
08/23 22:18, , 6F
08/23 22:18, 6F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):