#pragma warning(disable:4996)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    const char *question;
    const char *choices[3];
    int correct;   // 1〜3
    int points;
} Quiz;

static int read_answer_1to3(void) {
    char buf[64];

    for (;;) {
        printf("回答（1〜3）> ");
        fflush(stdout); // これを入れると環境によって表示が確実になる

        if (fgets(buf, sizeof(buf), stdin) == NULL) {
            return -1;
        }

        buf[strcspn(buf, "\r\n")] = '\0';

        char *end = NULL;
        long v = strtol(buf, &end, 10);

        if (end == buf || *end != '\0') {
            puts("数字だけで入力してください。");
            continue;
        }
        if (v < 1 || v > 3) {
            puts("1〜3の範囲で入力してください。");
            continue;
        }
        return (int)v;
    }
}

int main(void) {
    Quiz quizzes[6] = {
        {"かな文字で，物語「源氏物語」を書いた。", {"1. 紫式部","2. 天草四郎","3. ヘレン・ケラー"}, 1, 20},
        {"随筆「枕草子」を書いた。", {"1. エジソン","2. 小野小町","3. 清少納言"}, 3, 15},
        {"全国各地を実測し，日本地図の作成に努めた。", {"1. 徳川家光","2. 伊能忠敬","3. 吉田松陰"}, 2, 10},
        {"日本最初の内閣総理大臣。", {"1. 田中正造","2. 小村寿太郎","3. 伊藤博文"}, 3, 15},
        {"「学問のすすめ」を書いた人物。", {"1. 福沢諭吉","2. 樋口一葉","3. 新渡戸稲造"}, 1, 20},
        {"細菌学者（医者）として，黄熱病の解明に努めた。", {"1. 山本五十六","2. 吉田茂","3. 野口英世"}, 3, 20}
    };

    int total = 0;
    int n = (int)(sizeof(quizzes) / sizeof(quizzes[0]));

    puts("--------クイズを答えてください。--------");

    for (int i = 0; i < n; i++) {
        puts("");
        printf("第%d問（配点 %d点）\n", i + 1, quizzes[i].points);
        puts(quizzes[i].question);
        puts(quizzes[i].choices[0]);
        puts(quizzes[i].choices[1]);
        puts(quizzes[i].choices[2]);

        int ans = read_answer_1to3();
        if (ans == -1) {
            puts("\n入力が終了しました。");
            break;
        }

        if (ans == quizzes[i].correct) {
            puts("正解です。");
            total += quizzes[i].points;
        } else {
            printf("不正解です。正解は%dです。\n", quizzes[i].correct);
        }
    }

    puts("");
    printf("あなたの得点は %d 点です。\n", total);

    // ここが重要：Visual Studioでウィンドウが閉じる問題を潰す
    puts("Enterキーで終了します。");
    getchar();

    return 0;
}