You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言赌场电话簿程序出现「comparison between pointer and integer」错误求助

Fixing the "comparison between pointer and integer" Error for Your Casino Phonebook

Hey there! Welcome to C programming—great choice picking a practical project like a casino phonebook to learn with. Let's tackle that error and build your basic version step by step.

First, let's fix the small issues in the code you shared:

  • The main() function should have a return type of int (this is standard in C)
  • Your printf string is missing a closing quote and newline, which will cause a compile error right off the bat

Here's the corrected starting snippet:

#include <stdio.h>

int main() {
    printf("Please Select Department [Security, Food & Beverage, Hotel]: ");
    // We'll add input handling and department logic here next
    return 0;
}

Why You're Seeing the "comparison between pointer and integer" Error

That error almost always happens when you try to compare a string (which is a pointer to a char array in C) with a single char (an integer type) or when you use == to compare strings directly. For example, if you tried something like this:

// Wrong way - triggers the error
char user_choice;
scanf("%c", &user_choice);
if (user_choice == "Security") { // Comparing a char (integer) to a string pointer
    // ...
}

Or even this (which doesn't trigger the same error but still doesn't work as intended):

// Still wrong - compares pointer addresses, not string content
char user_input[50];
scanf("%s", user_input);
if (user_input == "Security") { 
    // ...
}

The Fix: Use strcmp() for String Comparisons

In C, to compare the content of two strings, you need to use the strcmp() function from the <string.h> header. This function returns 0 if the two strings are identical.

Full Basic Working Example

Here's a complete basic version of your casino phonebook that avoids the error:

#include <stdio.h>
#include <string.h> // Required for strcmp()

int main() {
    char department[50];
    
    printf("Welcome to the Casino Phonebook!\n");
    printf("Please Select a Department [Security, Food & Beverage, Hotel]: ");
    scanf("%s", department); // Note: This won't handle spaces in "Food & Beverage" - we can fix that later!

    // Compare user input to department names using strcmp()
    if (strcmp(department, "Security") == 0) {
        printf("\nSecurity Department Phones:\n");
        printf("- Main Desk: 555-1234\n");
        printf("- Patrol Team: 555-1235\n");
    } 
    else if (strcmp(department, "Hotel") == 0) {
        printf("\nHotel Department Phones:\n");
        printf("- Front Desk: 555-2345\n");
        printf("- Housekeeping: 555-2346\n");
    }
    // Handle "Food & Beverage" - since scanf("%s") stops at spaces, we'll use a workaround for now
    else if (strcmp(department, "Food") == 0) {
        printf("\nFood & Beverage Department Phones:\n");
        printf("- Kitchen: 555-3456\n");
        printf("- Bar: 555-3457\n");
    }
    else {
        printf("\nSorry, that department isn't in our phonebook yet!\n");
    }

    return 0;
}

Quick Notes for Your Next Steps

  • To handle department names with spaces (like "Food & Beverage"), use fgets() instead of scanf("%s")—just remember to trim the newline character that fgets() adds.
  • Later, you can expand this by storing departments and their phones in arrays or structs to make the code cleaner and easier to extend.

内容的提问来源于stack exchange,提问作者Koquan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:16:59