c - 如何将字符串与用户输入进行比较?

标签 c c-strings gcc-warning

我的论文中有一个问题。我有 10 个员工 ID M001、A004、D007 等...用户正在输入上述 ID 之一,如果该 ID 不存在,则会打印未找到 ID。我厌倦了 strcmp 并卡住了。告诉我一个方法好吗?谢谢,注意:我是 C 的初学者。我正在尝试一种简单的方法,现在它给出了 for 循环的错误。

subscripted value is neither array nor pointer nor vector

    #include<stdio.h>
    #include<string.h>
    float tSalary(float salary,float bonus);
    char searchid(char search);
    int main(void)
    {
       char status,input,search,C,P;
       char searchId[8][4]={"M001","A004","D007","D010","D012","Q008","Q015","DE09"};
       float salary,bonus,tSalary;
       int i,j;

       do{
          printf("Enter the employee id: ");
          scanf("%s", &search);
          printf("Enter the job status: ");
          scanf("%s", &status);  
          printf("Enter the salary: ");
          scanf("%f", &salary);

          for(i=0;i<8;i++){              //This is where all things go wrong
             for(j=0;j<4;j++){
                if(searchid[i][j]=search){    //the [i] where the subscripted error occurs 
                   printf("Id is valid\n");
                }
                else printf("Invalid id\n");

             }
          }

        printf("Do you want to enter another record?(Y-Yes/N-No): ");
        scanf(" %c", &input);
        }while(input=='Y'||input=='y');
    return 0;
    }

最佳答案

发布的代码中有不少问题。对于初学者,searchId 应声明为 searchId[8][5],以便为每个末尾的 \0 终止符腾出空间字符串。

从输入代码看来,statussearch 应该包含字符串,但它们被声明为 char。修复此问题后,请注意在读取这些数组的 scanf() 调用中不需要地址运算符 &。此外,在将 %s 转换说明符与 scanf() 一起使用时,应始终指定最大宽度以避免缓冲区溢出。

字符串不能使用== 比较运算符进行比较,所以这里应该使用strcmp()。这可以在遍历字符串数组的循环中完成;当索引达到 8 或比较成功时,循环退出。然后,在循环之后,如果索引达到 8(所有有效的 id 字符串均未通过测试),则 search 字符串无效。

这是实现所有这些的已发布代码的修改版本:

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

float tSalary(float salary,float bonus);
char searchid(char search);

int main(void)
{
    char status[1000];
    char search[1000];
    char input, C, P;
    char searchId[8][5] = { "M001", "A004", "D007", "D010",
                            "D012", "Q008", "Q015", "DE09" };
    float salary, bonus, tSalary;
    int i, j;

    do {
        printf("Enter the employee id: ");
        scanf("%999s", search);
        printf("Enter the job status: ");
        scanf("%999s", status);  
        printf("Enter the salary: ");
        scanf("%f", &salary);

        i = 0;
        while (i < 8 && strcmp(search, searchId[i]) != 0) {
            ++i;
        }

        if (i < 8) {
            printf("Id is valid\n");
        } else {
            printf("Invalid id\n");
        }

        printf("Do you want to enter another record?(Y-Yes/N-No): ");
        scanf(" %c", &input);
    } while (input == 'Y' || input == 'y');

    return 0;
}

示例程序交互:

Enter the employee id: A004
Enter the job status: pending
Enter the salary: 2000
Id is valid
Do you want to enter another record?(Y-Yes/N-No): y
Enter the employee id: Q015
Enter the job status: completed
Enter the salary: 3000
Id is valid
Do you want to enter another record?(Y-Yes/N-No): y
Enter the employee id: B001
Enter the job status: completed
Enter the salary: 1200
Invalid id
Do you want to enter another record?(Y-Yes/N-No): n

关于c - 如何将字符串与用户输入进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44248918/

相关文章:

c++ - 无法从成员变量中的初始化字符串推断数组大小的原因是什么?

c - 警告 : integer constant is too large for "long" type

c - 为什么要包含标准头文件?

c - 这个程序有什么作用? (数组+函数)

c++ - 用最少/一行代码更新一维数组的多个位置

c - pipe2(...) vs pipe() + fcntl(...),为什么不同?

c - 为什么int指针可以存储字符串?

c++ - 为什么 `(void *)&` 会得到变量的地址?

c - 为什么使用gcc返回局部变量地址时得不到警告信息?

c - 如何使用 libcurl 函数 "curl_easy_setopt(CURL *handle, CURLOPT_DNS_LOCAL_IP4, char *address);"