-4

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:

enter image description here

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

Example 2:

enter image description here

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;
}
Rizier123
  • 56,111
  • 16
  • 85
  • 130

1 Answers1

0

This answers your question, but please fix the if thing

if(student.firstname[length]==studentname[length])

has to be

if(strcmp(student.firstname[length], studentname[length]) == 0)

also

scanf("%s", &studentname);

is wrong it should be

scanf("%s", studentname);

it just accidentally works.

Here you have, consider it a hollidays gift

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

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

void
storeRecord()
{
    FILE *fp;

    printf("\nEnter Student Details:\n\nID number: ");
    scanf("%lld",&student.id);

    printf("\nName:");
    scanf("%19s",student.firstname);

    printf("\nSurname:");
    scanf("%19s",student.lastname);

    printf("\nMark(0 - 100 integer) : ");
    scanf("%d",&student.mark);

    fp = fopen("studentfile.txt","a+"); /* check if the file was opened */
    if (fp == NULL)
        return;
    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");
    getchar();
}

void
printStudent()
{
    printf("\nThe record is found.\n");
    printf("\nID: %lld\nName: %s\nSurname: %s\nMark: %d \n",
        student.id,
        student.firstname,
        student.lastname,
        student.mark
    );
}

void
searchStudentByName()
{
    char  studentname[20];
    FILE *fp;
    int   found;
    int   matches;

    printf("\nEnter student first name: ");
    scanf("%19s", studentname);

    printf("Searching record with studentname=%s.\n", studentname);

    found = 0;
    fp    = fopen("studentfile.txt", "r");
    if (fp == NULL)
    {
        printf("IO error\n");
        return;
    }

    matches = fscanf(fp,"\n%lld\t%s\t%s\t%d\t", 
        &student.id, 
        student.firstname, 
        student.lastname, 
        &student.mark);

    do
    {
        matches = fscanf(fp,"\n%lld\t%s\t%s\t%d\t", 
            &student.id, 
            student.firstname, 
            student.lastname, 
            &student.mark);
        if (matches == 4)
            found = (strcmp(student.firstname, studentname));
    } while ((matches == 4) && (found == 0));
    if (found != 0)
        printStudent(); 
    else
        printf("Not found...\n");
    getchar();
}

void
searchStudentById()
{
    int   id;
    int   found;
    int   matches;
    FILE *fp;

    printf("\nEnter student first name: ");
    scanf("%d", &id);

    printf("Searching record with id=%d.\n", id);

    found = 0;
    fp    = fopen("studentfile.txt", "r");
    if (fp == NULL)
    {
        printf("IO error\n");
        return;
    }

    do
    {
        matches = fscanf(fp,"\n%lld\t%s\t%s\t%d\t", 
            &student.id, 
            student.firstname, 
            student.lastname, 
            &student.mark);
        if (matches == 4)
            found = (student.id == id);
    } while ((matches == 4) && (found == 0));
    if (found != 0)
        printStudent(); 
    else
        printf("Not found...\n");
    getchar();
}

int main()
{
    int choice;

    choice = 0;
    while (choice != 4)
    {
        printf("\n\tC PROGRAM OF STUDENT DATABASE SYSTEM"); 
        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:
            storeRecord();
            break;
        case 2:
            searchStudentByName();
            break;
        case 3:
            searchStudentById();
            break;
        }
    }
    return 0;
}

this is readable, and still has one more improvement regarding the DRY principle. Which you apparently ignore.

You don't need to declare student as a global.

This code is better than the previous one

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

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

void
storeRecord()
{
    FILE *fp;

    printf("\nEnter Student Details:\n\nID number: ");
    scanf("%lld",&student.id);

    printf("\nName:");
    scanf("%19s",student.firstname);

    printf("\nSurname:");
    scanf("%19s",student.lastname);

    printf("\nMark(0 - 100 integer) : ");
    scanf("%d",&student.mark);

    fp = fopen("studentfile.txt","a+"); /* check if the file was opened */
    if (fp == NULL)
        return;
    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");
    getchar();
}

int
compareStudentsById(struct Student lhs, struct Student rhs)
{
    return (lhs.id == rhs.id);
}

int
compareStudentsByName(struct Student lhs, struct Student rhs)
{
    return (strcmp(lhs.firstname, rhs.firstname) == 0);
}

void
printStudent()
{
    printf("\nThe record is found.\n");
    printf("\nID: %lld\nName: %s\nSurname: %s\nMark: %d \n",
        student.id,
        student.firstname,
        student.lastname,
        student.mark
    );
}

void
searchStudent(int(*compare)(struct Student,struct Student), const char *const name, int id)
{
    FILE *fp;
    int   found;
    int   matches;


    if (name != NULL)
        printf("Searching record with Name = %s.\n", name);
    if (id != -1)
        printf("Searching record with ID   = %d.\n", id);

    found = 0;
    fp    = fopen("studentfile.txt", "r");
    if (fp == NULL)
    {
        printf("IO error\n");
        return;
    }

    matches = fscanf(fp,"\n%lld\t%s\t%s\t%d\t", 
        &student.id, 
        student.firstname, 
        student.lastname, 
        &student.mark);

    do
    {
        struct Student other;

        if (name != NULL)
            strcpy(other.firstname, name);

        other.id = id;
        matches  = fscanf(fp,"\n%lld\t%s\t%s\t%d\t", 
            &student.id, 
            student.firstname, 
            student.lastname, 
            &student.mark);

        if (matches == 4)
            found = (compare(student, other) != 0);

    } while ((matches == 4) && (found == 0));

    if (found != 0)
        printStudent(); 
    else
        printf("Not found...\n");

    getchar();
}

void
searchStudentByName()
{
    char studentname[20];

    printf("\nEnter student first name: ");
    scanf("%19s", studentname);

    searchStudent(compareStudentsByName, studentname, -1);
}

void
searchStudentById()
{
    int id;

    printf("\nEnter student first name: ");
    scanf("%d", &id);

    searchStudent(compareStudentsByName, NULL, id);
}

int main()
{
    int choice;

    choice = 0;
    while (choice != 4)
    {
        printf("\n\tC PROGRAM OF STUDENT DATABASE SYSTEM"); 
        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:
            storeRecord();
            break;
        case 2:
            searchStudentByName();
            break;
        case 3:
            searchStudentById();
            break;
        }
    }
    return 0;
}
Iharob Al Asimi
  • 51,091
  • 5
  • 53
  • 91
  • Thank you, let me try. I guess it will work. – cprogramdatabase Dec 30 '14 at 19:10
  • `&studentname` is not wrong – Gopi Dec 30 '14 at 19:45
  • @Gopi explain. And do you always delete your downvoted/unaccepted answers? – Iharob Al Asimi Dec 30 '14 at 19:55
  • `printf("%p",studentname);` and `printf("%p",&studentname);` Check the link: http://stackoverflow.com/questions/5406935/reading-a-string-with-scanf – Gopi Dec 30 '14 at 19:57
  • I see, go [here](http://stackoverflow.com/q/27613485/1983495) please – Iharob Al Asimi Dec 30 '14 at 20:01
  • @iharob thanks for your holiday gift! but there is still some serious problems even with this code. [When I run this (click me)](http://i.imgur.com/szHY4eU.png) it compiles perfectly but after searching for students it won't find the exact true guy. – cprogramdatabase Dec 30 '14 at 20:13
  • sure, i see why. let me fix it. – Iharob Al Asimi Dec 30 '14 at 20:15
  • @iharob when you fix it can you notify me here, because I don't really understand when it fixes. I tried your edit (2 min ago) still finding wrong result. – cprogramdatabase Dec 30 '14 at 20:20
  • @cprogramdatabase now you see why is DRY principle so important, I fixed the `id` function and forgot the `name` function. – Iharob Al Asimi Dec 30 '14 at 20:38
  • @iharob when I try to compile it now these appeared I guess we're really near to the end. error C2039: 'studentname' : is not a member of '' error C2198: 'strcmp' : too few arguments for call (line 91) Nevermind, my bad. Your 2nd code works PERFECT! Thanks for your time and help. Happy new year! – cprogramdatabase Dec 30 '14 at 20:42