0

I have bug or error with strcpy. Everytime I goback to local label it changes "namalengkap" variables even though strcpy wasn't use for that variables. The code and output will give more explanation:

int main()
{
    FILE *fp;
    char test='T',check3rd,optreg='T',cekbingung[1000][100];
    char namalengkap[50],gender[50],charnamafile[50],tempat[50],temp[1000],gendercheck;
    char tdee[20],tdeeprint[50];
    char *x;
    long z;
    int umur,tb,bb;
    int cmp1,cmp2,check;
    fp=fopen("strcpytest.txt","w+");
    printf("Nama Lengkap  : ");
    fgets(namalengkap,50,stdin);
    strtok(namalengkap,"\n");
    printf("Jenis Kelamin : ");
    scanf("%c",&gendercheck);
    if(gendercheck=='L' || gendercheck=='l')
    {
        strcpy(gender,"Laki-Laki");
    }
    else if(gendercheck=='P' || gendercheck=='p')
    {
        strcpy(gender,"Perempuan");
    }
    fflush(stdin);
    printf("Umur          : ");
    scanf("%d",&umur);
    fflush(stdin);
    printf("Tinggi Badan  : ");
    scanf("%d",&tb);
    fflush(stdin);
    printf("Berat Badan   : ");
    scanf("%d",&bb);
    fflush(stdin);
    printf("Rutinitas Olahraga : ");
    printf("\n1. Jarang Berolahraga\n2. 1 Hingga 3 Hari Dalam Seminggu\n");
    printf("Pilihan anda  : ");
    while(fgets(tdee,sizeof(tdee),stdin))
    {
        strtok(tdee,"\n");
        z=strtol(tdee,&x,10);
        if(x==tdee)
        {
            printf("Hanya masukkan angka!\n");
        }
        cmp1=strcmp(tdee,"1");
        cmp2=strcmp(tdee,"2");
        if(cmp1==0)
        {
            strcpy(tdeeprint,"Jarang Berolahraga");
            break;
        }
        else if(cmp2==0)
        {
            strcpy(tdeeprint,"1 Hingga 3 Hari Dalam Seminggu");
            break;
        }
        else
        {
            printf("\nPilih dengan pilihan yang benar.\nPilihan Anda : ");
        }
    }
    fprintf(fp,"%s\n%s\n%d\n%d\n%d\n%s",namalengkap,gender,umur,tb,bb,tdeeprint);
    fclose(fp);
    fp=fopen("strcpytest.txt","r+");
    scan:
    fscanf(fp,"%[^\n]\n[^\n]\n%d\n%d\n%d\n[^\n]",namalengkap,gender,&umur,&tb,&bb,tdeeprint);
    printf("\n\nwelcome %s!",namalengkap);
    printf("\nname : %s\ngender : %s\numur : %d\ntb : %d\nbb : %d\nRutinitas Olahraga : %s",namalengkap,gender,umur,tb,bb,tdeeprint);
    printf("\ncek lagi?: ");
    scanf("%d",&check);
    if(check==1)
    {
        goto scan;
        rewind(fp);
    }
    else
    {
        exit(0);
    }

    fclose(fp);

    return 0;
}

When I run it:

Nama Lengkap  : test str cpy
Jenis Kelamin : l
Umur          : 82
Tinggi Badan  : 5
Berat Badan   : 2
Rutinitas Olahraga :
1. Jarang Berolahraga
2. 1 Hingga 3 Hari Dalam Seminggu
Pilihan anda  : 2


welcome test str cpy!
name : test str cpy
gender : Laki-Laki
umur : 82
tb : 5
bb : 2
Rutinitas Olahraga : 1 Hingga 3 Hari Dalam Seminggu
cek lagi?: 1


welcome Laki-Laki!
name : Laki-Laki
gender : Laki-Laki
umur : 82
tb : 5
bb : 2
Rutinitas Olahraga : 1 Hingga 3 Hari Dalam Seminggu
cek lagi?: 1


welcome 82!
name : 82
gender : Laki-Laki
umur : 82
tb : 5
bb : 2
Rutinitas Olahraga : 1 Hingga 3 Hari Dalam Seminggu
cek lagi?: 1


welcome 5!
name : 5
gender : Laki-Laki
umur : 82
tb : 5
bb : 2
Rutinitas Olahraga : 1 Hingga 3 Hari Dalam Seminggu
cek lagi?:

The strcpytest.txt content file:

test str cpy
Laki-Laki
82
5
2
1 Hingga 3 Hari Dalam Seminggu

As you can see when i run it, everytime after i input 1, the "namalengkap" variable changes, but in strcpytest.txt doesn't change. I use while(fgets) for prevent any character input. So what's wrong here, I think I didn't notice yet cause I'm still learning. Thank you.

LearnerC
  • 51
  • 6
  • 1
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Jun 17 '22 at 11:45
  • 1
    Questions seeking debugging help should generally provide a [mre] of the problem, which includes all `#include` directives. This allows other people to easily test your program, by simply using copy&paste. – Andreas Wenzel Jun 17 '22 at 11:45
  • Which compiler are you using? If it is gcc or clang, then I suggest that you compile with the `-Wall` and `-Wextra` command-line arguments. This will enable most compiler warnings, which will point out at least one bug in your program. You may want to read this: [Why should I always enable compiler warnings?](https://stackoverflow.com/q/57842756/12149471) – Andreas Wenzel Jun 17 '22 at 11:50
  • Side note: Although `fflush( stdin )` may be working as intended on your platform, it may not work on other platforms and it is generally not recommended to do this. You may want to read this for further information: [Using fflush(stdin)](https://stackoverflow.com/q/2979209/12149471) – Andreas Wenzel Jun 17 '22 at 11:54

0 Answers0