Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

A function in my code is not working properly

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>   
#define I 27
#define DosyaAdi "test.txt"

struct  müşteri
{
    char iban[I];
    long long tc;
    int bakiye;
};

int ibankontrol(char iban[])
{
    if(!isalpha(iban[0]) || !isalpha(iban[1]))
        return 0;
    for (int i = 2; i < 27; i++)
    {
        if (iban[i]<48 || iban[i]>57)
        {
            return 0;
        }
    }
    return 1;
}

int tckontrol(long long tc)
{
    int a=0,bas;
    long long i=1;
    while (a<11)
    {
        bas=(tc/i)%10;
        if (bas<0 || bas>9)
        {
            return 0;
        }
        
        i*=10;
        a++;
    }
    if (tc / i > 0) {
        return 0;
    }
    return 1;
}

int kaydet(struct müşteri m)
{
    FILE *dosya;
    dosya=fopen(DosyaAdi,"a");
    if (dosya==NULL)
    {
        printf("\nKayıt başarısız\n");
        return 0;
    }
    
    fprintf(dosya,"%lld %d %s\n",m.tc,m.bakiye,m.iban);

    fclose(dosya);
    return 1;
}

int havale(char kaynak[],char hedef[],int miktar)
{
    struct müşteri müşteriler[100];
    int i=0,y=0,z=0;
    char string[100];
    FILE *dosya;
    dosya=fopen(DosyaAdi,"r");
    while (1)
    {
        if ((fgets(string,100,dosya))==NULL)
        {
            break;
        }
        sscanf(string,"%lld %d %s",&müşteriler[i].tc,&müşteriler[i].bakiye,müşteriler[i].iban);
        i++;
    }   

    for(z=0;z<i;z++)
    {
        printf("\nGuncel bilgiler:\n");
        printf("%d: %s  %lld  %d\n",z+1,müşteriler[z].iban,müşteriler[z].tc,müşteriler[z].bakiye);
    }

    fclose(dosya);
    for (int j = 0; j < i; j++)
    {
        if (strcmp(kaynak,müşteriler[j].iban) == 0)
        {
            müşteriler[j].bakiye -= miktar;
            y++;
        }
        if (strcmp(hedef,müşteriler[j].iban) == 0)
        {
            müşteriler[j].bakiye += miktar;
            y++;
        }
    }

    if (y != 2)
    {
        return 0;
    }
    
    dosya = fopen(DosyaAdi, "w");
    if (dosya == NULL)
    {
        printf("Dosya açılamadı.\n");
        return 0;
    }
    
    for (int s = 0; s < i; s++)
    {
        fprintf(dosya, "%lld %d %s", müşteriler[s].tc, müşteriler[s].bakiye, müşteriler[s].iban);
        if (s!=i-1)
        {
            printf("\n");
        }
        
    }
    fclose(dosya);
    
    return 1;
}


int main()
{
    int seçim;
    int dönüş=0,kontrol=0;
    char kaynak[27],hedef[27];
    int miktar=0;
    struct müşteri m;

    while (1)
    {
        printf("Menü:\n");
        printf("1:Kayıt\n");
        printf("2:Havale\n");
        printf("3:Çıkış\n");
        printf("Seçiminiz: ");
        scanf("%d",&seçim);

        switch (seçim)
        {
        case 1:
       
            while (1)
            {
                printf("İban giriniz: ");
                scanf("%s",m.iban);
                fflush(stdin);
                dönüş=ibankontrol(m.iban);
                if (dönüş==0)
                {
                    printf("İban yanlış");
                }
                else if (dönüş==1)
                {
                    break;
                }
            }
            while (1)
            {
                printf("TC giriniz");
                scanf("%lld",&m.tc);
                dönüş=tckontrol(m.tc);
                if (dönüş==0)
                {
                    printf("TC yanlış");
                }
                else if (dönüş==1)
                {
                    break;
                }
            }
            
            printf("Bakiye giriniz");
            scanf("%d",&m.bakiye);

            kontrol=kaydet(m);
            if (kontrol==0)
            {
                printf("Kayıt başarısız");
            }
            else if (kontrol==1)
            {
                break;
            }
            break;
        case 2:
            printf("Kaynak iban: ");
            scanf("%s",kaynak);
             
            fflush(stdin);
            
            printf("Hedef iban: ");
            scanf("%s",hedef);
   
            fflush(stdin);

            printf("Gönderilicek miktar: ");
            scanf("%d",&miktar);

            int a=havale(kaynak,hedef,miktar);
            if (a==0)
            {
                printf("Havale başarısız\n");
            }
            break;
        case 3:
            exit(2);
            break;
        default:
            printf("geçersiz işlem");
            break;
        }
    }
    return 0;
}

In my code, as information is entered, I process it into the file, and then, when making the transfer, I delete the file and write it back to the file after the transaction. While the registration process is performed properly in my code, the transfer transaction does not occur properly. I think it is a whitespace character, but what do you think?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Printf a \n for the last line too, and scanf with a newline. Also you do not check that tc could be negative.

And IBAN does not reserve room for a terminating '\0', neither is there a check up on termination, like strlen. So printf and scanf will err.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading