Repository navigation
Expand file tree
/
Copy pathex24.c
More file actions
103 lines (81 loc) · 2.3 KB
/
Copy pathex24.c
File metadata and controls
103 lines (81 loc) · 2.3 KB
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
#include "dbg.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DATA 100
typedef enum EyeColor {
BLUE_EYES,
GREEN_EYES,
BROWN_EYES,
BLACK_EYES,
OTHER_EYES
} EyeColor;
const char *EYE_COLOR_NAMES[] = {"Blue", "Green", "Brown", "Black", "Other"};
typedef struct Person {
int age;
char first_name[MAX_DATA];
char last_name[MAX_DATA];
EyeColor eyes;
float income;
} Person;
char *read_str(int size) {
char *result = malloc(sizeof(char * [size + 1]));
int i = 0;
for (i = 0; i < size; i++) {
printf("Enter a character: \n");
scanf(" %c", &result[i]);
printf("input so far: %s\n", result);
}
result[size] = '\0';
return result;
}
// int main(void) {
// printf("How many characters you want to input?: \n");
// int number_of_chars = 0;
// int rc = fscanf(stdin, "%d", &number_of_chars);
// check(rc > 0, "Please input an integer");
// char *in = read_str(number_of_chars);
//
// printf("input: %s\n", in);
// printf("size: %lu\n", strlen(in));
//
// error:
// return 1;
// }
int main(int argc, char *argv[]) {
Person you = {.age = 0};
int i = 0;
char *in = NULL;
printf("What's your First Name? ");
in = fgets(you.first_name, MAX_DATA - 1, stdin);
check(in != NULL, "Failed to read first name");
printf("What's your Last Name? ");
in = fgets(you.last_name, MAX_DATA - 1, stdin);
check(in != NULL, "Failed to read last name");
printf("How old are you? ");
int rc = fscanf(stdin, "%d", &you.age);
check(rc > 0, "You have to enter a number");
printf("What color are your eyes:\n");
for (i = 0; i <= OTHER_EYES; i++) {
printf("%d) %s\n", i + 1, EYE_COLOR_NAMES[i]);
}
printf("> ");
int eyes = -1;
rc = fscanf(stdin, "%d", &eyes);
check(rc > 0, "You have to enter a number.");
you.eyes = eyes - 1;
check(you.eyes <= OTHER_EYES && you.eyes >= 0,
"Do it right, that's not an option");
printf("How much do you make in an hour? ");
rc = fscanf(stdin, "%f", &you.income);
check(rc > 0, "Enter a floating point number");
printf("---- RESULTS ----\n");
printf("First Name: %s", you.first_name);
printf("Last Name: %s", you.last_name);
printf("Age: %d\n", you.age);
printf("Eyes: %s\n", EYE_COLOR_NAMES[you.eyes]);
printf("Income: %f per hour\n", you.income);
return 0;
error:
main(argc, argv);
}