0

I'm making a program that sort of acts like a student records system using an array of structures within structures. The program allows adding, editing and viewing student profile and their corresponding information. I'm having trouble with my displayAll function, when checking if a structure is empty. Supposedly if no subject information has been added to a student profile yet I'm supposed to display a message saying so and display their subject they're enrolled in otherwise. But I'm quite confused how to do so. Some tips would be much appreciated.

I've omitted some parts of the code to put emphasis on the displayAll function.

Someone pointed out this thread: Checking if an array of structs is empty or not, but it doesn't really halp me fully as I am dealing with an array of structures within an array of structures.

#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");
                }
            }
        }



    }
}

}

4
  • Your displayAll function works. When the struct is empty, It is printing the message. I cannot understand what you are trying to say. Commented Jan 24, 2016 at 11:41
  • Many ways to do what you want. For example, have a counter in your student struct which records the current number of subjects added. Or have a flag in your subjs struct which records whether that struct is valid or not. Commented Jan 24, 2016 at 11:43
  • Possible duplicate of Checking if an array of structs is empty or not Commented Jan 24, 2016 at 11:53
  • @AshishAhuja No, it has undefined behaviour because when this code line if(info[i].course[j].courseCode == 0){ is reached first, the variable j has an unknown value. Commented Jan 24, 2016 at 11:56

1 Answer 1

1

You can use a flag to track wether a subject has been found in the subject for loop. I would name it found and clear it before the loop. Then set it within the loop, when a subject has been found. If the flag is still cleared after the loop, then print the desired message. To print the header "Subjects", you can check within the loop if a subject has been found (and printed) before.

Example code:

    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");
    }

This replaces the whole

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

block within the student loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.