質問編集履歴

2

追記

2017/06/01 10:44

投稿

sonozaki_SZ
sonozaki_SZ

スコア28

test CHANGED
File without changes
test CHANGED
@@ -206,6 +206,120 @@
206
206
 
207
207
  56
208
208
 
209
-
210
-
211
- ```
209
+ ```
210
+
211
+ 2017/06/01 19:44 追記
212
+
213
+ 皆様の意見を参考にしてread_year_month関数を下記のように修正したら再起がうまく機能するようになりました
214
+
215
+ ```C言語
216
+
217
+ #include <stdio.h>
218
+
219
+ #include <stdbool.h>
220
+
221
+ #include "calendar.h"
222
+
223
+
224
+
225
+ void read_year_month(int *y, int *m)
226
+
227
+ {
228
+
229
+ char str[STR_LEN];
230
+
231
+ int i;
232
+
233
+ bool err_flg;
234
+
235
+
236
+
237
+ /* 入力バッファのクリア */
238
+
239
+ fflush(stdin);
240
+
241
+
242
+
243
+ puts("年を2016~2099の範囲、月を1~12の範囲で入力して下さい");
244
+
245
+ puts("年と月の間を半角スペースで区切って下さい");
246
+
247
+ puts("例:2016 1/2099 12");
248
+
249
+ printf("年と月の入力:");
250
+
251
+ fgets(str, sizeof(str), stdin);
252
+
253
+ fflush(stdin);
254
+
255
+
256
+
257
+ /* 数字以外の値が入力された場合にエラー処理 */
258
+
259
+ for (i = 0; i < STR_LEN-1; i++)
260
+
261
+ {
262
+
263
+ if (('0' < str[i] && str[i] < '9') || (str[i] == ' ' || str[i] == '\n'))
264
+
265
+ {
266
+
267
+ err_flg = false;
268
+
269
+ }
270
+
271
+ else
272
+
273
+ {
274
+
275
+ err_flg = true;
276
+
277
+ }
278
+
279
+ }
280
+
281
+ sscanf(str, "%d %d", y, m); /* 文字型を整数型に変換して変数y, mに値を代入 */
282
+
283
+ /* 範囲外の値が入力されたらエラー処理 */
284
+
285
+ if ((*y < 2016 || 2099 < *y) || (*m < 1 || 12 < *m))
286
+
287
+ {
288
+
289
+ err_flg = true;
290
+
291
+ }
292
+
293
+ else
294
+
295
+ {
296
+
297
+ err_flg = false;
298
+
299
+ }
300
+
301
+ if (err_flg == true)
302
+
303
+ {
304
+
305
+ puts("入力された値は不正です");
306
+
307
+ puts("入力し直して下さい");
308
+
309
+ read_year_month(y, m);
310
+
311
+ }
312
+
313
+ else
314
+
315
+ {
316
+
317
+ /* NOP */
318
+
319
+ }
320
+
321
+ }
322
+
323
+
324
+
325
+ ```

1

誤字

2017/06/01 10:44

投稿

sonozaki_SZ
sonozaki_SZ

スコア28

test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  /* 関数プロトタイプ宣言 */
10
10
 
11
- int read_year_month(int *y, int *m); /* 年と月の入力と入力された値の判定後に値を引数に代入 */
11
+ void read_year_month(int *y, int *m); /* 年と月の入力と入力された値の判定後に値を引数に代入 */
12
12
 
13
13
  void d_days(int *ds, int y, int m); /* 日数の判定 */
14
14