mardi 30 décembre 2014

My program won't do search function well. Why?


Vote count:

0




I made a student database program as my latest project in school. Everything works fine, you can create records with ID's, names, surnames and marks.


The main problem here is; "Name search" function of my code.


Whenever I search someone with ID it just works flawless, but if I try to do it with name there are some problems.


If you try to search 1st student it will find but will never search for others. Will only show 1st student whatever you put as the input and if you do a search for same length student names it might find wrong student.


Example 1: http://i.imgur.com/YfKqbE1.png


As you can see I typed in Yov but it found Wow.


Example 2: http://i.imgur.com/HkvFSzU.png


As you can see I typed in Testing as the 1st input and true student showed up. Main problem started with whenever I typed Stack as the following input. I expected to Stack Overflow show up but again Testing student shown as the output.


Those are the 2 things that "only" problems that I experience in my program. Where is my mistake?


Here is my full code;



#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct
{
long long int id;
char firstname[20];
char lastname[20];
int mark;
}student;

int main()
{
long long int idnumber;
int flag,choice,shift,found,continu,length;
char studentname[20];
FILE *fp;

printf("\n\tC PROGRAM OF STUDENT DATABASE SYSTEM");
Label1:
printf("\n1 -> Store a new record in database\n");
printf("2 -> Search a student record by Student First Name\n");
printf("3 -> Search a student record by ID\n");
printf("4 -> Quit Student Database");
printf("\n\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
Label2:
printf("\nEnter Student Details:\n\nID number: ");
scanf("%lld",&student.id);
printf("\nName:");
scanf("%s",student.firstname);
printf("\nSurname:");
scanf("%s",student.lastname);
printf("\nMark(0 - 100 integer) : ");
scanf("%d",&student.mark);
fp=fopen("studentfile.txt","a+");
fprintf(fp,"\n%lld\t%s\t%s\t%d\t",student.id,student.firstname,student.lastname,student.mark);
fclose(fp);
printf("A student record has been added successfully...\n");
printf("\n\n1 -> Wish to add another record to database");
printf("\n2 -> Wish to move to Main Menu");
printf("\n3 -> Exit from Program\n");
scanf("%d",&shift);
if(shift==1)
goto Label2;
if(shift==2)
goto Label1;
if(shift==3)
break;
if(shift!=1&&2&&3){
printf("Exiting.........");
break;
}

case 2:
Label4:
printf("\nEnter student first name: ");
scanf("%s",&studentname);
printf("Searching record with studentname=%s.\n",studentname);
found=0;
if((fp=fopen("studentfile.txt","r"))==NULL)
{
printf(" ! The File is Empty...\n\n");
}
else
{
while(!feof(fp)&& found==0)
{
fscanf(fp,"\n%lld\t%s\t%s\t%d\t",&student.id,student.firstname,student.lastname,&student.mark);
length = strlen(student.firstname);
if(student.firstname[length]==studentname[length])
found=1;
}
}
if(found)
{
printf("\nThe record is found.\n");
printf("\nID: %lld\nName: %s\nSurname: %s\nMark: %d \n",student.id,student.firstname,student.lastname,student.mark);
}
else
{
printf("Not found...\n");
getch();
}
Label5:
printf("\n\n1 -> Wish to search another record");
printf("\n2 -> Wish to move to Main Menu");
printf("\n3 -> Exit from Program\n");
scanf("%d",&shift);
if(shift==1)
goto Label4;
if(shift==2)
goto Label1;
if(shift==3)
break;
if(shift!=1&&2&&3){
printf("\nEnter a valid choice");
goto Label5;
}
case 3:
Label6:
printf("\nEnter the ID: ");
scanf("%lld",&idnumber);
printf("Searching record with ID=%lld.\n",idnumber);
found=0;
if((fp=fopen("studentfile.txt","r"))==NULL)
{
printf(" ! The File is Empty...\n\n");
}
else
{
while(!feof(fp)&& found==0)
{
fscanf(fp,"\n%lld\t%s\t%s\t%d\t",&student.id,student.firstname,student.lastname,&student.mark);
if(student.id==idnumber)
found=1;
}
}
if(found)
{
printf("\nThe record is found.");
printf("\nID no: %lld\nName: %s\nSurname: %s\nMark: %d \n",student.id,student.firstname,student.lastname,student.mark);
}
else
{
printf("Not found...\n");
getch();
}
Label7:
printf("\n\n1 -> Wish to search more..");
printf("\n2 -> Wish to move to Main Menu");
printf("\n3 -> Exit from Program\n");
scanf("%d",&shift);
if(shift==1)
goto Label6;
if(shift==2)
goto Label1;
if(shift==3)
break;
if(shift!=1&&2&&3){
printf("\nEnter a valid choice");
goto Label7;
}
case 4: break;
default :
printf(" Bad choice...Enter the choice again...\n");
goto Label1;
}

getch();
return 0;
}



iharob

3,263

asked 1 min ago







My program won't do search function well. Why?

Aucun commentaire:

Enregistrer un commentaire