Thread: help to detect errors in this simple proggramme

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    4

    Unhappy help to detect errors in this simple proggramme

    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<string.h>
    struct customer readCustomer();
    struct slot readSlot();
    struct slot{int day,period;};
    struct customer{
    char name[50],tel[10], paidOrNot[8];};
    struct booking{
        char freeOrNot;//if not free, gives 'n'....
        struct slot slot_;
        struct customer customer_;
    };
    struct bookingFile{
         struct booking array[7][10];    
    };
    
    main(){
        FILE *fp;
        struct bookingFile bf; 
        struct slot s;
        fp=fopen("store.txt","r+");
        if(fp==NULL){
            printf("cannot open the file");
            exit(0);
        }
        fread(&bf,sizeof(struct bookingFile),1,fp);
    do{                //validating whether the given slot is free or not....
    
        s=readSlot();
        if(bf.array[s.day][s.period].freeOrNot=='n')
        printf("Sorry,the slot you gave has been already booked.Try another slot!\n");
    }while(bf.array[s.day][s.period].freeOrNot=='n');
    
    bf.array[s.day][s.period].freeOrNot=0;
    bf.array[s.day][s.period].slot_=s;
    bf.array[s.day][s.period].customer_=readCustomer();
    rewind(fp);
    fwrite(&bf,sizeof(struct bookingFile),1,fp)    ;
    rewind(fp);    
            printf("%s\n",bf.array[0][0].customer_.tel);//line a
            printf("%s\n",bf.array[1][1].customer_.tel);//line b
            printf("%s\n",bf.array[2][2].customer_.tel);//line c
            printf("%s\n",bf.array[3][3].customer_.tel);//line d
            fclose(fp);    
    }
    
    struct customer readCustomer(){
        struct customer c; 
    
        printf("Enter your name: ");
        gets(c.name);
        printf("Enter the telephone number: ");
        gets(c.tel);
        printf("Are you paying ,just now?(Yes/No):");
        gets(c.paidOrNot);
    
        return c;
    }
    struct slot readSlot(){
        struct slot s;
        printf("Enter the number of day you want\n1.mon\n2.tues\n3.wed\n4.thu\n5.fri\n6.sat\n7.sun: ");
        scanf("%i",&s.day);
        printf("Enter the number of period you want\n1.8-9\n2.9-10\n3.10-11\n4.11-12\n5.12-1\n6.1-2\n7.2-3\n8.3-4\n9.4-5\n10.5-6: ");
        scanf("%i",&s.period);
        return s;
    }

    This code compiles fine.But
    the data I tried to save to the file,' store.txt' are not permanantly stored. i could assert it by running this code block several times(more than 4), by the output produced by line a,b,c&d.
    Can you plese help me to detect the reasons for why it doesn't get stored them permanantly.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Start by fixing all compiler errors/warnings:
    Code:
    $ make slot
    gcc -Wall -ggdb3 -std=c99 -O0 -o slot slot.c -lm -lpthread -lrt
    slot.c:18:1: warning: return type defaults to ‘int’ [enabled by default]
    slot.c: In function ‘main’:
    slot.c:25:5: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]
    slot.c:25:5: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
    Declare main to explicitly return an int. It's also a good idea if you also explicitly return an int at the end of main (though it's not required with C99+). Also, if you wish to use the exit() function, you need to #include <stdlib.h>

    Then, it would be helpful if you actually provided us with a sample store.txt file, since without it the program wont run. You should provide us the exact file you are using that causes the problem, so we can best help you find that problem. We don't want to play a guessing game with the format of that file, and what, if any, typos or formatting mistakes you may have in your file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tell me the errors of this simple phone book ?
    By blogchama in forum C Programming
    Replies: 5
    Last Post: 01-16-2010, 05:42 AM
  2. Few Simple Program Errors
    By pobri19 in forum C Programming
    Replies: 3
    Last Post: 05-22-2008, 06:12 PM
  3. Simple Shift errors...
    By Junior89 in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2007, 05:55 AM
  4. Simple Windows program errors
    By Kespoosh in forum Windows Programming
    Replies: 2
    Last Post: 07-17-2004, 12:05 AM
  5. Unknown Errors in simple program
    By neandrake in forum C++ Programming
    Replies: 16
    Last Post: 04-06-2004, 02:57 PM

Tags for this Thread