簡體   English   中英

指針和整數錯誤之間的比較

[英]comparison between pointer and integer error

嘿伙計們我有這個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);
}
if(myStruct[i].Status[15] == "checked-out")

myStruct[i].Status[15]給出該索引處的字符,並將它與字符串進行比較,這就是問題所在。

我認為你需要使用strcmpStatus數組本身進行比較。

if( strcmp( myStruct[i].Status, "checked-out") == 0 )
// ...

Status[15]是一個char ,你試圖比較const char * “check-out”。 我不知道“15”是什么,但我懷疑你只是想要

if(strcmp(myStruct[i].Status, "checked-out") == 0) 

strcmp()是C中的字符串比較函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM