While 循环不运行
#include <stdio.h>
struct info{
char name[20];
char email[30];
char tel[20];
char sex;
int x,x1,y1,z1,x2,y2,z2;
int balance;
char id[11];}client;
main()
{
FILE *fp1,*fp2;
printf("Welcome to Sports Booking System registration!\n\n");
printf("After being a member of our system,\nyou can book the sports facilities at any time you like!!!\nIf you registrate now , you can a bonus of 30 dollars,\nSo come on !!!\n");
while(1)
{
printf("Please inpute the ID you want to use:");
scanf("%s",&client.id);
/*printf("%s",client.id);*/
if(fp1=fopen(client.id,"rb")!=NULL)
{
printf("The ID you inpute has already been used! Please choose another ID, sorry for the inconvenient!\n");
fclose(fp1);
}
else
{
printf("Please inpute your name:");
scanf("%s",&client.name);
/*printf("%s",client.name);*/
printf("Please inpute your email , so we can send you a email to confirm the booking after you booking the facilites ,\nand we can also remind you before the date you has booked! \nSo make sure that the email is correct!\n");
printf("Your email address:");
scanf("%s",&client.email);
/*printf("%s",client.email);*/
printf("Please inpute your telephone number , so we can contact you immediately if there is something emergency!\n");
printf("Your telephone number:");
scanf("%s",&client.tel);
/*printf("%s",client.tel);*/
printf("Please input your sex:");
fflush(stdin);
client.sex=getchar();
client.x=0;client.x1=6;client.y1=0;client.z1=0;client.x2=6;client.y2=0;client.z2=0;
client.balance=30;
fp2=fopen(client.id,"wb");
fwrite(&client,sizeof(struct info),1,fp2);
fclose(fp2);
printf("Thank you for complete registration.Now you can log in the booking system to book the facilities . Also you have already 30 dollors in your account.\n");
break;
}
}
}
在存完一个文件后,再运行程序,输入相同的ID,程序不进入While循环,而是出错并跳出这是为什么啊
[解决办法]
if( ( fp1=fopen(client.id,"rb" ) )!=NULL)
{
printf("The ID you inpute has already been used! Please choose another ID, sorry for the inconvenient!\n");
fclose(fp1);
}
加上括弧
否则的话会这样:f(fp1=fopen(client.id,"rb")!=NULL)
从右边向左边
fopen()!=NULL这个的值是1
那么fp1也的值被赋成1
这时候你去关闭1
这是标准输入(输出?不记得了)
这时候肯定出错了
[解决办法]
- C/C++ code
#include <stdio.h>struct info{ char name[20]; char email[30]; char tel[20]; char sex; int x,x1,y1,z1,x2,y2,z2; int balance; char id[11];}client; int main() { FILE *fp1,*fp2; printf("Welcome to Sports Booking System registration!\n\n"); printf("After being a member of our system,\nyou can book the sports facilities at any time you like!!!\nIf you registrate now , you can a bonus of 30 dollars,\nSo come on !!!\n"); while(1) { printf("Please inpute the ID you want to use:"); scanf("%s",&client.id); /*printf("%s",client.id);*/ if((fp1=fopen(client.id,"rb"))!=NULL)//至少这里有一个问题,运算符优先级 { printf("The ID you inpute has already been used! Please choose another ID, sorry for the inconvenient!\n"); fclose(fp1); } else { printf("Please inpute your name:"); scanf("%s",&client.name); /*printf("%s",client.name);*/ printf("Please inpute your email , so we can send you a email to confirm the booking after you booking the facilites ,\nand we can also remind you before the date you has booked! \nSo make sure that the email is correct!\n"); printf("Your email address:"); scanf("%s",&client.email); /*printf("%s",client.email);*/ printf("Please inpute your telephone number , so we can contact you immediately if there is something emergency!\n"); printf("Your telephone number:"); scanf("%s",&client.tel); /*printf("%s",client.tel);*/ printf("Please input your sex:"); fflush(stdin); client.sex=getchar(); client.x=0;client.x1=6;client.y1=0;client.z1=0;client.x2=6;client.y2=0;client.z2=0; client.balance=30; fp2=fopen(client.id,"wb"); fwrite(&client,sizeof(struct info),1,fp2); fclose(fp2); printf("Thank you for complete registration.Now you can log in the booking system to book the facilities . Also you have already 30 dollors in your account.\n"); break; } } }