Node Iterator and Start Input Error Checking

This commit is contained in:
stitchy 2023-10-13 03:29:13 -07:00
parent 22d475db00
commit 15e004dc7c
Signed by: stitchy
SSH key fingerprint: SHA256:yz2SoxdnY67tfY5Jzb0f2v8f5W3o/IF359kbcquWip8
7 changed files with 79 additions and 9 deletions

21
movies/input.c Normal file
View file

@ -0,0 +1,21 @@
#include "input.h"
int integer_input(char* val) {
int num;
char *error = "";
do {
printf("\n%s\n%s",error, val);
fflush(stdout);
char buf[128];
read(STDIN_FILENO, buf, 127);
num = atoi(buf);
error = "Input Error, try again.";
} while(!num);
return num;
}

11
movies/input.h Normal file
View file

@ -0,0 +1,11 @@
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#ifndef INPUT
#define INPUT
int integer_input(char*);
#endif

View file

@ -1,8 +1,8 @@
CC=gcc --std=c99 -g
output=movies
all: movies.c csv_parser.o node.o
$(CC) movies.c csv_parser.o node.o -o $(output)
all: movies.c csv_parser.o node.o input.o
$(CC) movies.c csv_parser.o node.o input.o -o $(output)
csv_parser.o: csv_parser.c csv_parser.h
$(CC) -c csv_parser.c -o csv_parser.o
@ -10,6 +10,9 @@ csv_parser.o: csv_parser.c csv_parser.h
node.o: node.c node.h
$(CC) -c node.c -o node.o
input.o: input.h input.c
$(CC) -c input.c -o input.o
run:
./movies movies_sample_1.csv

View file

@ -39,23 +39,39 @@ int main(int argc, char** argv) {
int menu() {
int option = 0;
printf("1. Show movies released in the specified year\n");
printf("2. Show highest rated movie for each year\n");
printf("3. Show the title and year of release of all movies in a specific language\n");
printf("4. Exit from the program\n\n");
while(option > 4 || option < 1) {
printf("Enter a choice from 1 to 4: ");
scanf("%i", &option);
}
int option;
do {
option = integer_input("Enter a choice from 1 to 4: ");
} while(option > 4 || option < 1);
return option;
}
int year_print(struct csv* data, void* val) {
if(data->year != *(int*)val)
return 0;
printf("%s\n", data->title);
return 1;
}
void movie_year(struct node* head) {
printf("stub\n");
void* f = &year_print;
int* val = malloc(sizeof(int));
*val = integer_input("Enter year to search movies from: ");
iterate_nodes(head, f, val);
printf("\n\n");
}

View file

@ -8,6 +8,7 @@
#include "csv_parser.h"
#include "node.h"
#include "input.h"
int menu();

View file

@ -45,3 +45,19 @@ int count_nodes(struct node* head) {
return num;
}
void iterate_nodes(struct node* head, void *func (void*, void*), void* val) {
if(!head)
return;
if(head->node)
func(head->data, val);
struct node* temp = head;
while(temp->node) {
temp = temp->node;
func(temp->data, val);
}
}

View file

@ -14,4 +14,6 @@ struct node* appendv_node(struct node*, void*);
int count_nodes(struct node*);
void iterate_nodes(struct node*, void *func(void*, void*), void*);
#endif