Thread: How to improve this program

  1. #1
    Registered User
    Join Date
    Aug 2015
    Location
    India
    Posts
    14

    Question How to improve this program

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    struct student
    {
        char name[50];
        int roll;
        char addrs [100];
    };
    main()
    {
        struct student stu;
        FILE *fp;
        int ch;
        char c;
        char sn[50];
        printf("\n\t\t\t\tMENU:");
        printf("\n1. Update Record in the file");
        printf("\n2. Search by Name");
        printf("\n3. Exit");
        printf("\nEnter Choice: ");
        scanf("%d", &ch);
    
    
        switch(ch)
        {
    
    
        case 1:
            fp=fopen("student.txt", "ab");
            while (1)
            {
            printf("\nEnter Name: ");
            scanf("%s", stu.name);
            fflush(stdin);
            printf("\nEnter Roll No.: ");
            scanf("%d", &stu.roll);
            fflush(stdin);
            printf("\nEnter Address: ");
            scanf("%s", stu.addrs);
            fflush(stdin);
            fwrite(&stu,sizeof(stu),1,fp);
            printf("\nDo you want to continue?(Y/N): ");
            scanf("%c", &c);
            if(c=='n'||c=='N')
            {
                break;
            }
            }
            fclose(fp);
            break;
        case 2:
            fp=fopen("student.txt", "rb");
            printf("\nEnter Name: ");
            gets(sn);
            while(fread(&stu,sizeof(stu),1,fp))
            {
            if(strcmp(sn,&stu.name)==0)
            {
                printf("\n\t%s \t %d \t %s", stu.name, stu.roll, stu.addrs);
                getch();
                break;
            }
            }
            fclose(fp);
            getch();
            break;
        case 3:
            exit(1);
        }
    }
    What do i want ?
    1. I want to store Name with space for example - Seth Rollins
    2. I want to store Address with house number (integer value) and string with space for example - 234, Texas

    Will somebody help me to modify this code with fgets function and i also do not want to use fflush function as it is not portable but without this function my code doesn't work properly.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > How to improve this program
    1. Stop including conio.h

    2. make main explicitly return an int.

    3. fp=fopen("student.txt", "ab");
    Don't name binary files with a .txt extension. If you try to load it into a text editor, prepare for disappointment.

    4. fflush(stdin);
    Stop using fflush(stdin)

    5. gets(sn);
    NEVER use gets() !!!
    Seriously, just forget you ever knew about it.

    6. Your indentation could be better.
    https://en.wikipedia.org/wiki/Indent_style


    > Will somebody help me to modify this code with fgets function and i also do not want to use fflush function as it is not portable
    We did all this in your previous thread.
    problem with fgets
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I improve this program?
    By WHOLEGRAIN in forum C++ Programming
    Replies: 14
    Last Post: 03-06-2011, 02:53 AM
  2. Replies: 2
    Last Post: 05-02-2010, 01:49 AM
  3. How can I improve this?
    By Raigne in forum C++ Programming
    Replies: 2
    Last Post: 04-19-2008, 01:30 AM
  4. help to improve program efficiency and speed
    By godhand in forum C Programming
    Replies: 11
    Last Post: 10-19-2003, 04:25 PM
  5. improve program.
    By emperor in forum C Programming
    Replies: 5
    Last Post: 01-05-2003, 01:34 AM

Tags for this Thread