1

My program "total marks" is not showing correctly? Why?

This is my program...

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

struct student
{
    char name[30];
    int marks[5];
};
struct std_date
{
    int date;
    int total;
    struct student add;
};
int main()
{
    struct std_date std[10];
    int i;
    int j;
    for(i=0;i<2;i++)
    {
        printf("Please enter the name of the student:\n");
        scanf("%s",std[i].add.name);
        fflush(stdin);
        printf("Please enter the marks of the student:\n");
        for(j=0;j<5;j++)
        {
            scanf("%d",&std[i].add.marks[j]);

        }
        fflush(stdin);
        printf("Please enter the result date of the student:\n");
        gets(std[i].date);
        fflush(stdin);
        printf("\n");
    }
    std[0].total=0;
    for(i=0;i<2;i++)
    {
        for(j=0;j<5;j++)
        {
            std[i].total+=std[i].add.marks[j];
        }
    }
    printf("\n\n");
    for(i=0;i<2;i++)
    {
        printf("This information for %s :\n", std[i].add.name);
        printf("Total marks: %d\n", std[i].total);
        printf("Result date:\n");
        puts(std[i].date);
        printf("\n");
    }
    return 0;

}

Suppose, output of my program

Input

Please enter the name of the student:
Nihan ahmed
Please enter the marks of the student:
1
2
3
4
5

Please enter the result date of the student:
22/5/2020 

Please enter the name of the student:
Marop hossain
Please enter the marks of the student:
2
3
4
5
6

Please enter the reault date of the student:
23/5/2020

output

This information for Nihan ahmed:
Total marks: 80
Result date: 22/5/2020

This information for Marop hossain:
Total marks: -130
Result date: 23/5/2020       
Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

0
int date;

date should be an character array (because you use gets(std[i].date); in your code):

char date[30]; 

You have to initialize value of std[1].total:

std[0].total=0;
std[1].total=0;

As the comment of @Barmar, do not use gets, use fgets instead. See Why is the gets function so dangerous that it should not be used?

And use : scanf("%29s",std[i].add.name); instead of scanf("%s",std[i].add.name);. See Disadvantages of scanf

I changed a bit in your code (see comment of code):

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

struct student
{
    char name[30];
    int marks[5];
};
struct std_date
{
    char date[30]; // max length of string ups to 29
    int total;
    struct student add;
};
int main()
{
    struct std_date std[10];
    int i;
    int j;
    for(i=0;i<2;i++)
    {
        printf("Please enter the name of the student:\n");
        scanf("%29s",std[i].add.name);
        printf("Please enter the marks of the student:\n");
        for(j=0;j<5;j++)
        {
            scanf("%d",&std[i].add.marks[j]);

        }

        printf("Please enter the result date of the student:\n");
        fgets(std[i].date, 30, stdin); // using fgets instead of gets
        printf("\n");
    }
    std[0].total=0;
    std[1].total=0; // init total = 0 of second student

    for(i=0;i<2;i++)
    {
        for(j=0;j<5;j++)
        {
            std[i].total+=std[i].add.marks[j];
        }
    }
    printf("\n\n");
    for(i=0;i<2;i++)
    {
        printf("This information for %s :\n", std[i].add.name);
        printf("Total marks: %d\n", std[i].total);
        printf("Result date:\n");
        puts(std[i].date);
        printf("\n");
    }
    return 0;

}

The output:

Please enter the name of the student:                                                                                     
std1                                                                                                                      
Please enter the marks of the student:                                                                                    
1                                                                                                                         
2                                                                                                                         
3                                                                                                                         
4                                                                                                                         
5                                                                                                                         
Please enter the result date of the student:                                                                              

Please enter the name of the student:                                                                                     
std2                                                                                                                      
Please enter the marks of the student:                                                                                    
3                                                                                                                         
2                                                                                                                         
4                                                                                                                         
5                                                                                                                         
1                                                                                                                         
Please enter the result date of the student:                                                                              



This information for std1 :                                                                                               
Total marks: 15                                                                                                           
Result date:                                                                                                              



This information for std2 :                                                                                               
Total marks: 15                                                                                                           
Result date: 
Hitokiri
  • 3,607
  • 1
  • 9
  • 29
  • If my program is for N number of students then, std[0].total = 0 and std[1].total=0, What would have happened instead? –  May 22 '20 at 17:20
  • you can use the for loop (`for(int i = 0; i < N; i++) {std[i].total = 0;}`) to initialize the total value of all students – Hitokiri May 22 '20 at 17:22