Advertisement
cd62131

GetNumber

Jul 3rd, 2014
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <limits>
  3. using namespace std;
  4. int get_num() {
  5.   int a;
  6.   while (true) {
  7.     cout << "1から10までの数値を入力してください。" << endl;
  8.     for (cin >> a; !cin; cin >> a) {
  9.       cin.clear();
  10.       cin.ignore(numeric_limits<streamsize>::max(), '\n');
  11.       cout << "形式が違います。" << endl;
  12.     }
  13.     if (1 <= a && a <= 10) break;
  14.   }
  15.   return a;
  16. }
  17. int main() {
  18.   int a = get_num();
  19.   cout << a << endl;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement