嘿伙计们,我有这个c程序工作,我想修改printbooking(),以便只打印状态为“签出”的房间 到目前为止,我只是得到关于指针和整数之间的比较的错误....任何帮助我应该如何做到这一点? 并且。我希望能够通过roomID搜索房间并编辑他们的详细信息。任何帮助将不胜感激! 这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char BookId[7];
char CustId[7];
char RoomId[5];
char NumGuests[4];
char StartDate[11];
char EndDate[11];
char Status[20];
} booking_t;
int readBooking(booking_t *myStruct)
{
FILE *infile;
infile = fopen("booking.txt", "r");
char record[201];
char *token;
int i = 0;
while (fgets(record, 200, infile) != NULL) {
token = strtok(record, ";");
strcpy(myStruct[i].BookId, token);
token = strtok(NULL, ";");
strcpy(myStruct[i].CustId, token);
token = strtok(NULL, ";");
strcpy(myStruct[i].RoomId, token);
token = strtok(NULL, ";");
strcpy(myStruct[i].NumGuests, token);
token = strtok(NULL, ";");
strcpy(myStruct[i].StartDate, token);
token = strtok(NULL, ";");
strcpy(myStruct[i].EndDate, token);
token = strtok(NULL, "\n");
strcpy(myStruct[i].Status, token);
i++;
}
fclose(infile);
return(i);
}
//这是我只想打印出“签出的房间”的代码
void printBooking(booking_t *myStruct, int Size)
{
printf("Booking ID, Customer ID, Room ID, Number of Guests, Start Date, End Date, Status\n");
int i;
for(i = 0; i < Size;i++){
if(myStruct[i].Status[15] == "checked-out") //the error message points to this line
printf("%s %s %s %s %s %s %s\n", myStruct[i].BookId, myStruct[i].CustId, myStruct[i].RoomId, myStruct[i].NumGuests, myStruct[i].StartDate, myStruct[i].EndDate, myStruct[i].Status);
}
printf("\n");
}
//
void printMayBooking(booking_t *myStruct, int Size)
{
printf("Booking ID, Customer ID, Room ID, Number of Guests, Start Date, End Date, Status\n");
int i;
for(i = 0; i < Size;i++){
if(myStruct[i].StartDate[4] == '5')
printf("%s %s %s %s %s %s %s\n", myStruct[i].BookId, myStruct[i].CustId, myStruct[i].RoomId, myStruct[i].NumGuests, myStruct[i].StartDate, myStruct[i].EndDate, myStruct[i].Status);
}
printf("\n");
}
int main()
{
booking_t booking_list[50];
int Size;
Size = readBooking(booking_list);
printBooking(booking_list, Size);
printMayBooking(booking_list, Size);
return(0);
}
答案 0 :(得分:2)
if(myStruct[i].Status[15] == "checked-out")
myStruct[i].Status[15]
给出该索引处的字符,并将它与字符串进行比较,这就是问题所在。
我认为您需要使用Status
与strcmp
数组本身进行比较。
if( strcmp( myStruct[i].Status, "checked-out") == 0 )
// ...
答案 1 :(得分:0)
Status[15]
是char
,您正在尝试与const char *
“签出”进行比较。我不知道“15”的用途是什么,但我怀疑你只想要
if(strcmp(myStruct[i].Status, "checked-out") == 0)
strcmp()
是C中的字符串比较函数。