Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ex24

  • Loading branch information...
commit fa7c50602f708f6f30c6ceee4ae3e1a90b972adc 1 parent 863fe33
@wookiehangover authored
Showing with 72 additions and 1 deletion.
  1. +1 −1  Makefile
  2. +71 −0 ex24.c
View
2  Makefile
@@ -1,5 +1,5 @@
CFLAGS=-Wall -g
-SOURCES=ex1 ex4 ex6 ex7 ex8 ex9 ex10 ex11 ex12 ex13 ex14 ex15 ex16 ex17 ex18 ex20 ex23
+SOURCES=ex1 ex4 ex6 ex7 ex8 ex9 ex10 ex11 ex12 ex13 ex14 ex15 ex16 ex17 ex18 ex20 ex23 ex24
all: $(SOURCES)
View
71 ex24.c
@@ -0,0 +1,71 @@
+#include <stdio.h>
+#include "dbg.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;
+
+
+int main(int argc, char *argv[])
+{
+ Person you = {.age = 0};
+ int i = 0;
+ char *in = NULL;
+
+ printf("Whats your first name? ");
+ in = fgets(you.first_name, MAX_DATA-1, stdin);
+ check(in != NULL, "Failed to read first name");
+
+ printf("Whats 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?");
+ 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 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\n", you.income);
+
+ return 0;
+error:
+ return -1;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.