char membership[10],memberID[10];
int c;
float AfterDiscount;
printf("\nEnter Your Membership ID to get 5 percent discount: ");
scanf(" %s",&memberID);
member=fopen("Membership.txt","r");
fgets(membership,10,member);
if(strcmp(membership,memberID)==0)
{
AfterDiscount=final*0.95;
printf("Your final price after 5 percent discount is RM%6.2f",AfterDiscount);
printf("Thank you to come again, our honour member.");
fclose(member);
return AfterDiscount;
}
printf("The Member ID is not exist. Please kindly contact the adminstrator, 03-XXXXXXX\n");
fclose(member);
return 0;
我想确定以“B123456789”模式形成的成员资格的存在。我创建了一个成员列表并命名为“Membership.txt”。但即使我输入了有效的会员ID,似乎也无效。
答案 0 :(得分:0)
您对fgets的通话最多可读取10 - 1 = 9个字符,这意味着您阅读的10个字符ID将缺少其最后一个字符。此外,您的字符串只能容纳9个字符+ \0
终结符。
变化:
char membership[10],memberID[10];
为:
char membership[11],memberID[11];
并改变:
fgets(membership,10,member);
为:
fgets(membership,sizeof(membership),member);