-
Notifications
You must be signed in to change notification settings - Fork 0
/
userinput.c
150 lines (137 loc) · 3.39 KB
/
userinput.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
// responsibility of caller to free line after usage for this function.
char *getLine()
{
char *line = NULL;
size_t len = 0;
ssize_t linelen;
int choice;
char *end;
linelen = getline(&line, &len, stdin);
if (linelen < 0)
{
free(line);
printf("Could not read user input. Try again.");
return NULL;
}
int lastCharacter = strlen(line) - 1;
if (line[lastCharacter] == '\n')
{
line[lastCharacter] = '\0';
}
return line;
}
char *getLimitedLine(int limit)
{
char *line = NULL;
int length = 0;
while (length <= 0 || length >= limit || line == NULL)
{
line = getLine();
length = strlen(line);
if (length >= limit)
{
free(line);
printf("The input was too long. Please try again:\n");
}
}
return line;
}
// function should handle freeing memory itself (as only 1, 0 or -1 is returned (-1 for error))
int getYesNoResponse(char *msg)
{
char *input;
int response = 2;
while (response > 1)
{
printf("%s", msg);
printf("Please type 'yes'/'y' or 'no'/'n' below:\n");
input = getLine();
if (strcmp(input, "yes") == 0 || strcmp(input, "y") == 0)
{
free(input); // free input as no longer needed.
return 1;
}
else if (strcmp(input, "no") == 0 || strcmp(input, "n") == 0)
{
free(input); // free input as no longer needed.
return 0;
}
else
{
printf("Please try again.\n");
}
}
free(input); // free input as no longer needed.
return -1;
}
// function should handle freeing memory itself as a parsed integer is returned rather than the string.
int getPositiveInt()
{
char *line = getLine();
int choice;
char *end;
choice = strtol(line, &end, 10);
if (end == line || choice < 0)
{
if (choice == 0)
{
if (errno == ERANGE)
{
printf("Input out of range\n");
}
if (errno == EINVAL)
{
printf("Could not parse input.\n");
}
}
printf("Please try again with a valid number.\n");
return -1;
}
free(line); // have to free line as no longer being used after this function exits.
return choice;
}
float getFloat()
{
char *line = getLine();
char *end;
float num;
num = strtof(line, &end);
if (end == line)
{
if (errno == ERANGE)
{
printf("Input out of range\n");
}
printf("Please try again with a valid number.\n");
free(line); // have to free line as no longer being used after this function exits.
return -1.0;
}
free(line);
// printf("Float: %.2f\n", num);
return num;
}
float getBoundPositiveFloat(int lowerBound, int upperBound)
{
float num = -1.0;
while (num == -1.0 || (num < lowerBound || num > upperBound))
{
num = getFloat();
if (num == -1.0)
{
printf("Try again: \n");
}
else if (num < lowerBound)
{
printf("Input is too low. Try again:\n");
}
else if (num > upperBound)
{
printf("Input is too high. Try again:\n");
}
}
return num;
}