Hi Guys,
I have a tutorial question that ask that I make a program to hold students names and grades.
I must be able to search by name or ID number.
I have the programe running except the search by name, if there is more than 1 record with the same name i get a general protection fault adn the program crashes, if there is just 1 name in the records the search works fine.
I wonder of someone could look at my function "search_for_name()" and guide me in the right direction.
Many thanks in advance
C
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
/*struct for a student */
typedef struct
{
char name[20];
int grade1;
int grade2;
int grade3;
int grade4;
int id;
} names;
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
#define MAX 999
names names_database[20];
int index=0;
int id=100;
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
/* Functions used in this program */
void menu_controller(void);
void draw_menu(void);
int get_menu_option(void);
void process_menu_option(int option);
void add_new_name(void);
void search_for_name(void);
void search_for_id(void);
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
/*The main program*/
void main(void)
{
menu_controller();
}
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
/* These are the specifications for the functions called*/
void menu_controller()
{
/*This function is the main controlling routine for our menu*/
int menu_choice=0;
/*The controlling loop for the whole programe*/
while(menu_choice !=9)
{
/*step 1 draw menu*/
draw_menu();
/*Step2 - Prompt for a user responce*/
menu_choice=get_menu_option();
/*Step3 - Action the users choice*/
process_menu_option(menu_choice);
}
}
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
void draw_menu(void)
{
/*This function simply draws the main menu - nothing else*/
clrscr();
/* Display the menu options*/
printf("\n\n\t\t Student Search program\n\n");
printf("\t\t\t 1. Add new name and 4 exam percentages\n");
printf("\t\t\t 2. Search by name\n");
printf("\t\t\t 3. Search by ID number\n");
printf("\t\t\t 9. Quit\n");
}
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
int get_menu_option(void)
{
/*This function returns the menu option selected by the user*/
int option;
printf("\n\n\n\t\t Please enter your option: ");
scanf("%d", &option);
fflush(stdin);
return option;
}
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
void process_menu_option(int option)
{
/*An intiger to hold the value returned by getchar() */
int key;
/*This function eveluates each option and calls the appropriate function*/
switch(option)
{
case 1 : add_new_name();
break;
case 2 : search_for_name();
break;
case 3 : search_for_id();
break;
case 9 : printf("\n\nThank you, the program will now exit....\n");
break;
default : printf("\n\nSorry that is not a valid option - press enter to continue");
key = getchar();
break;
} /* End switch*/
} /* End process_menu_option() */
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
/*These are the functions for our menu options, but they dont do much... */
void add_new_name(void)
{
clrscr();
/* Add the name and DOB to the database*/
printf("\n\n\t\t Enter new name and Grades\n\n");
printf("Enter new name: ");
gets(names_database[index].name);
fflush(stdin);
printf("Please enter 4 grade marks in as a percentage value:\n\n");
scanf("%d%d%d%d",&names_database[index].grade1, &names_database[index].grade2,
&names_database[index].grade3,&names_database[index].grade4);
fflush(stdin);
names_database[index].id =id;
/* Increment the student array & id position*/
index++;
id++;
}
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
void search_for_name(void)
{
int found = 0;
int result;
nt loop=0;
int key;
int average;
char name[20];
clrscr();
printf("Please enter the name to search for: ");
gets(name);
fflush(stdin);
while(found==0 && loop < MAX)
{
result = strcmp(names_database[loop].name,name);
if(result ==0)
{
average = (names_database[loop].grade1 + names_database[loop].grade2 +
names_database[loop].grade3 + names_database[loop].grade4) / 4 ;
printf("The average grade for %s is %d the ID number is %d\n",names_database[loop].name,average,names_database[loop].id);
found=1;
}
loop++;
}
if(loop == MAX)
{
printf("NOT FOUND\n");
}
key = getchar();
}
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
void search_for_id(void)
{
int found = 0;
int loop=0;
int average;
int key;
int getid;
clrscr();
printf("Please enter the ID Number to search for between 100 & 999: ");
scanf("%d",&getid);
fflush(stdin);
if ( getid == names_database[loop].id)
{
average = (names_database[loop].grade1 + names_database[loop].grade2 +names_database[loop].grade3 + names_database[loop].grade4) / 4 ;
printf("The average grade for %s is %d the ID number is %d\n",names_database[loop].name,average,names_database[loop].id);
found=1;
}
if ( getid != names_database[loop].id)
{
printf("NOT FOUND\n");
}
loop++;
key = getchar();
}