-
Notifications
You must be signed in to change notification settings - Fork 4
/
terminal_user_input.c
107 lines (90 loc) · 3.03 KB
/
terminal_user_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
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
// ============================
// = User Input Function in C =
// ============================
#include <stdio.h>
#include "terminal_user_input.h"
//
// Reads a string of up to 255 characters + 1 for null
//
my_string read_string(const char* prompt)
{
my_string result; // declares a "my_string" variable (contains the array of character)
printf("%s", prompt); // output the string from the prompt "%s" defines where to place the string in the output
scanf(" %255[^\n]%*c", result.str ); // scan the input looking for upto 255 characters [ that are not newlines ], read this into the string variable
return result; // return the my string value
}
//
// Reads a integer from the user.
//
int read_integer(const char* prompt)
{
my_string line;
int result; // where we will store the result of the function
char temp; //used to check nothing comes after the int
// Read in the string the user entered.
line = read_string(prompt);
// scan the string, looking for a number ... followed by nothing
// sscanf = string scan format
// This will "scan" the array of character in line.str (reads this)
// " " = skip any spaces
// "%d" = read an integer
// " " = skip any spaces
// "%c" = read a character
// sscanf returns the number of things it read (0 to 2 in this case)
// Loop while this is not equal to 1
// 0 = did not read a number at the start
// 1 = read a number, but no character followed it
// 2 = read a number and a character... like "1 fred" (1 is the number, f is the character)
while ( sscanf(line.str, " %d %c", &result, &temp) != 1 )
{
// scan found a number followed by something... so its not a whole number
printf("Please enter a whole number.\n");
// read the next "string" and try again
line = read_string(prompt);
}
return result;
}
int read_integer_range(const char* prompt, int min, int max)
{
int result = read_integer(prompt);
while ( result < min || result > max )
{
printf("Please enter a number between %d and %d\n", min, max);
result = read_integer(prompt);
}
return result;
}
double read_double(const char* prompt)
{
my_string line;
double result; // where we will store the result of the function
char temp; //used to check nothing comes after the int
// Read in the string the user entered.
line = read_string(prompt);
while ( sscanf(line.str, " %lf %c", &result, &temp) != 1 )
{
// scan found a number followed by something... so its not a whole number
printf("Please enter a number.\n");
// read the next "string" and try again
line = read_string(prompt);
}
return result;
}
float read_float(const char* prompt)
{
return (float)read_double(prompt);
}
int read_boolean(const char* prompt)
{
my_string line;
line = read_string(prompt);
while( line.str[0] != 'y' && line.str[0] != 'n' ) {
printf("Please enter yes or no [y/n].");
line = read_string(prompt);
}
if (line.str[0] == 'y') {
return 1;
} else {
return 0;
}
}