-
Simple search program
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();
}
-
>#define MAX 999
>names names_database[20];
>.
>.
>while(found==0 && loop < MAX)
You've only got an array of 20 names, but your loop runs to 999, way off the end of the array.
-
Thank you
Thanks very much swoopy that seems to have done the trick :D
-
Welcome to the world of buffer overflow. Stop learning how to prevent it colinuk, it may hinder your ability to ever work at Microsoft.
-
-
:) Good one Master5001.
>fflush(stdin);
Colinuk, glad that worked. One other problem. fflush(stdin) is wrong. See:
FAQ > Explanations of > Why fflush(stdin) is wrong
and FAQ > How do I > Flush the input buffer
-
About buffer overflows again, you need to be careful since sometimes they come up from simple mistakes such as typoes.
Example:
Code:
int array[200], i;
for(i = 0; i < 2000; i++) {
array[i] = getchar();
}
In the above code, oops I typed 2000 instead of 200. This is another reason why I promote not using "magic numbers." Its harder to get away with simple typing erros when typing something like:
Example:
Code:
// assumes somewhere there is #define BUFFER_SIZE 200
int array[BUFFER_SIZE], i;
for(i = 0; i < BUFFER_SIZEE; i++) {
array[i] = getchar();
}
Oops I typed an extra E on BUFFER_SIZE. Good thing this time is the compiler has no idea what I'm talking about and the crisis was averted.