3

The following program accepts data from user of student name , last name and score. The function search_lastname is supposed to search records of given last name. But it gives segmentation fault. The other function is to print the records and the cases for switch have been created for further functions that are yet to be added to the program .

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char **first_name, **last_name;
float *score;
int entry;
void print();
void search_lastname(char search_last_name[21]);
int main()
{
int i,option;
char search_last_name[21];
do
    {
    printf("\nPlease indicate the number of records you want to enter (The minimum number of entries is 5) :");                     
    scanf("%d",&entry);
    }
while(entry<=4);
score=(float*)malloc(entry*sizeof(float));
first_name=(char**)malloc(entry*sizeof(char*));
last_name=(char**)malloc(entry*sizeof(char*));
for(i=0;i<entry;i++)
    {
    first_name[i]=(char*)malloc(21*sizeof(char));
    last_name[i]=(char*)malloc(21*sizeof(char));
    }
printf("Please input records of students (enter a new line after each record), with following format first name last name score \n");
for(i=0;i<entry;i++)
    {
    scanf("%s%s%f",&first_name[i],&last_name[i],&score[i]);                                                             /*Input of records itself*/
    }
do
{
    printf("\nPlease choose the appropriate options :");                                                /*Switch statement for choosing options.*/
    printf("\nPrint records (press 1)\nAdd a new record (press 2)\nDelete records (press 3)\nSearch by last name (press 4)\nSort by score (press 5)\nSort by last name (press 6)\nFind Median score (press 7)\nExit the Program (press 0)");
    scanf("%d",&option);
    switch(option)
        {
        case 1  :print();
            break;
        case 2  :
            break;
        case 3  :
            break;
        case 4  :
            printf("\n Please enter the last name you want to search: ");
            scanf("%s",search_last_name);
            search_lastname(search_last_name);
            break;
        case 5  :
            break;
        case 6  :
            break;
        case 7  :
            break;
        case 0  :
            break;
        default : 
            printf("\n Please enter a valid option.");
        }
}
while(option>=1 && option<=7);
return 0;
}

void print()
{
int i;
printf("\n The records of students are as follows :");
for(i=0;i<entry;i++)
    {
    printf("\nFirst name:%s, Last name:%s, Score:%f\n",&first_name[i],&last_name[i],score[i]);
    }
}


void search_lastname(char search_last_name[21])                                     /*Funtion to search by last name*/  
{
int i,counter=0;
for(i=0;i<entry;i++)
    {
    if(strcmp(search_last_name,last_name[i])==0)
        {
        printf("\nFirst name:%s, Last name:%s, Score:%f\n",first_name[i],last_name[i],score[i]);
        }
    }

}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

1 Answers1

2

your problem is with the usage of scanf() function. You need to change

scanf("%s%s%f",&first_name[i],&last_name[i],&score[i]);

to

scanf("%20s %20s %f", first_name[i], last_name[i], &score[i]);

Without the length modifier, your %s specifier won't be able to seperate the input strings and thus will be over-writing the memory area past the allocated memory, causing undefined behavior.

Also, it's strongly recommended that you check the return value of scanf() to ensure proper input.

Note:

  1. The recommended signature of main() is int main(void).
  2. Do not cast the return value of malloc() and family.
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261