0

I have a struct for employees, id, age and salary. I need the user to select what employee they would like to edit by typing their user ID. I need to use a do-while to search through the struct until the user enters a valid user ID.

So far I've got:

SIZE = 4

case 3: //Updating Employee Salary
        printf("Update Employee Salary\n");
        printf("======================\n");

        do {
            printf("Enter Employee ID: ");
            scanf("%d", &salaryEmpChoice);
        } while (salaryEmpChoice != emp[0 - SIZE].id) ;

        break;

For the while part, I've tried [0 - SIZE], [0, 1, 2, 3], but it only works when I put in one value only at a time i.e. [0] instead of all 4 elements.

  • There's not enough here to understand what you want to do and not enough code to see what's going on. What's emp[]? What's SIZE? Is SIZE >= 0 (so you have a positive index into that array)? It looks like this code will loop until you get a salaryEmpChoice equal to one specific value in emp[].id. If SIZE is > 0, then this array index is invalid. – Dave Feb 26 '18 at 04:44
  • @Dave I'm trying to have it loop until salaryEmpChoice is equal to any specific value in emp[].id, however it's not working and I'm not sure what I'm doing wrong. – user9411342 Feb 26 '18 at 04:47
  • Then you probably need a nested loop inside the do..while to walk through the emp array, for example, if SIZE is the size of the emp array, `for ( i = 0; i < SIZE; ++i ) ...` – Dave Feb 26 '18 at 04:53
  • @Dave Where in the do while would I put the nested loop? – user9411342 Feb 26 '18 at 04:59

1 Answers1

0

That's because the C language only looks at one element at a time. If you want to look at a range of elements, you need to implement that. The compiler doesn't do it for you.

You are asking the compiler to find emp[0-SIZE].id and compare it to salaryEmpChoice. You set SIZE to 4, so emp[0-SIZE] is emp[0-4] == emp[-4].

This is telling the compiler to look where the array emp is stored, then look 4 array elements earlier in memory. After that, since you're looking for emp[-4].id, it will look in memory to the part of the negative fourth element where it would find an id if that were a valid array element. That's not what you want, because arrays in C begin at zero, so negative elements are not part of the array.

If emp is an array of an employee struct and you are looking for emp.id, then you need to compare your scanned employee id to emp[0].id, emp[1].id, emp[2].id, etc...

Tom
  • 939
  • 5
  • 9
  • Is there a way I can compare the scanned employee id to all of my emp[].id's at once? – user9411342 Feb 26 '18 at 05:08
  • No, not at once. You have to iterate through them. You can write a function or loop to do it, or use one someone else has written, but there isn't one that I'm aware of built into C. C is a relatively low-level language, and the kind of functionality you're talking about is something you would find either in a library or in some high level languages. – Tom Feb 27 '18 at 05:27