【问题标题】:Checking to see if struct array is full检查结构数组是否已满
【发布时间】: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 不是调试服务。当您发现程序逻辑的问题时,如果您的代码的某些行为仍然让您感到疑惑,那么请务必提出问题。
  • 谢谢你,不知道有这样的东西存在。以后遇到问题我一定会用的。

标签: c arrays loops structure


【解决方案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");

        }
}

【讨论】:

    【解决方案2】:

    您可以使用普通的 printf() 语句进行调试,至少对于此代码。 检查完整的比较有问题,你为什么要比较 intstruct 在此处输入 if (emp[i].ID &gt; 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 2020-03-11
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      • 1970-01-01
      相关资源
      最近更新 更多