как обновить значения с помощью переключателей - PullRequest
0 голосов
/ 12 октября 2018
#include <stdio.h>

#define SIZE 4

struct Employee
{
    int no; // Employee ID
    int age; // Employee Age
    double sal; // Employee Salary
};
int main() {

    int option = 0;
    struct Employee emp[SIZE] = { {0} }; // declear the array and initialize the elements to 0
    int counter = 0;
    int count = 0;
    int idstore = 0;
    double salnew = 0;

    printf("---=== EMPLOYEE DATA ===---\n\n");


    do
    {
        //print options list
        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: ");

        // User input for the options 
        scanf("%d", &option);
        printf("\n");


        switch (option)
        {
        case 0: // Exit the program
            printf("Exiting Employee Data Program. Good Bye!!!");
            break;
        case 1: // Display employee data 

            printf("EMP ID EMP AGE EMP SALARY\n");
            printf("====== ======= ==========\n");

            for (counter = 0; counter < SIZE; counter++)
            {
                printf("%6d%9d%11.2lf\n", emp[counter].no, emp[counter].age, emp[counter].sal);
            }
            printf("\n");

            break;

        case 2: // Adding Employees

            printf("Adding Employee\n");
            printf("===============\n");

            if (count < SIZE)
            {
                printf("Enter Employee ID: ");
                scanf("%d", &emp[count].no);
                printf("Enter Employee Age: ");
                scanf("%d", &emp[count].age);
                printf("Enter Employee Salary: ");
                scanf("%lf", &emp[count].sal);
                count++;
            }
            else
            {
                printf("ERROR!!! Maximum Number of Employees Reached\n");
            }
            printf("\n");
            break;

        case 3: // Update Employee Salary

            do
            {
                printf("Update Employee Salary\n");
                printf("======================\n");
                printf("Enter Employee ID: ");
                scanf("%d", &idstore);

                for (counter = 0; counter < SIZE; counter++)
                {
                    if (idstore == emp[count].no)
                    {
                        printf("The current salary is %lf\n", &emp[count].sal);
                        printf("Enter Employee New Salary: ");
                        scanf("%lf", &emp[count].sal);
                    }
                    else
                    {
                        printf("*** ERROR: Employee ID not found ***\n");
                        printf("Enter Employee ID: ");
                        scanf("%d", &idstore);
                    }

                }

            } while (idstore = 0);
            break;

        default:
            printf("ERROR: Incorrect Option: Try Again\n\n");
        }

    } while (option != 0);


    return 0;
}

в моем случае три у меня возникают проблемы при попытке использовать идентификаторы, введенные из моего случая 2, независимо от того, что я ввожу в случае 3, он выдает мое сообщение об ошибке, а также не показывает мою зарплату, которая также была введена в случае 2

1 Ответ

0 голосов
/ 12 октября 2018

Я думаю, что вы ошиблись здесь:

printf("The current salary is %lf\n", &emp[counter].sal);

Вы должны получить значение переменной emp[counter].sal, но не ее адрес &emp[counter].sal

...