我正在编写一个代码,允许用户输入多达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;
}
请阅读[如何调试小程序(由Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 SO不是调试服务。当你用你程序的逻辑确定问题时,如果你的代码的某些行为仍然让你想知道,那么通过一切手段发布一个问题。 – StoryTeller
谢谢你,不知道这样的东西存在。未来我遇到问题时,我一定会使用它。 – Jinto