Thread: Don;t know how to fix

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    2

    Don;t know how to fix

    Code:
    #include<stdio.h>#include<stdlib.h>
    #include<time.h>
    #include<string.h>
    //Ham in ra cac bien cua strung bt theo lenh in nhung den truoc khi ghi lai vao file thi cac bien bi dinh voi nhau
    // va 1 so bien bi mat gia tri
    
    
     typedef struct {
        char id[14];
        char pincode[6];
        char name[50];
        char date[26];
        int status;
        float sodu;
    }thechinh;
    void line();
    void hienthi();
    void taomathe();
    void taopincode();
    int ngaythang();
    void uppername();
    void nhapten();
    thechinh makeCard();
    FILE *f;
    char path[30];
    int main()
    {
        int i,chon,len=0;
        thechinh listcard[100];
        fflush(stdin);
        char filename[100];
        do
        {
            chon = Menu();
            switch(chon)
            {
                case 1:
                    system("cls");
                    listcard[len] = makeCard();
                    len++;
                    break;
                case 2:
                    changeInfo();
                    break;
                default:
                    break;
            }
        }while(chon!=3);
    }
    
    
    int Menu()
    {
        fflush(stdin);
        char *MenuItem[] = {"1. Tao moi ma the","2. Cap nhat thong tin chu the","3. Thoat"};
        int i,choice;
        for(i=0;i<2;i++)
        {
            printf("%s\n",MenuItem[i]);
        }
        printf("Chon chuc nang can su dung: ");
        fflush(stdin);
        scanf("%d",&choice);
        return choice;
    }
    
    
    thechinh makeCard()
    {
        int i;
        char chon,file[15];
        thechinh mycard;
        mycard.sodu = 1000000;
        mycard.status = 1;
        fflush(stdin);
        printf("=====Chuong trinh tao moi ma the ATM=====");
        printf("\nNhap vao ho va ten cua chu the");
        ngaythang(mycard.date);
        nhapten(mycard.name);
        system("cls");
        line();
        printf("Ho va ten: %s",mycard.name);
        taomathe(mycard.id);
        line();
        printf("ID card : ");
        printf("%s",mycard.id);
        line();
        taopincode(mycard.pincode);
        printf("Pincode :");
        printf("%s",mycard.pincode);
        line();
        printf("So du : %.2f  VND",mycard.sodu);
        line();
        //In here have problem variable mycard.id change value to combined mycard.id and mycard.pincode
    // mycard.name change value to NULL
        printf("%s",mycard.id);
        printf("+%-25s+\n", "-------------------------");/*
        char filename[30], dat[4]=".dat", tempFile[15], pathName[] = "Accounts\\",id[15];
        strcpy(filename,mycard.id);
        strcat(filename,dat);
        strcat(pathName,filename);*/
        f = fopen("213.dat","wb");
        fwrite(&mycard, sizeof(thechinh), 1, f);
        fclose(f);
    }
    void doitenfile(char *a,char *b)
    {
        strcat(a,b);
    }
    
    
    changeInfo()
    {
        printf("Test");
    }
    
    
    //Ham su dung de tao ma the su dung 3 chuoi de ghep lai do moi lan chi tao ra dc 5 so random
    void taomathe(char *ID)
    {
        char code2[5], code3[5], code4[4], realcode[15];
        int num1, num2, num3, num4;
        srand(time(NULL));
        //lay random 5 so
        num1 = rand()%10001+32768;
        num2 = rand()%10001+32768;
        //lay random 4 so
        num3 = rand()%100+999;
        //chuyen doi tu kieu int thanh string
        itoa(num3,code4,10);
        itoa(num2,code3,10);
        itoa(num1,code2,10);
        //ghep cac chuoi lai cho du 14 so
        strcpy(realcode,code2);
        strcat(realcode,code3);
        strcat(realcode,code4);
        strcpy(ID,realcode);
    }
    
    
    
    
    //Ham su dung de tao pincode ghep 2 chuoi do 1 lan chi tao ra dc 3 so random
    void taopincode(char *pass)
    {
        int num1, num2, num3;
        char code1[5], code2[1], ranPW[6];
        srand(time(0));
        num1 = rand() % 10000+9999;
        num2 = rand() % 10;
        itoa(num2,code2,10);
        itoa(num1,code1,10);      
        strcpy(ranPW,code1);
        strncat(ranPW,code2,1);     
        strcpy(pass, ranPW);
        
    }
    //Ham lay ra ngay thang
    int ngaythang(char *p)
    {
        time_t time_raw_format;
        time (&time_raw_format);
        //ctime lay ra ngay thang hien tai tra ve theo kieu string
        strcpy(p,ctime(&time_raw_format));
    }
    
    
    void line()
    {
        printf("\n+%-25s+\n", "-------------------------");
    }
    //Ham de nhap ten
    void nhapten(char ten[50])
    {
        int i;
        fflush(stdin);
        gets(ten);
        for(i=0;i<5;i++)
        {
            uppername(ten);
        }
    
    
    }
    //Doi thanh chu hoa
    void uppername(char name_arr[])
    {
        int x;
        for(x=0;name_arr[x] != '\0';x++)
        {
            if(name_arr[x]>=97 && name_arr[x]<122)
            {
                name_arr[x]=toupper(name_arr[x]);
            }
        }    
    }
    anyone can help my
    my code have some problem i can k'now how to fix this
    i comnet in code where have problem
    sr for my English

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Regarding the problem you're asking about - it appears to be caused a buffer overrun.

    - You generate a string for "id" in the function "taomathe()".
    - You then generate a string for "pincode" in the function "taopincode()".

    These are stored as strings in separate character arrays within your structure. However, you're only declaring "id" to have 14 characters (when used as a string, this gives you 13 characters plus one null character).

    But it appears that the "taomathe()" function, the resulting string is greater than the space you allow for it. Therefore, the null character gets overwritten. "printf()" keeps printing characters until a null character is reached. Therefore, it will print all of "id" and keep going through "pincode" (the next characters in memory) until it reaches the null character at the end of "pincode."

    The solution would be to give more space to the "id" array (such as "id[24]").

    ----------------

    Some other things to consider:

    - Don't use "fflush(stdin)" - it can result in undefined behavior. (FAQ > Why fflush(stdin) is wrong - Cprogramming.com)
    - Don't use "gets()" - it is unsafe and obsolete. Use "fgets()" as a safer alternative. (FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com)
    - When opening a file, check that it opened successfully before writing to it.

    [edit]

    Oh yes, and be sure to heed all warnings from your compiler. This is what I got when I compiled your code.

    Code:
    main.c||In function 'main':|
    main.c|35|warning: implicit declaration of function 'Menu'|
    main.c|44|warning: implicit declaration of function 'changeInfo'|
    main.c|32|warning: unused variable 'filename'|
    main.c|29|warning: unused variable 'i'|
    main.c||In function 'makeCard':|
    main.c|72|warning: unused variable 'file'|
    main.c|72|warning: unused variable 'chon'|
    main.c|71|warning: unused variable 'i'|
    main.c|114|warning: return type defaults to 'int'|
    main.c||In function 'taomathe':|
    main.c|123|warning: unused variable 'num4'|
    main.c||In function 'taopincode':|
    main.c|147|warning: unused variable 'num3'|
    main.c||In function 'uppername':|
    main.c|194|warning: implicit declaration of function 'toupper'|
    main.c||In function 'ngaythang':|
    main.c|166|warning: control reaches end of non-void function|
    main.c||In function 'changeInfo':|
    main.c|116|warning: control reaches end of non-void function|
    main.c||In function 'makeCard':|
    main.c|106|warning: control reaches end of non-void function|
    main.c||In function 'main':|
    main.c|50|warning: control reaches end of non-void function|
    ||=== Build finished: 0 errors, 15 warnings ===|
    Last edited by Matticus; 01-14-2013 at 11:27 AM.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    2
    thanks for you help
    i use Dev-C++5.3.0.4 to compiler but i cant see any warring

Popular pages Recent additions subscribe to a feed