【发布时间】:2017-02-17 23:33:19
【问题描述】:
我正在编写一个代码,允许用户输入最多 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;
}
【问题讨论】:
-
请阅读How to debug small programs (by Eric Lippert)。 SO 不是调试服务。当您发现程序逻辑的问题时,如果您的代码的某些行为仍然让您感到疑惑,那么请务必提出问题。
-
谢谢你,不知道有这样的东西存在。以后遇到问题我一定会用的。