Home > Back-end >  Program doesnt show the things I want it to show
Program doesnt show the things I want it to show

Time:12-27

I am currently trying to write a program in which when the user selects "1" It will ask him the number of students.It will then create a dynamic array and store all of their information in there.However I am running into the problem where when I try to print the information of the students my program just spews out numbers and letters.Here is the program:

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

typedef struct student{
int id;
char name[50];
char surname[50];
int semester;
float grade;
}student;


int main()
{
    int x,std,i;
    x=0;
    student** info;
    
    while (x!=8)
    {
        printf("1. Initialize student list\n2. Add a student record\n3. Delete a student record\n4. Display a student record by student surname\n5. Display students passed\n6. Display students failed\n7. Display all student records\n8. Exit");
        printf("\nYour choice: ");
        scanf("%d",&x);

        if (x==1)
        {
            printf("How many new students? ");
            scanf("%d",&std);
            struct student come[std];
            struct student *ptr = NULL;
            ptr=come;
            info = (student**)malloc(std*sizeof(student*));
            
            for(i = 0; i < std; i  ){
               printf("Enter detail of student #%d\n", (i   1));
               ptr->id=i 1;
               printf("Enter first name: ");
               scanf("%s", ptr->name);
               printf("Enter last name: ");
               scanf("%s", ptr->surname);
               printf("Enter semester: ");
               scanf("%d", &ptr->semester);
               printf("Enter grade: ");
               scanf("%f", &ptr->grade);
               ptr  ;
            }
            

        }
    
        else if (x==2)
        {
            /* code */
        }
        else if (x==3)
        {
            /* code */
        }
        else if (x==4)
        {
            /* code */
        }
        else if (x==5)
        {
            /* code */
        }
        else if (x==6)
        {

        }
        else if (x==7)
        {
            struct student come[std];
            struct student *ptr = NULL;
            ptr=come;
            for(i = 0; i < std; i  ){
               printf("%d", ptr->id);
               printf("%s", ptr->name);
               printf("%s", ptr->surname);
               printf("%d", ptr->semester);
               printf("%f", ptr->grade);
               printf("\nn");
               ptr  ;

            }

        }
    }

At first I used come[i].grade for all of them (name,id,etc) but it would return something along the lines of 19845241814DNva1280.000000 . Then I added the "&" before it but it gave me 6422088oIv4DNva64221920.000000. The last thing i tried is going with ptr->id but no luck.It just returned to me 19845241814DNv`a1280.000000. The same thing as the first.

CodePudding user response:

The problem is how you use the pointers. There is no necessary use of a pointer to pointer.

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

typedef struct student{
int id;
char name[50];
char surname[50];
int semester;
float grade;
}student;


int main()
{
    int x,std,i;
    x=0;
    struct student *ptr = NULL;
    
    while (x!=8)
    {
        printf("1. Initialize student list\n2. Add a student record\n3. Delete a student record\n4. Display a student record by student surname\n5. Display students passed\n6. Display students failed\n7. Display all student records\n8. Exit");
        printf("\nYour choice: ");
        scanf("%d",&x);

        if (x==1)
        {
            printf("How many new students? ");
            scanf("%d",&std);
            ptr = (student*)malloc(std*sizeof(student));
            
            for(i = 0; i < std; i  ){
               printf("Enter detail of student #%d\n", (i   1));
               ptr[i].id=i 1;
               printf("Enter first name: ");
               scanf("%s", ptr[i].name);
               printf("Enter last name: ");
               scanf("%s", ptr[i].surname);
               printf("Enter semester: ");
               scanf("%d", &ptr[i].semester);
               printf("Enter grade: ");
               scanf("%f", &ptr[i].grade);
            }
            

        }
    
        else if (x==2)
        {
            /* code */
        }
        else if (x==3)
        {
            /* code */
        }
        else if (x==4)
        {
            /* code */
        }
        else if (x==5)
        {
            /* code */
        }
        else if (x==6)
        {

        }
        else if (x==7)
        {
            for(i = 0; i < std; i  ){
               printf("%d", ptr[i].id);
               printf("%s", ptr[i].name);
               printf("%s", ptr[i].surname);
               printf("%d", ptr[i].semester);
               printf("%f", ptr[i].grade);
               printf("\n");
            }

        }
    }
}

CodePudding user response:

your fundamental problem is that you create a fresh student table for each command (1 and 7). So in the display command you are displaying n unitialized student records.

You also seem to have got into 'grasping at straws' mode for storing the student records, you have pointers, mallocs, dynamically sized stack arrays, 2 dimensional malloced arrays....

option 1 should malloc an array of students of the correct size. The array pointer should be declared before any of the loop code , so that yiou use the same array all the time

   student *info = NULL;
   int students=0;
   ...
   while (x!=8){
      .....
      if(x==1){
         ....
         info = malloc(std*sizeof(student));
      }

you now have a correctly sized array . to add a student

      scanf("%s", info[students].name);

etc

      then `students  ` to keep count of the number of active students 

  
  • Related