简体   繁体   English

错误 - 禁止指针和整数之间的比较以及如何将用户输入用于其他函数?

[英]error- forbids comparison between pointer and integer and how to use user input to other function?

In the seat reservation that I am working on, I used if(p.name == passengername) to get user input to update its input.在我正在处理的座位预订中,我使用if(p.name == passengername)来获取用户输入以更新其输入。 But it says error.但它说错误。

forbids comparison between pointer and integer [-fpermissive]
         if(p.name == passengername)```

I declared passengername as char but it is not working.我将passengername名称声明为char但它不起作用。 Am I doing it wrong?我做错了吗? I assumed that p.name is a char so that I can compare it to the char passengername that I declared.我假设p.name是一个字符,以便我可以将它与我声明的char passengername进行比较。

And what do I need to do to get user's input in chooseseat() so that I can use it in reserve();我需要做什么才能在chooseseat()中获取用户的输入,以便我可以在reserve(); When I print it the p.seatcol and p.seatrow in fprintf displays 0 .当我打印它时, fprintf中的p.seatcolp.seatrow显示0 What do I need to do to save users input in chooseseat();我需要做些什么来保存用户在chooseseat(); to reserve(); reserve(); ? ?

Below is the part of my sample program.下面是我的示例程序的一部分。

#include<stdio.h>
#include<malloc.h>
#include<string.h>


#define ROWS 11
#define COLS 8
#define PASSENGERSIZE sizeof(passenger)

typedef struct{
    char city[20], name[50], seatcol;
    int age, seatrow, id;
    unsigned int seatnum;

}passenger;

void chooseseat();

void reserve(){
    passenger p;
    FILE *fp, *fp1;

    fp=fopen("passenger.txt", "a+");
    fp1=fopen("passenger1.txt", "a+");
    
    printf("\n\t\t\tEnter your name:");
    scanf(" %s",p.name);

    chooseseat();
    write(&p, PASSENGERSIZE, 1, fp1);
    fprintf(fp, " %s\t%d%c",p.name, p.seatrow,p.seatcol);
    
fclose(fp);
fclose(fp1);
}

void chooseseat(){
    passenger p;
    // Read the user input until it reserves a seat or request quitting.
    
        // Read user input.
        printf("\nEnter your seat number: (EX: 1A)");
        scanf(" %d%c",&p.seatrow, &p.seatcol);

        // Check if the seat requested is valid entry.
        if (p.seatrow > 0 && p.seatrow < ROWS &&
            p.seatcol >= 'A' && p.seatcol <= 'A' + COLS){

                // Input range is valid, check if seat is already taken.
                if (seat[p.seatrow - 1][p.seatcol - 'A'] != 'X'){
                    // Seat is available, reserve it and break the loop.
                    seat[p.seatrow - 1][p.seatcol - 'A'] = 'X';
                    printf("Congratulations. Your seat number is %d%c\n",p.seatrow,p.seatcol);
                }
                else{
                    // Seat is already taken.
                    printf("Seat is already taken. Choose another seat? (Y/N)\n.");
                    scanf(" %c", &answer);
                if (answer != 'Y'){
                    printf("Your data will be not saved and will be returned to main menu.\n");
                }
        }
        
    }
    else{
            // Input range is invalid.
            printf("Invalid seat row/col,\n");
}
}
void display(){

    passenger p;

    FILE *fp, *fp1;

    fp=fopen("passenger.txt", "r");
    fp1=fopen("passenger1.txt", "r");

    while(fread(&p, PASSENGERSIZE, 1, fp1)){
        printf(" \n%s\t%d%c",p.name,p.seatrow,p.seatcol);
    }
    
    fclose(fp);
    fclose(fp1);
}

void update(){

    FILE *fp, *fp1, *fp2;

    char passengername;
    int found=0;

    fp=fopen("passenger.txt", "r");
    fp1=fopen("passenger1.txt", "r");
    fp2=fopen("temp.txt","w");

    printf("\n\t\t\tEnter roll number to update: ");
    scanf(" %c", &passengername);

    while(fread(&p, PASSENGERSIZE, 1, fp1)){
        if(p.name == passengername){

            found=1;
            displayseat();

            printf("\n\t\t\tUpdate your seat number: (EX: 1A)");
            scanf(" %d%c",&p.seatrow, &p.seatcol);
        }fwrite(&p, PASSENGERSIZE, 1, fp2);
    }
    
    fclose(fp);
    fclose(fp1);
    fclose(fp2);

}
int main(){

    int selection;

    do{printf("\n\t\t\t\t\t************WELCOME TO C AIRLINES********");

    printf("\n\t\t\t\t\t CHOOSE A MENU OPTION: ");
    printf("\n");

        printf("\n\t\t\t\t\t [1] ENTER A RESERVATION");
        printf("\n\t\t\t\t\t [2] EDIT A RESERVATION");
        printf("\n\t\t\t\t\t [3] DISPLAY RESERVATION");
        printf("\n\t\t\t\t\t [0] EXIT");

        printf("\n\t\t\t\t\t=============================\n");
        printf("\n\t\t\t\t\tPlease Enter Any : ");
        scanf("%d",&selection);

        switch(selection){
        case 1: reserve();
        break;
        case 2: update();
        break;
        case 3: display();
        break;
        default:
        break;
        }
}while(selection!=0);
}

Your passengername is a single char .您的passengername是单个char
For one, the compiler considers that a single integer (of small width), this is what causes your error.一方面,编译器认为单个整数(小宽度),这就是导致您的错误的原因。
For another, you need more than one char there, since you want to store a name.另一方面,您需要多个char ,因为您想存储一个名称。 Use a char array like you did for p.name .p.name一样使用 char 数组。
Then what you want to do is compare strings via pointers to them, not pointers.然后你想要做的是通过指向它们的指针而不是指针来比较字符串。
So switch from == (comparing pointers) to strcmp() .所以从== (比较指针)切换到strcmp() ( https://en.cppreference.com/w/cpp/string/byte/strcmp ) https://en.cppreference.com/w/cpp/string/byte/strcmp
Or even better, strncmp() .甚至更好的是strncmp()

There's a bunch of issues here.这里有一堆问题。

I'll concentrate just on the update() function, because this is the place of your current issue.我将只关注update()函数,因为这是您当前问题的地方。

Firstly, the code around the reported compilation error is using a variable named p .首先,报告的编译错误周围的代码使用了一个名为p的变量。 This variable has not been declared in the scope of function update() .此变量尚未在函数update()的范围内声明。 Looking at the rest of your code, reading a few tutorials on C variable scope would be good.查看其余代码,阅读一些关于 C 变量范围的教程会很好。

The code is comparing p.name (char[50]) with passengername (char).该代码将p.name (char[50]) 与passengername姓名 (char) 进行比较。 The reason you're seeing this error is the char-array p.name ultimately is treated as a pointer, whereas passengername is a single character (perhaps you meant this to be a char-array too?).您看到此错误的原因是 char-array p.name最终被视为指针,而passengername是单个字符(也许您的意思是这也是一个 char-array?)。 The single character is basically a small integer (1 byte).单个字符基本上是一个小整数(1 个字节)。

This means that the statement:这意味着声明:

if ( p.name == passengername )

resolves to:决议:

if ( pointer-to-array-of-char == char )

which degrades to:降级为:

if ( pointer == number )

I can only guess at what the intention of the code is, but perhaps something like this is more what you desired:我只能猜测代码的意图是什么,但也许这样的东西更符合您的期望:

void update()
{
    FILE *fp, *fp1, *fp2;
    passenger p;                                      // <<-- HERE define  'p'
    char passengername[sizeof(passenger.name)];       // <<-- HERE array, not char
    int found=0;

    fp  = fopen("passenger.txt", "r");
    fp1 = fopen("passenger1.txt", "r");
    fp2 = fopen("temp.txt","w");

    printf("\n\t\t\tEnter roll number to update: ");
    fgets( passengername, sizeof(passenger.name), stdin );        // <<-- HERE fgets()
    // TODO, Handle \n from fgets()

    while( fread( &p, sizeof(passenger), 1, fp1 ) ) 
    {
        if ( strcmp( p.name, passengername ) == 0 )      // <<-- HERE strcmp()
        {
            found=1;
            displayseat();

            printf( "\n\t\t\tUpdate your seat number: (EX: 1A)" );
            scanf( " %d%c", &p.seatrow, &p.seatcol );
        }

        fwrite( &p, sizeof(passenger), 1, fp2 );
    }
    
    fclose(fp);
    fclose(fp1);
    fclose(fp2);

}
  • Errors and omissions expected.预计会出现错误和遗漏。 I made no attempt to fix all the errors in the code.我没有尝试修复代码中的所有错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM