Home > Mobile >  error- forbids comparison between pointer and integer and how to use user input to other function?
error- forbids comparison between pointer and integer and how to use user input to other function?

Time:07-19

In the seat reservation that I am working on, I used if(p.name == passengername)to get user input to update its input. 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. 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.

And what do I need to do to get user's input in chooseseat() so that I can use it in reserve(); When I print it the p.seatcol and p.seatrow in fprintf displays 0. What do I need to do to save users input in chooseseat(); to 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);
}

CodePudding user response:

Your passengername is a single 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. Use a char array like you did for p.name.
Then what you want to do is compare strings via pointers to them, not pointers.
So switch from == (comparing pointers) to strcmp(). ( https://en.cppreference.com/w/cpp/string/byte/strcmp )
Or even better, strncmp().

CodePudding user response:

There's a bunch of issues here.

I'll concentrate just on the update() function, because this is the place of your current issue.

Firstly, the code around the reported compilation error is using a variable named p. This variable has not been declared in the scope of function update(). Looking at the rest of your code, reading a few tutorials on C variable scope would be good.

The code is comparing p.name (char[50]) with 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?). The single character is basically a small integer (1 byte).

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.
  •  Tags:  
  • c
  • Related