【问题标题】:Checking if an array of structures is 'empty' in C在 C 中检查结构数组是否为“空”
【发布时间】:2016-01-24 11:34:30
【问题描述】:

我正在制作一个类似于学生记录系统的程序,它使用结构中的结构数组。该程序允许添加、编辑和查看学生资料及其相应信息。在检查结构是否为空时,我的 displayAll 函数有问题。假设如果没有将主题信息添加到学生个人资料中,但我应该显示一条消息,说明并显示他们注册的主题。但我很困惑如何做到这一点。一些提示将不胜感激。

为了强调 displayAll 函数,我省略了部分代码。

有人指出了这个线程:Checking if an array of structs is empty or not,但它并没有完全阻止我,因为我正在处理结构数组中的结构数组。

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>

struct name{
    char fname[30];
    char lname[20];
    char mi;
};

struct local{
    char address[30];
    char city[20];
};

struct subjs{
    char courseCode[10];
    char courseDes[20];
    char grade;
};

struct student{
    char id[8];
    struct name studName;
    struct local place;
    struct subjs course[4];
};

void inputInfo(struct student *studInfo);   
void addSubjects(struct student *studInfo);
void displayAll(struct student info[], int limit);

int main(){
    struct student info[12];
    int i=0, j, courseLimit=0;
    char choice;
    char idVerify[8];

    do{
       printf("MENU");
       printf("\n\n[A] Add student Information");
       printf("\n[B] Add student subject");
       printf("\n[C] Edit student address or city");
       printf("\n[D] Edit subject grade");
       printf("\n[E] View individual student info/subjects");
       printf("\n[F] View all students with their corresponding subjects");
       printf("\n[g] Quit");
       printf("\n\nEnter choice: ");
       choice=tolower(getche());
       system("cls");

       switch (choice){
         case 'a': 
                inputInfo(&info[i]);
                i++;
                break;
         case 'b': 
                printf("Enter you id number for verification: ");
                gets(idVerify);
                for(j=0; j<i; j++){
                    if(strcmp(idVerify, info[j].id) == 0){
                        addSubjects(&info[j]);
                    }
                    else
                        printf("ID Number not found");
                }
                break;
         case 'c':
                //codes
                break;
         case 'd': 
                //codes
                break;
         case 'e':
                //codes 
                break;
         case 'f':
                displayAll(info, i);
                break;
         case 'g':
                printf("This program will now close.\nPress any key to continue.");
                break;
        default: printf("Invalid character. Try again");
                break;
    }

    getch();
    system("cls");

   }while (choice!='g');

}

 void inputInfo(struct student *studInfo){
  //codes
 }

void addSubjects(struct student *studInfo){
  //codes
 }

void displayAll(struct student info[], int limit){
   int i, j;
   if(limit == 0){
       printf("Records are empty");
    }

   else{
       for(i=0; i<limit; i++){
        printf("\nStudent Name: %s %c %s", info[i].studName.fname, info[i].studName.mi, info[i].studName.lname);
        printf("\nID Number: %s", info[i].id);
        printf("\nAddress and city: %s, %s", info[i].place.address, info[i].place.city);

        if(info[i].course[j].courseCode == 0){
            printf("\nNo enrolled subjects");
        }
        else{
            printf("\nSubjects:");
            for(j=0; j<4; j++){
                if(info[i].course[j].courseCode != 0){
                    printf("Subject %d", j+1);
                    printf("\nCourse Code: %s", info[i].course[j].courseCode);
                    printf("\nCourse Description: %s", info[i].course[j].courseDes);
                    printf("\nCourse Grade: %c", info[i].course[j].grade);
                    printf("\n");
                }
            }
        }



    }
}

}

【问题讨论】:

  • 您的displayAll 功能有效。当struct 为空时,它正在打印消息。我不明白你想说什么。
  • 多种方式来做你想做的事。例如,在 student 结构中有一个计数器,用于记录当前添加的主题数。或者在你的 subjs 结构中有一个标志来记录该结构是否有效。
  • @AshishAhuja 不,它具有未定义的行为,因为当首先到达此代码行 if(info[i].course[j].courseCode == 0){ 时,变量 j 具有未知值。

标签: c arrays structure


【解决方案1】:

您可以使用标志来跟踪是否在主题 for 循环中找到了主题。我将其命名为 found 并在循环之前将其清除。然后在找到主题后将其设置在循环中。如果循环后标志仍然被清除,则打印所需的消息。要打印标题“Subjects”,您可以在循环中检查之前是否已找到(并打印)主题。

示例代码:

    int found = 0; // clear flag
    for(j=0; j<=4; j++){
        if(info[i].course[j].courseCode != 0){
            if(!found) { // if true then this will be the first subject to print
                printf("\nSubjects:");
            }
            found = 1; // set flag
            printf("Subject %d", j);
            // the other printfs
        }
    }
    if(!found) {  // check flag
         printf("No enrolled subjects.\n");
    }

这取代了整个

    if(info[i].course[j].courseCode == 0){
        ...
    } else {
        ...
    }

在学生循环中阻塞。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 2017-09-13
    • 2014-11-12
    相关资源
    最近更新 更多