![](/img/trans.png)
[英]How can you generate a number that is 5-10% greater than the number that the user inputted using the rand() function?
[英]How to validate user input a number and the number greater than 0
我正在嘗試驗證用戶輸入的內容,即用戶必須輸入數字並且該數字必須大於0,這僅是我使它有效的數字的驗證; 但是,我似乎無法合並大於0的驗證
float income;
cout << "How much did you earn last year: ";
//validating imput for income
while(!(cin >> income))
{
char ch;
cin.clear();
cout << "Sorry, number must be biger than \"0\" \n"
<< "How much did you make last year: ";
while(cin.get(ch) && ch != '\n');
}
只需將條件添加到while
,然后在循環中處理它:
while ( !(cin >> income) || income < 0.0 ) {
if ( !cin )
// Clean up input stream...
else
// Must be negative number...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.