Почему этот цикл, который ищет сотрудника, не находит его? - PullRequest
0 голосов
/ 18 июня 2019

Я недавно начал изучать C, и я должен завершить эту программу сотрудника C: https://pastebin.com/b5SyYQQ4

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#define  SIZE 4

// Declare Struct Employee
struct employee {
    int id;
    int age;
    double salary;
};

/* main program */
int main(void) {
    int l = 0, i = 0, k = 0, j = 0;
    int option = 0;
    int empId;
    double empSalary;
    int empFound = 0;
    printf("---=== EMPLOYEE DATA ===---\n\n");


    struct employee emp[SIZE] = { {0} };
    do {
        printf("1. Display Employee Information\n");
        printf("2. Add Employee\n");
        printf("3. Update Employee Salary\n");
        printf("4. Remove Employee\n");
        printf("0. Exit\n\n");
        printf("Please select from the above options: ");

        scanf("%d", &option);
        printf("\n");

        switch (option) {
        case 0:
            break;
        case 1:
            printf("EMP ID  EMP AGE EMP SALARY\n");
            printf("======  ======= ==========\n");
            for (j = 0; j < i; j++) {
                printf("%6d%9d%11.2lf\n", emp[j].id, emp[j].age, emp[j].salary);
            }
            printf("\n");
            break;
        case 2:
            printf("Adding Employee\n");
            printf("===============\n");
            if (i >= SIZE) {
                printf("ERROR!!! Maximum Number of Employees Reached\n\n");
            }
            else {
                printf("Enter Employee ID: ");
                scanf("%d", &emp[i].id);
                while (emp[i].id < 0) {
                    printf("\nPlease input a value higher than 0: ");
                    scanf("%d", &emp[i].id);
                }
                printf("Enter Employee Age: ");
                scanf("%d", &emp[i].age);
                printf("Enter Employee Salary: ");
                scanf("%lf", &emp[i].salary);
                printf("\n");
                i++;
            }
            break;
        case 3:
            printf("Update Employee Salary\n");
            printf("======================\n");
            do {
                printf("Enter Employee ID: ");
                scanf("%d", &empId);
                for (k = 0; k < SIZE; k++) {
                if (emp[k].id == empId) {
                    printf("The current salary is %0.2lf\n", emp[k].salary);
                    printf("Enter Employee New Salary: ");
                    scanf("%lf", &empSalary);
                    emp[k].salary = empSalary;
                    empFound = 1;
                    printf("\n");

                }
            }
                if (empFound == 0) {
                printf("*** ERROR: Employee ID not found! ***\n");
            }
            } while (empFound != 1);
            break;
        case 4:
            printf("Remove Employee\n");
            printf("===============\n");
                printf("Enter Employee ID: ");
                scanf("%d", &empId);
                l = 0;
                empFound = 0;
                while (empFound == 0) {
                    l++;
                    if (empId == emp[l].id) {
                        printf("Employee %d will be removed\n\n", empId);
                        empFound = 1;
                    }
                    if (empFound == 0) {
                        printf("*** ERROR: Employee ID not found! ***\n");
                    }
                }
                for (l = l; l < 4; l++) {
                    emp[l].id = emp[l + 1].id;
                    emp[l].age = emp[l + 1].age;
                    emp[l].salary = emp[l + 1].salary;
                }
                i--;
                break;
        default:
            printf("ERROR: Incorrect Option: Try Again\n\n");
        }
    } while (option != 0);

    printf("Exiting Employee Data Program. Good Bye!!!\n");

    return 0;
}

В случае 4 мне нужно сказать * ОШИБКА: идентификатор сотрудника ненайденный!* когда идентификатор не найден, как в случае 3, но, что бы я ни пытался, я потерплю неудачу.

Я очень ценю помощь!

пробовал так же, как в случае 3, но это не удалось.

case 4:
            printf("Remove Employee\n");
            printf("===============\n");
                printf("Enter Employee ID: ");
                scanf("%d", &empId);
                l = 0;
                empFound = 0;
                while (empFound == 0) {
                    l++;
                    if (empId == emp[l].id) {
                        printf("Employee %d will be removed\n\n", empId);
                        empFound = 1;
                    }
                    if (empFound == 0) {
                        printf("*** ERROR: Employee ID not found! ***\n");
                    }
                }
                for (l = l; l < 4; l++) {
                    emp[l].id = emp[l + 1].id;
                    emp[l].age = emp[l + 1].age;
                    emp[l].salary = emp[l + 1].salary;
                }
                i--;
                break;

Я ожидаю:

Remove Employee

===============

Enter Employee ID: 1

*** ERROR: Employee ID not found! ***

Enter Employee ID: 2

Employee 2 will be removed.

Но я получил:

Remove Employee

===============

Enter Employee ID: 1

*** ERROR: Employee ID not found! ***

*** ERROR: Employee ID not found! ***

*** ERROR: Employee ID not found! ***

*** ERROR: Employee ID not found! ***

*** ERROR: Employee ID not found! ***

*** ERROR: Employee ID not found! ***

*** ERROR: Employee ID not found! ***

*** ERROR: Employee ID not found! ***

*** ERROR: Employee ID not found! ***

*** ERROR: Employee ID not found! ***

*** ERROR: Employee ID not found! ***

Employee 1 will be removed
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...