int getnights(void)
{
int nights;
printf("Please enter the days you want live: \n");
while (scanf("%d",&nights) != 1)
{
scanf("%*s"); //處理非整數輸入
printf("Please enter an integer,sun as 2.\n");
}
return nights;
}
uj5u.com熱心網友回復:
對于字串來講,沒有多余的uj5u.com熱心網友回復:
能稍微講解一下 為什么輸入字母子類的就不行嗎 是因為有個回車字符在的原因嗎?
uj5u.com熱心網友回復:
為什么要*s?字符的話%s就是字串了啊,因為你定義的是整數的變數,所以你獲取的會省略小小數部分。
如果要輸入帶小數的,定義別的,如double型別,輸入用%lf就好了啊
%c %s都是字符型別,你輸進來也不對啊,輸個數字也許可能隱式轉換成功,這個我沒這么寫過,不太清楚,但是你輸字母給數字變數,肯定會出問題啊,還不是單個字符
uj5u.com熱心網友回復:
MSDN里面提到An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.
大致的意思是,scanf()函式的引數中,在百分號后面添加星號*,表示星號之后的所有引數都無效。使用星號的作用是讀取流中的內容,但是不會存盤這些內容。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/32548.html
標籤:新手樂園
上一篇:用c++處理圖片的問題
