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.