2017-02-17 59 views
0

我正在编写一个代码,允许用户输入多达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; 

} 
+2

请阅读[如何调试小程序(由Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 SO不是调试服务。当你用你程序的逻辑确定问题时,如果你的代码的某些行为仍然让你想知道,那么通过一切手段发布一个问题。 – StoryTeller

+0

谢谢你,不知道这样的东西存在。未来我遇到问题时,我一定会使用它。 – Jinto

回答

1

我添加了一个可变环,其用于当用户输入0作为一个选项离开while循环。
添加了另一个变量number_of_employees以跟踪员工数量。每次添加新用户时,初始化为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"); 

     } 
} 
1

您可以使用普通的printf()语句进行调试,至少为此代码进行调试。 完整检查比较有问题的,为什么你会比较int在这里if (emp[i].ID > emp[SIZE])

struct型我建议如下:

int i = 0; 

用途不同:

与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; 
}