Thread: need help in finding the error in my code

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    18

    need help in finding the error in my code

    record typedef doesnt work for some reason..
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define TRUE 1
    FILE *fstud;
    FILE *fteacher;
    record readscreen1(record stud);
    record readscreen2(record teacher);
    void writefile1(record stud);
    void writefile2(record teacher);
    
    typedef struct {
        int id;
        int psswrd;
    } idpass;
    
    typedef struct {
        char ders;
        int mark;
        char ogrt_yorum;
    } lesson_reprt;
    
    typedef struct {
        int month;
        int day;
        int year;
    } date;
    
    typedef struct {
        char name[30];
        char mahalle[30];
        char city[20];
        int tc_no;
        idpass info;
        date kayit;
    } record;
    
    
    int main()
    {
        record stud,teacher;
        int n;
        printf("Ogrenci [1]/Ogretmen[2]");
        scanf("%d",&n);
        switch (n)
        {
            case 1:
                fstud=fopen("studrecords.dat","w+");
                stud=readscreen1(stud);
                writefile(stud);
                fclose(fstud);
                break;
            case 2:
                fteacher=fopen("teacherrecords.dat","w+");
                teacher=readscreen2(teacher);
                writefile(teacher);
                fclose(fteacher);
                break;
        }
        return 0;
    }
    record readscreen1(record stud)
    {
        printf("Tarih(gg/aa/yyyy: ");
        scanf("%d/%d/%d",&stud.kayit.day,&stud.kayit.month,&stud.kayit.year);
        printf("Isim: ");
        scanf("%s",&stud.name);
        printf("T.C. numarasi: ");
        scanf("%d",&stud.tc_no);
        printf("Sehir: ");
        scanf("%s",&stud.city);
        printf("Mahalle: ");
        scanf("%s",&stud.mahalle);
        printf("ID: ");
        scanf("%d",&stud.info.id);
        printf("Parolla: ");
        scanf("%d",&stud.info.psswrd);
        return (stud);
    }
    
    record readscreen2(record teacher)
    {
        printf("Tarih(gg/aa/yyyy: ");
        scanf("%d/%d/%d",&teacher.kayit.day,&teacher.kayit.month,&teacher.kayit.year);
        printf("Isim: ");
        scanf("%s",&teacher.name);
        printf("T.C. numarasi: ");
        scanf("%d",&teacher.tc_no);
        printf("Sehir: ");
        scanf("%s",&teacher.city);
        printf("Mahalle: ");
        scanf("%s",&teacher.mahalle);
        printf("ID: ");
        scanf("%d",&teacher.info.id);
        printf("Parolla: ");
        scanf("%d",&teacher.info.psswrd);
        return (teacher);
    }
    
    void writefile1(record stud)
    {
        fprintf(fstud,"%d/%d/%d\n",stud.kayit.day,stud.kayit.month,stud.kayit.year);
        fprintf(fstud,"Isim: %s\n",stud.name);
        fprintf(fstud,"T.C. No: %d\n",stud.tc_no);
        fprintf(fstud,"Sehir: %s",stud.city);
        fprintf(fstud,"Mahalle: %s",stud.mahalle);
        fprintf(fstud,"ID: %d",stud.info.id);
        fprintf(fstud,"Parolla: %d",stud.info.psswrd);
        return;
    }
    
    void writefile2(record teacher)
    {
        fprintf(fteacher,"%d/%d/%d\n",teacher.kayit.day,teacher.kayit.month,teacher.kayit.year);
        fprintf(fteacher,"Isim: %s\n",teacher.name);
        fprintf(fteacher,"T.C. No: %d\n",teacher.tc_no);
        fprintf(fteacher,"Sehir: %s",teacher.city);
        fprintf(fteacher,"Mahalle: %s",teacher.mahalle);
        fprintf(fteacher,"ID: %d",teacher.info.id);
        fprintf(fteacher,"Parolla: %d",teacher.info.psswrd);
        return;
    }

  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
    Move the function prototypes to be after the typedefs.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    it worked.thank you very much.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    18

    need more help

    this time theres something wrong in the isdatevalid fuction..
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define TRUE 1
    #define isleapyear(year) ((!(year % 4) && (year % 100)) || (!(year % 400) && (year % 1000)))
    FILE *fstud;
    FILE *fteacher;
    
    typedef struct {
        int id;
        int psswrd;
    } idpass;
    
    typedef struct {
        char ders;
        int mark;
        char ogrt_yorum;
    } lesson_reprt;
    
    typedef struct {
        int month;
        int day;
        int year;
    } date;
    
    typedef struct {
        char name[30];
        char mahalle[30];
        char city[20];
        int tc_no;
        idpass info;
        date kayit;
    } record;
    
    typedef struct {
        int stud_no;
        char stud_bolum;
        lesson_reprt ders[7];
    } stud_info;
    
    record readscreen1(record stud);
    record readscreen2(record teacher);
    void writefile1(record stud);
    void writefile2(record teacher);
    int isdatevalid(int day,int month,int year);
    
    int main()
    {
        record stud,teacher;
        int n;
        printf("Ogrenci [1]/Ogretmen[2]");
        scanf("%d",&n);
        switch (n)
        {
            case 1:
                fstud=fopen("studrecords.txt","w+");
                stud=readscreen1(stud);
                writefile1(stud);
                fclose(fstud);
                break;
            case 2:
                fteacher=fopen("teacherrecords.txt","w+");
                teacher=readscreen2(teacher);
                writefile2(teacher);
                fclose(fteacher);
                break;
        }
    
    }
    record readscreen1(record stud)
    {
        beginning1:
        printf("Tarih(gg/aa/yyyy): ");
        scanf("%d/%d/%d",&stud.kayit.day,&stud.kayit.month,&stud.kayit.year);
        if(isdatevalid(stud.kayit.day, stud.kayit.month, stud.kayit.year))
        {
            printf("Isim: ");
            scanf("%s",&stud.name[30]);
            printf("T.C. numarasi: ");
            scanf("%d",&stud.tc_no);
            printf("Sehir: ");
            scanf("%s",&stud.city[20]);
            printf("Mahalle: ");
            scanf("%s",&stud.mahalle[30]);
            printf("ID: ");
            scanf("%d",&stud.info.id);
            printf("Parolla: ");
            scanf("%d",&stud.info.psswrd);
        }
        else
        {
            printf("Girdiginiz tarih gecerli degil.");
            goto beginning1;
        }
        return (stud);
    }
    
    record readscreen2(record teacher)
    {
        beginning2:
        printf("Tarih(gg/aa/yyyy: ");
        scanf("%d/%d/%d",&teacher.kayit.day,&teacher.kayit.month,&teacher.kayit.year);
        if(isdatevalid(teacher.kayit.day,teacher.kayit.month,teacher.kayit.year))
        {
            printf("Isim: ");
            scanf("%s",&teacher.name[30]);
            printf("T.C. numarasi: ");
            scanf("%d",&teacher.tc_no);
            printf("Sehir: ");
            scanf("%s",&teacher.city[20]);
            printf("Mahalle: ");
            scanf("%s",&teacher.mahalle[30]);
            printf("ID: ");
            scanf("%d",&teacher.info.id);
            printf("Parolla: ");
            scanf("%d",&teacher.info.psswrd);
        }
        else
        {
            printf("Girdiginiz tarih gecerli degil.");
            goto beginning2;
        return (teacher);
    }
    
    void writefile1(record stud)
    {
        fprintf(fstud,"%d/%d/%d\n",stud.kayit.day,stud.kayit.month,stud.kayit.year);
        fprintf(fstud,"Isim: %s\n",stud.name);
        fprintf(fstud,"T.C. No: %d\n",stud.tc_no);
        fprintf(fstud,"Sehir: %s\n",stud.city);
        fprintf(fstud,"Mahalle: %s\n",stud.mahalle);
        fprintf(fstud,"ID: %d\n",stud.info.id);
        fprintf(fstud,"Parolla: %d\n",stud.info.psswrd);
        return;
    }
    
    void writefile2(record teacher)
    {
        fprintf(fteacher,"%d/%d/%d\n",teacher.kayit.day,teacher.kayit.month,teacher.kayit.year);
        fprintf(fteacher,"Isim: %s\n",teacher.name);
        fprintf(fteacher,"T.C. No: %d\n",teacher.tc_no);
        fprintf(fteacher,"Sehir: %s\n",teacher.city);
        fprintf(fteacher,"Mahalle: %s\n",teacher.mahalle);
        fprintf(fteacher,"ID: %d\n",teacher.info.id);
        fprintf(fteacher,"Parolla: %d\n",teacher.info.psswrd);
        return;
    }
    
    int isdatevalid(int day,int month,int year)
    {
      if (day <= 0) return 0 ;
      switch( month )
        {
            case 1  :
            case 3  :
            case 5  :
            case 7  :
            case 8  :
            case 10 :
            case 12 : if (day > 31) return 0 ; else return 1 ;
            case 4  :
            case 6  :
            case 9  :
            case 11 : if (day > 30) return 0 ; else return 1 ;
            case 2  :
             if ( day > 29 ) return 0 ;
             if ( day < 29 ) return 1 ;
             if (isleapyear(year)) return 1 ;   // leap year
             else return 0 ;
         }
         return 0 ;
        }
        return 0;
    }

  5. #5
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    What's the error you get ?
    Please post it so we know what's going wrong with the function.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    readscreen2 is MISSING closing "}" so the functions after it are inside that function.

    If you use a good indentation style this would stand out.

    Tim S.

    PS: Using "goto" is normally considered bad style.
    Last edited by stahta01; 12-23-2011 at 04:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with finding maya converter to C code
    By kazuar in forum C Programming
    Replies: 2
    Last Post: 12-22-2009, 08:47 AM
  2. Help finding the error
    By C++Noob316 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2009, 01:16 PM
  3. need help finding where the problem is for this code
    By edmorgan in forum C++ Programming
    Replies: 3
    Last Post: 06-18-2007, 12:41 PM
  4. Need help finding error
    By chorney in forum C++ Programming
    Replies: 13
    Last Post: 01-15-2007, 12:16 AM
  5. Finding object code
    By kidburla in forum C Programming
    Replies: 3
    Last Post: 11-29-2005, 01:09 PM