Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

My code and the intended output is displayed below. When I input the employees information, do it again, then get the error message as should happen...

My code and the intended output is displayed below. When I input the employees information, do it again, then get the error message as should happen all seems well. But then when i display the employee information it will only show me the last employee's information that I added and the second line will be filled with 0s. Curiously, it shows the correct information if I DONT get the error message. How do I get it to display the employee information consistently? I havent the foggiest in how to solve this. Please help.

#define _CRT_SECURE_NO_WARNINGS

#define SIZE 2

#include <stdio.h>

// Define Number of Employees "SIZE" to be 2

// Declare Struct Employee

struct Employee {

int id, age;

double salary;

};

/* main program */

int main(void) {

int option = 0;

// Declare a struct Employee array "emp" with SIZE elements

// and initialize all elements to zero

struct Employee emp[SIZE] = { { 0 } };

int counter = 0;

int employeesNumber = 0;

printf("---=== EMPLOYEE DATA ===---nn");

do {

// Print the option list

printf("1. Display Employee Informationn");

printf("2. Add Employeen");

printf("0. Exitnn");

printf("Please select from the above options: ");

// Capture input to option variable

scanf("%d", &option);

printf("n");

switch (option) {

case 0: // Exit the program

break;

case 1: // Display Employee Data

// @IN-LAB

printf("EMP ID EMP AGE EMP SALARYn");

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

if (emp[0].id > 0) {

for (counter = 0; counter < SIZE; counter++)

printf("%6d%9d%11.2lfn", emp[counter].id, emp[counter].age, emp[counter].salary);

printf("n");

}

// 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 Employeen");

printf("=============== ");

// Check for limits on the array and add employee

// data accordingly.

if (employeesNumber < SIZE) {

printf("nEnter Employee ID: ");

scanf("%d", &emp[counter].id);

printf("Enter Employee Age: ");

scanf("%d", &emp[counter].age);

printf("Enter Employee Salary: ");

scanf("%lf", &emp[counter].salary);

printf("n");

employeesNumber++;

}

else {

printf("ERROR!!! MAXIMUM NUMBER OF EMPLOYEES REACHEDnn");

}

printf("n");

break;

default:

printf("ERROR: Incorrect Option: Try Againnn");

}

} while (option != 0);

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

return 0;

}

//PROGRAM OUTPUT IS SHOW BELOW

/*

---=== EMPLOYEE DATA ===---

1. Display Employee Information

2. Add Employee

0. Exit

Please select from the above options: 2

Adding Employee

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

Enter Employee ID: 111

Enter Employee Age: 34

Enter Employee Salary: 78980.88

1. Display Employee Information

2. Add Employee

0. Exit

Please select from the above options: 2

Adding Employee

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

Enter Employee ID: 112

Enter Employee Age: 41

Enter Employee Salary: 65000

1. Display Employee Information

2. Add Employee

0. Exit

Please select from the above options: 2

Adding Employee

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

ERROR!!! Maximum Number of Employees Reached

1. Display Employee Information

2. Add Employee

0. Exit

Please select from the above options: 1

EMP ID EMP AGE EMP SALARY

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

111 34 78980.88

112 41 65000.00

1. Display Employee Information

2. Add Employee

0. Exit

Please select from the above options: 0

Exiting Employee Data Program. Good Bye!!!

*/

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question