0

I made this structure to store entries but when I compute the total I'm getting a garbage value.I went through it a lot of times but still could not find the mistake.I also tried initializing the total but couldn't get he required answer .Every time I compute the total it gives the same garbage value as earlier.

#include <stdio.h>
#include <stdlib.h>
typedef struct
{
    char name[50];
    int Assignment[5];
    int Test[2];
    int Endsem;
    int Total;



}student;
void read(student s[],int n)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        printf("Enter the name of the student");
        scanf("%s",&(s[i].name));
        printf("Enter the assignment marks \n ");
        for(j=0;j<5;j++)
        {
            scanf("%d",&(s[i].Assignment[j]));
        }
        printf("\n Enter the Test marks \n ");
        for(j=0;j<2;j++)
        {
            scanf("%d",&(s[i].Test[j]));
        }
        printf("\n Enter the EndSem marks \n");
        scanf("%d",&(s[i].Endsem));
        printf("\n \n ");
    }
}
void compute(student s[],int n)
{
    int i,j,d=0,m=0;

    for(i=0;i<n;i++)
    {
        s[i].Total=0;
        for(j=0;j<5;j++)
        {
          d+=(s[i].Assignment[j]);
        }
        for(j=0;j<2;j++)
        {
           m+=(s[i].Test);
        }
        s[i].Total=(d+m+(s[i].Endsem));
        printf("\n The total is %d out of 100",(s[i].Total));
    }
}
void display(student s[],int n)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        printf("The entries are");
        printf("%s",(s[i].name));
        printf("assignment marks \n ");
        for(j=0;j<5;j++)
        {
            printf(" \n %d",(s[i].Assignment[j]));
        }
        printf("\nTest marks \n ");
        for(j=0;j<2;j++)
        {
            printf("%d \n ",(s[i].Test[j]));
        }
        printf("\n EndSem marks \n");
        printf("%d \n",(s[i].Endsem));
    }
}
void main()
{
    student s[1];

    read(s,1);
    display(s,1);
    compute(s,1);
}

How to solve this?

4
  • Because you display before you compute? That means you will print the value of an uninitialized variable, whose value will be indeterminate and lead to undefined behavior. Commented Aug 30, 2016 at 16:01
  • 1
    Welcome to Stack Overflow. You can improve your question. Please read Minimal, Complete, and Verifiable example. When your code shows your precise problem with nothing extra, you are showing respect to those who volunteer to help you. Commented Aug 30, 2016 at 16:03
  • 1
    side note: scanf("%s",&(s[i].name)); --> scanf("%49s",s[i].name); Commented Aug 30, 2016 at 16:04
  • ...and even then, the first whitespace ends the scan, so Weather Vane would be truncated to Weather. Commented Aug 30, 2016 at 16:06

1 Answer 1

1

The problem is in

  m+=(s[i].Test);

Test is an array, not a normal (scalar) variable. It points to (or decays to) the starting address of the first element of the array. Adding that to an int makes no sense here.

You might want to write

 m+=(s[i].Test[j]);

That said, as I already commented, you should rewrite the scanf() statements for

  • including the maximum field width to prevent buffer overfloe, like scanf("%49s",...) for an argument of array size 50
  • pass the array name, instead of the address of array, as %s expects a pointer-to-char array, like scanf("%49s",s[i].name);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.