我正在写一个功能,我有一个问题,scanf函数不能读空格键“”,我该如何解决它?如何使用scanf输入空格键作为输入?
void add()
{
char choose2;
FILE *fp;
struct booking book;
system("cls");
fp=fopen("hotelbooking.txt","a");
if(fp == NULL)
{
printf("There are no data file!");
}
else
{
printf("Add New Hotel Booking Record(s)\n");
printf(" Name of Traveller: \n");
scanf("%s",book.travellername);
fprintf(fp,"\n%s",book.travellername);
printf(" Destination: ");
scanf("%s",book.destination);
fprintf(fp,"\n%s",book.destination);
fclose(fp);
}
}
在tervellername部分,如果我想输入例如“杰森乔治”,如何扫描名称之间的空格键?
我使用下面的结构:
struct booking
{
char travellername[20];
char destination[20];
}book;
你只需要一个换行符(Return或Enter)来终止输入? – StoryTeller
我试过gets()但它不会让我输入任何东西,它只是跳过这一行 –
@StoryTeller我想要一些类似于scanf的东西,但我可以用space来输入。 “笔” –