📄 input.cpp
字号:
#include <iostream.h>
template<class T>
bool Input(T& x)
{// Input a nonnegative value.
int NumOfAttempts = 3;
// try at most NumOfAttempts times
for (int i = 0; i < NumOfAttempts; i++) {
cout << "Enter a nonnegative value" << endl;
cin >> x;
if (x >= 0) return true;
cout << x << " is not nonnegative" << endl;
}
// did not get nonnegative value
cout << "Sorry, you get only "
<< NumOfAttempts << " chances."
<< endl;
return false;
}
void main()
{// Test Input.
int a;
if (Input(a)) cout << "The number is " << a << endl;
else cout << "Did not get a nonnegative number" << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -