Home > Software engineering >  Issues with printing structs in C
Issues with printing structs in C

Time:06-18

I'm incredibly new to this and have a school assignment I have to write a gradebook program that uses a custom struct to hold student IDs and grades. I have been trying unsuccessfully for days to figure out why it will not print properly, or when it does print (after a lot of shifting things around) it only prints the second set of input. I'm at my wit's end. Can anyone help?

The gradebook.h section is the custom structure.

// gradebook.h

struct gradeID
{
    int id;
    char grades[25];
};

// Gradebook.h is a header file to define
// a global structure.
    
#include "gradebook.h"
#include <stdio.h>
#include <stdlib.h>
    
void sort(struct gradeID *, int);
    
int main(void)
{
    // Variables and structure definitions
    int ctr;
    char contInput;
    int i;
    struct gradeID grade;
    struct gradeID *identifier;
    int *temps;
    
    // Allocates 10 integers worth of memory for the program to use.
    // If there is not enough memory, the program will print an error
    // statement and terminate the program.
    temps = (int *) malloc(10 * sizeof(int));
    if (temps == 0)
    {
        printf("Not enough memory!\n");
        exit(1);
    }
    
    // Prints basic instructions for the program
    printf("\t\tGradebook Recorder\n");
    printf("Input student IDs and grades.\n");
    printf("These will be sorted by ID and printed.\n");
    
    
    /* Creates a for loop that will continue until the program
       hits the designated number of array elements. For the sake
       of expediency, I have set this amount to 10, but it can be
       changed as necessary.*/
    for(i = 0; i < 10; i  )
    {
        printf("Input student ID:\n");
        scanf(" %d", &grade.id);
        printf("Input grade:\n");
        scanf(" %s", grade.grades);
    
        // This allows early exit of the program
        printf("Do you have more grades to enter?\n");
        printf("Y/N\n");
        scanf(" %c", &contInput);
    
        if(contInput == 'N' || contInput == 'n')
        {
            printf("Finalizing and printing input-\n\n");
            break;
        }
    
        ctr  ;
    }
    
    printf("Grades Sorted by Student ID:\n\n");
    printf("\tStudent ID: Student Grade: \n");
    
    for(i = 0; i < ctr; i  )
    {
        printf("\t%d", grade.id );
        printf("\t%s", grade.grades);
    }
    
    identifier[i] = grade;
    
    return(0);
    
    free(temps);
}
    
void sort(struct gradeID identifier[], int counter)
{
    int inner;
    int outer;
    struct gradeID temp;
    
    // Loops for sorting
    for(outer = 0; outer < counter - 1;   outer)
    {
        for(inner = outer   1; inner < counter;   inner)
        {
            if(identifier[inner].id < identifier[outer].id)
            {
                temp = identifier[inner];
                identifier[inner] = identifier[outer];
                identifier[outer] = temp;
            }
        }
    
    }
    return;
}

CodePudding user response:

The pointer identifier is uninitialized

struct gradeID *identifier;

So this statement

identifier[i] = grade;

independent on the value of i invokes undefined behavior.

In this for loop

for(i = 0; i < 10; i  )
{
    printf("Input student ID:\n");
    scanf(" %d", &grade.id);
    printf("Input grade:\n");
    scanf(" %s", grade.grades);

    // This allows early exit of the program
    printf("Do you have more grades to enter?\n");
    printf("Y/N\n");
    scanf(" %c", &contInput);

    if(contInput == 'N' || contInput == 'n')
    {
        printf("Finalizing and printing input-\n\n");
        break;
    }

    ctr  ;
}

you are entering new data in the same object grade of the structure type. So the new data overrides the previous data stored in the object.

Moreover the variable ctr was not initialized

int ctr;

So this statement in the above for loop

ctr  ;

also invokes undefined behavior.

The variable temps that points to a dynamically allocated array

temps = (int *) malloc(10 * sizeof(int));

is not used.

This statement

free(temps);

never gets the control because before it there is the return statement

return(0);

free(temps);

What you need is to define an array of the type struct gradeID as for example

struct gradeID grades[10];

and fill it with values.

  • Related