You have a 391 line program that could be reduced to less than 180 lines if you removed all that unnecessary whitespace. Generally, you only need one blank line to separate logical sections of code, and maybe sometimes you might use two blank lines to emphasize the separation between the sections, or even three blank lines, but you pretty much never need more than three blank lines anywhere as all that would do is make your code harder to read because readers have more to process vertically than they need. So here's my sensibly reduced version of your code:
Code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// Define Number of Employees "SIZE" to be 2
#define SIZE 4
// Declare Struct Employee
struct employee {
int age, id;
double salary;
};
/* main program */
int main(void) {
struct employee emp[4] = { 0 };
int option = 0;
int true = 0;
int i = 0;
int n1 = 0;
int n2 = 0;
int n3 = 0;
int true2 = 0;
// Declare a struct Employee array "emp" with SIZE elements
// and initialize all elements to zero
printf("---=== EMPLOYEE DATA ===---\n\n");
do {
// Print the option 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: ");
// Capture input to option variable
scanf("%d", &option);
printf("\n");
switch (option) {
case 0: // Exit the program
printf("Exiting Employee Data Program. Good Bye!!!");
printf("\n");
break;
case 1: // Display Employee Data
// @IN-LAB
printf("EMP ID EMP AGE EMP SALARY\n");
printf("====== ======= ==========\n");
for (i = 0; i < 4; i++)
{
if (emp[i].id != 0) {
printf("%6d%9d%11.2lf\n", emp[i].id, emp[i].age, emp[i].salary);
}
}
printf("\n");
// Use "%6d%9d%11.2lf" formatting in a
// printf statement to display
// employee id, age and salary of
// all employees using a loop construct
// The loop construct will be run for SIZE times
// and will only display Employee data
// where the EmployeeID is > 0
break;
case 2: // Adding Employee
// @IN-LAB
printf("Adding Employee\n");
printf("===============\n");
if (n1 < 4) {
for (i = 0; i < 4; i++)
{
if (emp[i].id == 0)
{
printf("Enter Employee ID: ");
scanf("%d", &emp[n1].id);
printf("Enter Employee Age: ");
scanf("%d", &emp[n1].age);
printf("Enter Employee Salary: ");
scanf("%lf", &emp[n1].salary);
printf("%d\n", n1);
i += SIZE;
}
}
n1++;
}
else {
printf("ERROR!!! Maximum Number of Employees Reached\n");
printf("\n");
}
// Check for limits on the array and add employee
// data accordingly.
break;
case 3:
printf("Update Employee Salary\n");
printf("===============\n");
do {
printf("Enter Employee ID: ");
scanf("%d", &n2);
for (i = 0; i < n1; i++)
{
if (n2 == emp[i].id)
{
true = 1;
printf("The current salary is %.2f\n", emp[i].salary);
printf("\nInput new salary: ");
scanf("%lf", &emp[i].salary);
}
}
if (true == 0)
{
printf("wrong number \n");
}
} while (true == 0);
break;
case 4:
printf("Remove Employee\n");
printf("===============\n");
true2 = 0;
do {
printf("Enter Employee ID: ");
scanf("%d", &n3);
for (i = 0; i < 4; i++) {
if (n3 == emp[i].id)
{
true2 = 1;
printf("Employee %d will be removed", emp[i].id);
emp[i].id = 0;
n1--;
}
}
if (true2 == 0) {
printf("wrong number \n");
}
} while (true2 == 0);
break;
}
if (option > 4)
{
printf("ERROR: Incorrect Option: Try Again\n");
printf("\n");
}
} while (option != 0);
return 0;
}
A few comments:
- You defined SIZE to be 4. That's good, so use SIZE instead of hardcoding 4 everywhere.
- Please avoid naming your variables n1, n2, n3. What on earth do they mean? I'm guessing that you might want to use variable names like employee_count and employee_id instead.
- What on earth does true and true2 mean? It looks like you are using them as boolean variables to denote whether or not an employee with a given id has been found, but that's very awkward when true can be false (oh, the contradiction!). Rather, use just one variable and name it something like employee_found.
- Avoid using a do while loop when you can express the logic easily with a while loop. For example, your code to update salary would be easier to understand when written as:
Code:
employee_found = 0;
while (!employee_found) {
printf("Enter Employee ID: ");
scanf("%d", &employee_id);
for (i = 0; i < SIZE; i++)
{
if (employee_id == emp[i].id)
{
employee_found = 1;
printf("The current salary is %.2f\n", emp[i].salary);
printf("\nInput new salary: ");
scanf("%lf", &emp[i].salary);
break;
}
}
if (!employee_found)
{
printf("wrong number \n");
}
}
(Notice that I fixed your for loop condition and added a break: you must loop for up to SIZE times, not employee_count number of times since the employee_count could be 1... with that valid employee being the last in the array.)
Originally Posted by
hzkids
So it can remove the employee, but once I add the new one, it will only show 3 of the employee data instead of all 4 of them.
This is probably due to your poor naming. You seemed to have used n1 here:
Code:
printf("Enter Employee ID: ");
scanf("%d", &emp[n1].id);
... when you wanted to use i instead. If you had named n1 as employee_count instead, this mistake would have been obvious.