-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.c
71 lines (64 loc) · 1.6 KB
/
utilities.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
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <stdbool.h>
#ifndef __utilities__
#define __utilities__
void inputChar(char* message, char* input){
// scan to pointer
printf("%s\n>>>", message);
scanf(" %c", input);
// empty buffer
while(getchar() != '\n');
}
void inputString(char* message, char* input){
// scan to pointer
printf("%s\n>>>", message);
scanf("%[^\n]s", input);
// empty buffer
while(getchar() != '\n');
}
void inputInt(char* message, int* input){
// scan to pointer
printf("%s\n>>>", message);
while(scanf(" %d", input) == 0) {
// empty buffer
while(getchar() != '\n');
printf("%s\n>>>", "Please input an integer");
}
// empty buffer
while(getchar() != '\n');
}
void inputFloat(char* message, float* input){
// scan to pointer
printf("%s\n>>>", message);
while(scanf(" %f", input) == 0) {
// empty buffer
while(getchar() != '\n');
printf("%s\n>>>", "Please input an integer");
}
// empty buffer
while(getchar() != '\n');
}
void inputBool(char* message, bool* input){
char string[5];
bool done = false;
// scan to pointer
printf("%s\n>>>", message);
while(!done) {
scanf("%[^\n]s", string);
// Check if 'true', 't', 'false' or 'f'
if(strcasecmp(string, "true") || strcasecmp(string, "t")) {
*input = true;
done = true;
} else if(strcasecmp(string, "false") || strcasecmp(string, "f")) {
*input = false;
done = true;
} else {
printf("%s\n>>>", "Please input 'true', 't', 'false' or 'f'");
}
// empty buffer
while(getchar() != '\n');
}
}
#endif