teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

微修正

2020/05/20 08:19

投稿

episteme
episteme

スコア16612

answer CHANGED
@@ -5,7 +5,6 @@
5
5
 
6
6
  #include <stdio.h>
7
7
  #include <string.h>
8
- #include <stdbool.h>
9
8
 
10
9
  int input_int(const char* prompt, const char* bad_message) {
11
10
  int n;

1

追記

2020/05/20 08:19

投稿

episteme
episteme

スコア16612

answer CHANGED
@@ -26,5 +26,34 @@
26
26
  printf("%d を入力してくれてアリガト\n", n);
27
27
  return 0;
28
28
  }
29
+ ```
29
30
 
31
+ [追記] 1返したらアカンやろ! バージョン:
32
+ ```C
33
+ // VC++ : C3861 抑止
34
+ #define _CRT_SECURE_NO_WARNINGS
35
+
36
+ #include <stdio.h>
37
+ #include <string.h>
38
+ #include <stdbool.h>
39
+
40
+ bool input_int(int* result, const char* prompt, const char* bad_message) {
41
+ while (1) {
42
+ char buf[1024];
43
+ if ( prompt ) printf("%s", prompt);
44
+ if (fgets(buf, sizeof buf, stdin) == NULL) return false;
45
+ char c;
46
+ if (sscanf(buf, "%d %c", result, &c) == 1) break;
47
+ if ( bad_message ) puts(bad_message);
48
+ }
49
+ return true;
50
+ }
51
+
52
+ int main(void) {
53
+ int n;
54
+ if ( input_int(&n, "整数値入れてネ: ", "ダメ。やりなおし!") ) {
55
+ printf("%d を入力してくれてアリガト\n", n);
56
+ }
57
+ return 0;
58
+ }
30
59
  ```