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

How to compare strings with user input?

我的论文中有一个问题。我有 10 个员工 ID M001、A004、D007 等...用户正在输入上述 ID 之一,如果该 ID 不存在,则会打印未找到 ID。我厌倦了 strcmp 并卡住了。告诉我一个方法好吗?谢谢,请注意:我是 C.I 的初学者,我正在尝试一种简单的方法,现在它给出了 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],以便为每个字符串末尾的 [=14=] 终止符腾出空间。

从输入代码看来,statussearch 应该包含字符串,但它们被声明为 chars。修复此问题后,请注意在读取这些数组的 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