-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_input.c
83 lines (72 loc) · 1.46 KB
/
get_input.c
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <stdbool.h>
#include "get_input.h"
void wait_til_newline(void);
void enter_prompt(char *type_name, bool is_retry);
char *get_string(void) {
char n[256];
static char *r;
int i = 0;
enter_prompt("string", false);
r = fgets(n, 256, stdin);
if (r) {
while (n[i] != '\n' && n[i] != '\0') {
i++;
}
if (n[i] == '\n') {
n[i] = '\0';
} else {
wait_til_newline();
}
}
return r;
}
int get_int(void) {
int r;
enter_prompt("integer", false);
while(scanf("%d", &r) != 1) {
enter_prompt("integer", true);
}
wait_til_newline();
return r;
}
long get_long(void) {
long r;
enter_prompt("long integer", false);
while(scanf("%ld", &r) != 1) {
enter_prompt("long integer", true);
}
wait_til_newline();
return r;
}
char get_char(void) {
int ch;
enter_prompt("char", false);
ch = getchar();
wait_til_newline();
return ch;
}
double get_double(void) {
double r;
char n[256];
int i = 0;
while (1) {
enter_prompt("double", i++);
fgets(n, 256, stdin);
if (sscanf(n, "%lf", &r) == 1) {
break;
}
}
return r;
}
void wait_til_newline(void) {
while(getchar() != '\n') {
;
}
}
void enter_prompt(char *type_name, bool is_retry) {
if (is_retry) {
printf("Try again. ");
}
printf("Please enter a %s: ", type_name);
}