#include <stdio.h>

#define MAXSTUDENTS 100

typedef struct {
	int id;
	char name[100];
	double grade;	
} student;

student s[MAXSTUDENTS];

void init(){
	int i;
	for(i=0; i<MAXSTUDENTS; i++){
		s[i].id = -1;
	}
}

void print_menu(){
	printf("Choose from 1 to 4:\n1. Add new student\n2. Delete student by AM\n3. Print all students\n4. Quit\n");
}

int find_student(int AM){
	int i;
	for (i=0; i<MAXSTUDENTS; i++){
		if (s[i].id == AM){
			return i;
		}
	}
	return -1;
}

void add_new_student(){
	int id = 0;
	printf("please provide the id of the student\n");
	scanf("%d",&id);

	int i = find_student(id);	//check for the id in case it is an update of an existing record
	if (i == -1){				//there's no student with that id, so no update
		i = find_student(-1);	//but we need a new slot to add the new data
		if (i == -1){
			printf("No empty slot\n");
			return;
		}
	}

	s[i].id = id;

	printf("Please provide the name and grade of the student with the id: %d.\n",id);
	scanf("%s%lf",s[i].name, &s[i].grade);
}

void delete_student(){
	int AM = -2;

	printf("Please provide a student id.\n");
	scanf("%d",&AM);

	int i = find_student(AM);
	if (i == -1){
		printf("Requested id was not found\n");
		return;
	}
	s[i].id = -1;
}

void print_students(){
	int i;
	for(i=0;i<MAXSTUDENTS;i++){
		if (s[i].id != -1){
			printf("%d\t%s\t%lf\n",s[i].id,s[i].name,s[i].grade);
		}
	}
}

int main(int argc, char **argv){
	int run = 1;
	char choice;
	init();
	while( run ) {
		print_menu();
		do {
			scanf("%c", &choice);
		}
		while( choice < '1' || choice > '4' );
		switch( choice ) {
			case '1':
				add_new_student();
				break;
			case '2':
				delete_student();
				break;
			case '3':
				print_students();
				break;
			case '4':
				run = 0;
				break;
		}
	}
}






