简体   繁体   中英

How to compare strings with user input?

I have a question in my paper. I have 10 employee ids M001,A004,D007,etc...User is inputting one of the the mentioned Ids and if the id is not there it prints id not found. I tired with strcmp and got stuck. Its good if you tell me a way to do it? Thanks, note: i am a beginner in CI am trying an easy way now it gives the error with the for loop.

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;
    }

There are quite a few problems in the posted code. For starters, searchId should be declared as searchId[8][5] , to make room for the \\0 terminator at the end of each string.

It appears from the input code that status and search should hold strings, but these are declared as char s. After fixing this, note that there is no need for the address operator & in the calls to scanf() that read into these arrays. Also, maximum widths should always be specified when using the %s conversion specifier with scanf() to avoid buffer overflows.

Strings can not be compared using the == comparison operator, so strcmp() should be used here. This can be done in a loop that steps through the array of strings; the loop exits when the index reaches 8, or a comparison is successful. Then, after the loop, if the index has reached 8 (all valid id strings failed the test) the search string was not valid.

Here is a modified version of the posted code that implements all of this:

#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;
}

Sample program interaction:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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