Need help With C

+1 vote
asked Oct 13, 2018 by Qibs (270 points)

CODE:

https://onlinegdb.com/By0PEhkjm​

I need a piece of code that will make me type 4 in and say than type in an employee number that doesn't exist and gives me an error printf .

https://imgur.com/a/KUcOrYf

2 Answers

0 votes
answered Oct 14, 2018 by Fady Serhan (420 points)
code not found
commented Oct 15, 2018 by anonymous
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#define SIZE 4

struct Employee {

int ID;

int AGE;

double SALARY;

};

int main(void) {

int option = 0,

i = 0,

employees = 0,

userID = 0,

new = 0,

flag = 0;

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

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

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:

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

break;

case 1:

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

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

for (i = 0; i < SIZE && i >= 0; i++) {

if (emp[i].ID > 0) {

printf("%6d%9d%11.2lf", emp[i].ID, emp[i].AGE, emp[i].SALARY);

printf("\n");

}

}

break;

case 2:

printf("Adding Employee\n");

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

if (employees < SIZE) {

printf("Enter employee ID: ");

scanf("%d", &emp[employees].ID);

printf("Enter employee Age: ");

scanf("%d", &emp[employees].AGE);

printf("Enter employee Salary: ");

scanf("%11lf", &emp[employees].SALARY);

employees++;

}

else {

printf("ERROR!!! Maximum Number of Employees Reached");

printf("\n");

}

break;

case 3:

do {

printf("Update Employee Salary\n");

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

printf("Enter Employee ID: ");

scanf("%d", &userID);

printf("\n");

for (i = 0; i < SIZE; i++) {

if (userID == emp[i].ID) {

printf("The current salary is %.2lf\n", emp[i].SALARY);

printf("Enter Employee New Salary: \n");

scanf("%lf", &emp[employees].SALARY);

flag = 1;

}

}

} while (flag != 1);

break;

case 4:

do {

printf("Remove Employee\n");

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

printf("Enter Employee ID: ");

scanf("%d", &userID);

for (i = 0; i < SIZE - 1; i++)

{

if (userID == emp[i].ID) {

new = i;

}

}

while (new < SIZE)

{

if (new == SIZE - 1)

{

emp[new].ID = 0;

emp[new].AGE = 0;

emp[new].SALARY = 0;

}

else

{

emp[new] = emp[new + 1];

}

new++;

}

employees--;

printf("Employee %d will be removed\n", userID);

}

while (SIZE < i);

break;

default:

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

}

} while (option != 0);

return 0;

}
0 votes
answered Oct 16, 2018 by Fady Serhan (420 points)
just do a for loop searching for the id of giving worker if not then Print ERR message, if you find him remove him from array by putting NULL for example
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...