gpt4 book ai didi

检查结构数组是否已满

转载 作者:太空宇宙 更新时间:2023-11-04 08:06:51 25 4
gpt4 key购买 nike

我正在编写一个代码,允许用户输入最多 2 名员工的信息,但是,如果用户在数组已经包含 2 名员工的信息时尝试添加另一名员工,则会出现一条错误消息。目前代码似乎只是覆盖员工信息而不是输出错误消息。

此外,按照目前的代码,您必须在菜单出现之前输入两组员工数据。有没有办法让你输入一组员工数据,然后出现菜单?这是我的代码:

#include <stdio.h>

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

struct Employee{
int ID;
int AGE;
double SALARY;
};
//Declare Struct Employee


/* main program */
int main(void) {

int option = 0;
int i;
struct Employee emp[SIZE];

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

// Declare a struct Employee array "emp" with SIZE elements
// and initialize all elements to zero

do {
// Print the option list
printf("\n");
printf("1. Display Employee Information\n");
printf("2. Add 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. Goodbye!!!\n");

break;
case 1: // Display Employee Data
// @IN-LAB

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

//Use "%6d%9d%11.21f" formatting in a
//printf statement to display
//employee id, age and salary of
//all employees using a loop construct

for(i=0; i<SIZE; i++) {
printf("%d %d %11.2lf", emp[i].ID, emp[i].AGE, emp[i].SALARY);
}

//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 (emp[i].ID > emp[SIZE]) {
printf("Full");
}

for(i=0;i>SIZE;i++) {
printf("Error");
}

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

printf("\nEnter employee ID: ");
scanf ("%d", &emp[i].ID);

printf("\nEnter employee Age: ");
scanf ("%d", &emp[i].AGE);

printf("\nEnter employee Salary: ");
scanf ("%11lf", &emp[i].SALARY);
}


//Check for limits on the array and add employee
//data accordingly

break;

default:

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

}

} while (option!= 0);

return 0;

}

最佳答案

我添加了一个变量循环,用于在用户输入 0 作为选项时离开 while 循环。
添加了另一个变量 number_of_employees 以保持跟踪员 worker 数。每次添加新用户时,它都会初始化为 0 并递增。

int option = 0;
int i;
int loop = 1; /* This variable is used to terminate the programme by exiting the while loop */
int number_of_employees = 0; /* We add this variable that increments every time a new employee is added */
struct Employee emp[SIZE];

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

// Declare a struct Employee array "emp" with SIZE elements
// and initialize all elements to zero

while(loop) {
// Print the option list
printf("\n");
printf("1. Display Employee Information\n");
printf("2. Add 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. Goodbye!!!\n");
loop = 0; // Exiting
break;

case 1: // Display Employee Data
// @IN-LAB

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

//Use "%6d%9d%11.21f" formatting in a
//printf statement to display
//employee id, age and salary of
//all employees using a loop construct

for(i=0; i<SIZE; i++) {
printf("%d %d %11.2lf", emp[i].ID, emp[i].AGE, emp[i].SALARY);
}

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

/* This is how to check if we can add an employee */
if (number_of_employees < size) {

printf("\nEnter employee ID: ");
scanf ("%d", &emp[number_of_employees].ID);

printf("\nEnter employee Age: ");
scanf ("%d", &emp[number_of_employees].AGE);

printf("\nEnter employee Salary: ");
scanf ("%11lf", &emp[number_of_employees].SALARY);

/* Inceremeting */
number_of_employees++;
}

else {
printf("Full");
}

//Check for limits on the array and add employee
//data accordingly

break;

default:

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

}
}

关于检查结构数组是否已满,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42309206/

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