aboutsummaryrefslogtreecommitdiff
path: root/src/user_input.c
blob: 061fb2e8e4cdba46ebac5a142181724d83f930a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "spm.h"

/**
 * Basic case-insensitive interactive choice function
 * @param answer
 * @param answer_default
 * @return
 */
int spm_user_yesno(int answer, int empty_input_is_yes) {
    int result = 0;
    answer = tolower(answer);

    if (answer == 'y') {
        result = 1;
    } else if (answer == 'n') {
        result = 0;
    } else {
        if (empty_input_is_yes) {
            result = 1;
        } else {
            result = -1;
        }
    }

    return result;
}

void spm_user_yesno_test() {
    int choice;
    int status;
    while ((choice = getchar())) {
        status = spm_user_yesno(choice, 1);
        if (status == -1) {
            puts("Please answer Y or N");
            continue;
        }
        else if (status == 0) {
            puts("You answered no");
            break;
        }
        else if (status == 1) {
            puts("You answered yes");
            break;
        }
    }
}