我正在编写一个代码,允许用户输入最多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 :(得分:1)
您可以使用普通的printf()语句进行调试,至少对此代码进行调试。
要检查完整的比较存在问题,为什么要将int
与struct
类型进行比较if (emp[i].ID > emp[SIZE])
我建议如下:
使用0值初始化整数:
int i = 0;
使用不同的计数器假设j用于显示数组的内容,以便保存i的值
for(int j = 0;j<SIZE;j++) {
printf("%d %d %11.2lf\n", emp[j].ID, emp[j].AGE, emp[j].SALARY);
}
检查i是否充实
if(i >= SIZE) {
printf("Full");
break;
}
答案 1 :(得分:1)
我添加了一个变量循环,用于在用户输入0作为选项时离开while
循环。
添加另一个变量number_of_employees
以跟踪员工数量。它初始化为0&amp;每次添加新用户时都会增加。
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");
}
}