Home > OS >  Can't get the name and age of each student. Instead I get weird characters and age = 0?
Can't get the name and age of each student. Instead I get weird characters and age = 0?

Time:10-22

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

typedef struct Student {
    char name[50];
    int age;
} student;


int main() {
    char name[50];
    int age;

    // Requirement values
    char stop[] = "stop";
    int check = 1;

    int countOfStudents = 0;

    // Array with students
    student* students = malloc(countOfStudents);

    while(check) {

        scanf("%s", &name);

        if(!strcmp(name, stop) == 0) {

            scanf(" %i", &age);
            struct Student student = {*name, age};

            strcpy(students[countOfStudents].name, student.name);
            students[countOfStudents].age = student.age;

            countOfStudents  ;
        } else {
            printf("You wrote stop \n");
            check = 0;
        }
    }

    for (int i = 0; i < countOfStudents;   i) {
        printf("Name = %s , Age = %i", students[i].name, students[i].age);
        printf("\n");
    }

    return 0;
}

I have an array with students. Each student is a struct, and has a name & age. I have to register each student until the user write "stop".

Input:

test
12
stop

The output is like this:

You wrote stop
ogram Files (x86)
Name = t♀ , Age = 0

Why does this happen?

CodePudding user response:

At least this problem

Wrong allocation

int countOfStudents = 0;
student* students = malloc(countOfStudents);

Allocates zero mmmeory.

Try

int countOfStudents = 0;
int Students_MAX = 100;
student* students = malloc(sizeof *students * Students_MAX);

and limit iterations:

while(countOfStudents < Students_MAX && check)

Better to use a width limit

char name[50];
...
// scanf("%s", name);
scanf("Is", name);

struct Student student = {{*name, age}}; is not the needed initialization.

CodePudding user response:

memory allocation is wrong. the following code will work.

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

typedef struct Student
{
    char name[50];
    int age;
} student;

int main()
{
    char name[50];
    int age;

    char stop[] = "stop";
    int check = 1;

    int countOfStudents = 0;

    student* students = NULL;

    while(check)
    {
        scanf("Is", name); // 49   '\0' = 50
        if(!strcmp(name, stop) == 0)
        {
            scanf(" %i", &age);
            if(!students)
            {
                students = (student*) malloc(sizeof(student*)); // allocating for the first
            }
            else
            {
                students = (student*) realloc(students, sizeof(student*)); // allocating 
            }
            strcpy(students[countOfStudents].name, name);
            students[countOfStudents].age = age;
            countOfStudents  ;
         }
         else
         {
            printf("You wrote stop \n");
            check = 0;
         }
     }

    for (int i = 0; i < countOfStudents;   i) 
    {
        printf("Name = %s , Age = %i", students[i].name, students[i].age);
        printf("\n");
    }

    free(students);
    students = NULL;

    return 0;
}
  • Related