how to update values using switch cases

Devin Mahadeo Source
#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;
}

in my case three im having trouble trying use id numbers entered from my case 2,no matter what i enter in case 3 it gives my error message and it also doesnt show my salary that was also entered in case 2

carraysdata-structuresstruct

Answers

answered 5 days ago Banzay #1

I think you got a mistake here:

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

You have to get a variable emp[counter].sal value, but not its address &emp[counter].sal

comments powered by Disqus