So i have a problem that strcmp not working even the string is same. The cmp between destination and source string is have same string, but when strcmp it, it not working. See the code for explanation:
# include <stdio.h>
# include <string.h>
int main()
{
FILE *fp;
fp=fopen("test.txt","r");
char namalengkap[100],gender[100],charnamafile[50],tempat[50],temp[1000],tdeeprint[100],gendercheck;
int umur,tb,bb,x;
int cmp1,cmp2,cmp3,cmp4,cmp5,cmp6,check;
float tdeenum;
fscanf(fp,"Username : %s\nPassword : %s",tempat,temp);
fscanf(fp,"\n\n\n=============================DATA PRIBADI USER===============================\n");
fscanf(fp,"= Nama Lengkap : R[^\n] =\n",namalengkap);
fscanf(fp,"= Jenis Kelamin : R[^\n] =\n",gender);
fscanf(fp,"= Umur : Rd =\n",&umur);
fscanf(fp,"= Tinggi Badan (CM) : Rd =\n",&tb);
fscanf(fp,"= Berat Badan (KG) : Rd =\n",&bb);
fscanf(fp,"= Rutinitas Olahraga : R[^\n] =\n",tdeeprint);
fscanf(fp,"=============================================================================\n");
cmp1=strcmp(tdeeprint,"Jarang Berolahraga");
cmp2=strcmp(tdeeprint,"1 Hingga 3 Hari Dalam Seminggu");
cmp3=strcmp(tdeeprint,"3 Hingga 5 Hari Dalam Seminggu");
cmp4=strcmp(tdeeprint,"6 Hingga 7 Hari Dalam Seminggu");
cmp5=strcmp(tdeeprint,"Pekerjaan Fisik Berat Atau Olahraga 2x Dalam Sehari");
if(cmp1==0)
{
tdeenum=1.2;
printf("tde1work : %f\n",tdeenum);
}
else if(cmp2==0)
{
tdeenum=1.375;
printf("tde2work : %f\n",tdeenum);
}
else if(cmp3==0)
{
tdeenum=1.55;
printf("tde3work : %f\n",tdeenum);
}
else if(cmp4==0)
{
tdeenum=1.725;
printf("tde4work : %f\n",tdeenum);
}
else if(cmp5==0)
{
tdeenum=1.9;
printf("tde5work : %f\n",tdeenum);
}
printf("\nUsername : %s\n",tempat);
printf("\nPassword : %s\n",temp);
printf("\nName : %s\n",namalengkap);
printf("\nGender : %s\n",gender);
printf("\nUmur : %d\n",umur);
printf("\nTB : %d\n",tb);
printf("\nBB : %d\n",bb);
printf("\ntdeeprint : %s\n",tdeeprint);
printf("\ntdeepnum : %f\n",tdeenum);
fclose(fp);
return 0;
}
When i run it:
Username : hitunga
Password : 12345
Name : a
Gender : Perempuan
Umur : 30
TB : 155
BB : 50
tdeeprint : Jarang Berolahraga
tdeepnum : 0.000000
Process returned 0 (0x0) execution time : 0.043 s
Press any key to continue.
The test.txt file content:
Username : hitunga
Password : 12345
=============================DATA PRIBADI USER===============================
= Nama Lengkap : a =
= Jenis Kelamin : Perempuan =
= Umur : 30 =
= Tinggi Badan (CM) : 155 =
= Berat Badan (KG) : 50 =
= Rutinitas Olahraga : Jarang Berolahraga =
= TDEE : 1.200 =
=============================================================================
So what wrong on my code? I really confused why isn't working. Its literally same string comparison but not working. Thank you
Edit: So i replace the cmp section with fscanf, but still not getting the number
fscanf(fp,"= TDEE : R.3f =\n",&datusr.tdeenum);
CodePudding user response:
If you change your printf
statement to this, the problem will likely become clear to you:
printf("\ntdeeprint : '%s'\n",tdeeprint);
Output snippet:
tdeeprint : 'Jarang Berolahraga '
The same applies to namalengkap
and gender
. The R[^\n]
pattern includes all the space characters that follow what you want until you get to =
, that's why it doesn't match the Jarang Berolahraga
.