0

I'm having trouble getting a struct pointer to take user input through fgets inside a function in a c program; I'm not sure what I'm doing wrong. The getInput() function is where the crash is occurring. I'm first trying to assign memory to the where the name is going to be stored with

*stu->name = (char*)malloc(N_LENGTH);

then getting input from the user with

fgets(*stu->name, N_LENGTH, stdin);

The program crashes during the first line and also the second line.

Sorry if I'm breaking any rules as this is my first time on the site.

Code:

#include <stdio.h>
#include <stdlib.h>

#define UNIT 100
#define HOUSE 1000
#define THRESH 12
#define DISCOUNT 10
#define NUM_PERSONS 5
#define N_LENGTH 30


struct student
{
    char *name;
    char campus;
    int userUnit;
};

void getInput(struct student *stu);
int amountCalc(struct student *stu);
void printOutput(struct student stu, int total);

int main()
{
    int total[NUM_PERSONS];
    int averageTotal=0;
    struct student tempStudent;
    struct student students[NUM_PERSONS];
    struct student *sPtr = &tempStudent;
    int i;
    for (i=0; i < NUM_PERSONS; i++)
    {
        getInput(sPtr);
        students[i]=tempStudent;
        total[i]=amountCalc(sPtr);
        averageTotal+=total[i];
    };

    for (i=0; i < NUM_PERSONS; i++)
    {
        printOutput(students[i], total[i]);
    };

    printf("\nThe average tuition cost for these %d students is $%.2f.\n",
            NUM_PERSONS, averageTotal/(NUM_PERSONS*1.0));
    return 0;
}

void getInput(struct student *stu)
{
        fflush(stdin);
        printf("Enter student name: ");
        *stu->name = (char*)malloc(N_LENGTH);
        fgets(*stu->name, N_LENGTH, stdin);

        printf("Enter y if student lives on campus, n otherwise: ");
        scanf(" %s", &stu->campus);

        printf("Enter current unit count: ");
        scanf(" %d", &stu->userUnit);

        printf("\n");
}

int amountCalc(struct student *stu)
{
        int total;
        total=(stu->userUnit)*UNIT;

        if (stu->userUnit>THRESH) {
            total-=((stu->userUnit)-12)*DISCOUNT;
        };

        if (stu->campus=='y') {
            total+=HOUSE;
        };
        return total;
}

void printOutput(struct student stu, int total)
{
    printf("\nStudent name: %s\n", stu.name);
    printf("Amount due: $%d\n\n", total);
}
Sandesh
  • 1,190
  • 3
  • 23
  • 41

2 Answers2

1

Your allocation is wrong. True allocation is like this ;

void getInput(struct student *stu)
{
    fflush(stdin);
    printf("Enter student name: ");
    stu->name = (char*)malloc(N_LENGTH);
    fgets(stu->name, N_LENGTH, stdin);

    printf("Enter y if student lives on campus, n otherwise: ");
    scanf(" %s", &stu->campus);

    printf("Enter current unit count: ");
    scanf(" %d", &stu->userUnit);

    printf("\n");
}

When you compile it, you can see as a warning. You should take care of all warnings. And casting malloc to (char *) is also unnecessary.

Hakkı I.
  • 11
  • 5
-1

Don't use fflush(stdin). See this question. I don't know if that causes the error but it is not defined in the C-standard so maybe your platform doesn't handle it!

The wrong allocation of memory is also a problem, look at @Hakkı Işık 's answer.

Community
  • 1
  • 1
izlin
  • 2,129
  • 24
  • 30