Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

solve this programing error

0 votes
asked May 30, 2021 by Aman Bhatti (120 points)
#include<stdio.h>
#include<string.h>
struct student
{
    char name[20],city[20];
    int fees;
    
}
 int main()
 {
     student p;
     int ch;
     FILE *fp;
     char mm[20];
     do{
         fp=fopen("aman","rb+");
         if(fp == null)
{
    fp=fopen("aman","wb+");
    
}

printf("\n 1add record \n 2 list \n 3 search \n");

         printf("enter the choice ");
         scanf("%d",&ch);
         switch(ch)
         {
             case 1:
             printf("enter the name city and fees");
             scanf("%s%s%d"&p.name,&p.city,&p.fees);
             fseek(fp,0,2);
             fwrite((char*)&p,sizeof(p),1,fp);
             break;
             case 2:
             printf("\n name \t city\t fees");
             rewind(fp);
             while(fread(char*)&p,sizeof(p),1,fp));
             {
                 printf("\n %s\t%s\t%d",p.name,p.city,p.fees);
                 
             }
             fclose(fp);
             break;
             case 3:
             printf("enter the serach ");
             scanf("%s",&mm);
             printf("\n name \t city \t fees");
             rewind(fp);
             while(fread(char*)&p,sizeof(p),fp)
             {
                 if strcmp(mm,p.name)==0)
                  printf("\n %s\t%s\t%d",p.name,p.city,p.fees);
                 
             }
             
         }
     }
     return 0;
     
 }while(ch!=4);
 }

1 Answer

+1 vote
answered Oct 10, 2021 by Peter Minarik (84,720 points)
edited Oct 11, 2021 by Peter Minarik

If you compile your code ("RUN" button) it tells you where are any compilation problems and what the issue is.

Your code has so many problems that listing all of them and proposing a fix one by one is very inconvenient. Instead, I rewrote your code and provide you with a generic list of problems (probably I will miss a few of the issues).

List of problems

  1. mismatched curly braces
  2. mismatched parenthesis
  3. missing comma
  4. address of (&) operator used when not needed
  5. extra semicolon at the end of the line for loops (it makes the loop do nothing)
  6. mismatched fopen()/fclose()
  7. return in the loop (exiting early, it should have been at the end of the main())
  8. Using numeric literals instead of the defined values (using 2 instead of SEEK_END)
  9. The user experience is not too good either. You should tell what you expect of the user (e.g. didn't specify how the search works)
  10. You should use functions to better structure your code.

A proposed fix

Note: I didn't spend time to validate user input as it would have added even more work.

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

#define NAME_WIDTH 20
#define CITY_WIDTH 20
#define FEES_WIDTH 10

typedef struct
{
    char name[NAME_WIDTH];
    char city[CITY_WIDTH];
    int fees;
} Student;

FILE * OpenFile(const char * fileName)
{
    FILE * file = fopen(fileName, "rb+");
    if (file == NULL)
        file = fopen(fileName,"wb+");
        
    return file;
}

void PrintHeader()
{
    printf("\n");
    printf("%-*s%-*s%-*s\n", NAME_WIDTH, "Name", CITY_WIDTH, "City", FEES_WIDTH, "Fees");
    for (int i = 0; i < NAME_WIDTH + CITY_WIDTH + FEES_WIDTH; i++)
        printf("-");
    
    printf("\n");
}

void PrintFooter(size_t counter)
{
    printf("\n");
    if (counter == 0)
        printf("-- No record found. --\n");
    else
        printf("-- %lu records found. --\n", counter);
    printf("\n\n");
}

void PrintStudent(const Student * student)
{
    printf("%-*s%-*s%-*d\n", NAME_WIDTH, student->name, CITY_WIDTH, student->city, FEES_WIDTH, student->fees);
}

int GetChoice()
{
    int choice;
    printf("\n");
    printf("Please, select an operation\n");
    printf("---------------------------\n");
    printf(" 1) add new record\n");
    printf(" 2) list record\n");
    printf(" 3) search by name\n");
    printf(" 4) exit\n");
    printf("? ");
    scanf("%d", &choice);
    return choice;
}

void AddNewRecord(FILE * file)
{
    printf("\nPlease, enter a record: ([name] [city] [fees]): ");
    Student student = { };
    scanf("%s %s %d", student.name, student.city, &student.fees);
    fseek(file, 0, SEEK_END);
    fwrite((char*)&student, sizeof(student), 1, file);
}

void ListRecords(FILE * file)
{
    rewind(file);
    Student student;
    size_t counter = 0;
    while (fread((char*)&student, sizeof(student), 1, file))
    {
        if (counter == 0)
            PrintHeader();

        PrintStudent(&student);
        counter++;
    }
    PrintFooter(counter);
}

void SearchRecords(FILE * file)
{
    printf("\nPlease, enter the [name] to look for: ");
    char name[NAME_WIDTH];
    scanf("%s", name);
    rewind(file);
    Student student;
    size_t counter = 0;
    while (fread((char*)&student, sizeof(student), 1, file))
    {
        if (strcmp(name, student.name) == 0)
        {
            PrintHeader();
            PrintStudent(&student);
            counter++;
            // assuming multiple students with the same name exists. If not, we should break here.
        }
    }
    PrintFooter(counter);
}

int main()
{
    const char * fileName = "aman";
    FILE * file = OpenFile(fileName);
    if (file == NULL)
    {
        fprintf(stderr, "Failed to open file: %s\n", fileName);
        return -1;
    }

    int choice;
    do
    {
        choice = GetChoice();
        switch (choice)
        {
            case 1: AddNewRecord(file); break;
            case 2: ListRecords(file); break;
            case 3: SearchRecords(file); break;
        }
    } while (choice != 4);
    
    fclose(file);
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...