[問題] 將cin放進while的判斷式
請教兩個cin觀念的問題
我有問題的code如下
int choice=0;
while( (choice=getChoice()) != 0)
{
cout<<"hello"<<endl;
}
int getChoice()
{
int c=0;
cin>>c;
return c;
}
當cin時給int都可以正常運作
但若故意在cin時給char, 為何會產生無限loop?
(我以為會是char轉成int非 0, 印一次hello, 再回到while判斷)
另外一個問題是我改用 while(cin>>choice != 0) 當判斷
反而是給char不會進loop, 又是為什麼呢
(>>會傳回istream的cin, 是嗎? 這個cin非int 0不是該進loop嗎?)
可能有很多觀念錯誤的地方..煩請指教, 謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.69.128.66
※ 編輯: jehovah 來自: 210.69.128.66 (08/05 15:13)
→
08/05 15:20, , 1F
08/05 15:20, 1F
→
08/05 15:21, , 2F
08/05 15:21, 2F
→
08/05 15:22, , 3F
08/05 15:22, 3F
→
08/05 15:22, , 4F
08/05 15:22, 4F
→
08/05 15:24, , 5F
08/05 15:24, 5F
→
08/05 15:25, , 6F
08/05 15:25, 6F
→
08/05 15:25, , 7F
08/05 15:25, 7F
→
08/05 15:30, , 8F
08/05 15:30, 8F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章