-
Notifications
You must be signed in to change notification settings - Fork 0
/
mylib.c
84 lines (74 loc) · 1.52 KB
/
mylib.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
82
83
84
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio_ext.h>
#include "mylib.h"
#define MAXLENGTH 64
// ------------------------------------------
int print_manual() {
char buffer[MAXLENGTH];
FILE *fp = fopen("README.md", "r");
while (fgets(buffer, MAXLENGTH, fp)) {
printf("%s", buffer);
}
printf("\n");
fclose(fp);
return 0;
}
// ------------------------------------------
char mygets(char *str) {
fgets(str, MAXLENGTH, stdin);
str[strlen(str) - 1] = '\0';
return *str;
}
// ------------------------------------------
int myget_int() {
int number;
char buffer[MAXLENGTH];
while (1) {
fgets(buffer, MAXLENGTH, stdin);
number = atoi(buffer);
if (number <= 0) {
printf("Error! Please, enter the correct number...: ");
continue;
}
break;
}
return number;
}
// ------------------------------------------
int myget_int_range(int a, int b) {
int number;
while(1) {
number = myget_int();
if (number >= a && number <= b) {
return number;
break;
} else {
printf("Error! Please, enter the correct number in range [%d-%d]...: ", a, b);
continue;
}
}
}
// ------------------------------------------
int confirm_choice() {
char answer;
while(1) {
answer = getchar();
__fpurge(stdin);
if(answer == 'y' || answer == 'Y') {
return 1;
}
if(answer == 'n' || answer == 'N') {
return 0;
}
printf("\nError! Please, make your choice... (y/n)? ");
}
}
// ------------------------------------------
#ifdef __cplusplus
}
#endif