0

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 C.I 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;
    }
5
  • You want strings instead of chars Commented May 29, 2017 at 18:57
  • @KeineLust Please tell me a way to do it Commented May 29, 2017 at 19:04
  • 1
    I found one problem in your code ` if(searchid[i][j]=search){ //the [i] where the subscripted error occurs printf("Id is valid\n"); }` here its == not assignment that is =? Commented May 29, 2017 at 19:29
  • 1
    Many problems here. The multiple prints of "Id id valid\n" are because code prints this after checking every character in searchId. As noted by @LethalProgrammer, the = assignment operator is used instead of the == comparison operator. But, even before this there is big trouble, since search and status are chars, but are treated as strings by input code. If search is a string, == can not be used, but strcmp() should instead be used. Commented May 29, 2017 at 19:35
  • 1
    when calling any of the scanf() family of functions: 1) always check the returned value (not the parameter values) to assure the operation was successful. 2) when using the '%s' input/format specifier, always use a MAX CHARACTERS modifier that is on less than the length of the input buffer to avoid buffer overflow. Such overflow results in undefined behavior and can lead to a seg fault event. Commented May 30, 2017 at 18:36

1 Answer 1

2

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 chars. 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
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much for the effort and help.I learnt a lot from you.Is this also an alternative searchId[8][4]={ "M001", ....."Q008", "Q015", "DE09", "\0" }; ? I didn;t know about the buffer overflow in strings, Now i know what is the importance. strcmp was the trouble part and the calling the values in the array.Thank you so much for the brief explanation and solving the problem.I really appreciate it.
@JinaPerera-- You are welcome. Your alternative proposal will not work; consider that "M001" is a string which contains 4 chars plus a null-terminator. That might look like char arr[5] = "M001", which is the same thing as char arr[5] = { 'M', '0', '0', '1', '\0' }. For this reason you need searchId[8][5], to declare an array of 8 arrays of 5 chars. Your alternative adds a 9th string to the mix, so would need to be searchId[9][5], still needing [5] for all of the characters in the original strings.
Note that you do sometimes see lists of strings like this with the final string empty, but this is usually expressed as something like: char strings[4][5] = { "str1", "str2", "str3", "" }. The final empty string serves to mark the end of the list of strings.
Thats a wonderful explanation @David Bowling .Understood very well.I would have to do some exercises and practice this.Its good to know about the null terminator(the placement and declaring the arrays). A place where i can possibly go wrong.Thank you once again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.