gpt4 book ai didi

c - 如何显示和替换员工数据?

转载 作者:行者123 更新时间:2023-11-30 16:40:17 24 4
gpt4 key购买 nike

我对案例 3 部分感到困惑——通过浏览结构并检查输入是否匹配来更新员工信息。

The question:

"Prompt the user for the employee identification number using a do-while loop. While the number is not found in the employee array, keep prompting the user."

"Once the number is found, display the current salary for the employee with that identification number and prompt the user to input the new salary. Replace the old salary with the input value."

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#define SIZE 4

struct employee_data{
int Age;
int Int_Num;
double Salary;
};

int main(void){

int option = 0;
int NOE = 0;
printf("---=== EMPLOYEE DATA ===---\n");`enter code here`

struct employee_data emp[SIZE];
// Employee_ID
int i;
do {
// Print the option list
printf("\n1. 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!!!\n");
break;
case 1: // Display Employee Data
// @IN-LAB

printf("EMP ID EMP AGE EMP SALARY\n");
printf("====== ======= ==========\n");
for (i=0; i<SIZE; i++){
printf("%6d%9d%11.2lf\n", emp[i].Int_Num, emp[i].Age,emp[i]
// /printf("\n");
}
// printf("%6d%9d%11.2lf", emp[1].Age, emp[1].Int_Num,emp[1]
// 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
// printf("\n");
break;
case 2: // Adding Employee

// @IN-LAB

// for (i = 0; i < SIZE; i++)
printf("Adding Employee\n");
printf("===============\n");
if(NOE < SIZE){
// printf("ERROR!!! Maximum Number of Employees Reached\n"
printf("Enter Employee ID: ");
scanf("%d", &emp[NOE].Int_Num);
printf("Enter Employee Age: ");
scanf("%d", &emp[NOE].Age);
printf("Enter Employee Salary: ");
scanf("%11lf", &emp[NOE].Salary);

NOE++;
}
else{
printf("ERROR!!! Maximum Number of Employees Reached\n");
}
// Check for limits on the array and add employee
// data accordingly.}
break;

int number = 0;
case 3: printf("Update Employee Salary\n");
printf("======================\n");
do {

printf("Enter Employee ID: ");
scanf("%d", &number);
if(number == emp[NOE].Int_Num){
printf("The current salary is %11lf", emp[NOE].Salary);

}
// else{
}while (number != emp[NOE].Int_Num);
break;

最佳答案

您需要进行以下更改。首先将所有当前员工与给定的输入进行比较。如果不匹配,则提示用户再次询问员工识别号。

case 3: printf("Update Employee Salary\n");
printf("======================\n");
int flag = 0;
do{
printf("Enter Employee ID: ");
scanf("%d", &number);
int i;
for( i =0; i<NOE ; i++) //checking for each current employee
{
if(number == emp[i].Int_Num)
{
printf("The current salary is %11lf", emp[i].Salary);
printf("\nInput new salary");
scanf("%11lf", &emp[i].Salary);
flag = 1;
break;
}

}
} while (flag == 0);
break;

关于c - 如何显示和替换员工数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46740497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com