Irfan
Irfan

Reputation: 13

I'm trying this code and it shows some weird outputs

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


struct Sabjek
{
    int bm;
    int bi;
    int math;
    int sains;
};

struct Pelajar
{
    char studId[10];
    char studName[20];
    char clsRoom[20];
    struct Sabjek subMark;
    int totalMark;
};

void displays(struct Pelajar studs[])
{
    int i;
    printf("\t\tSTUDENTS INFO");

    for(size_t i=0;i<2;i++)
    {
        printf("\nStudent ID: %s", studs[i].studId);
        printf("\nStudent's name: %s", studs[i].studName);
        printf("\nClassroom: %s", studs[i].clsRoom);
        printf("\nSubject Marks:- ");
        printf("\nBahasa Melayu: %d", studs[i].subMark.bm);
        printf("\nBahasa Inggeris: %d", studs[i].subMark.bi);
        printf("\nMatematik: %d", studs[i].subMark.math);
        printf("\nSains: %d", studs[i].subMark.sains);
        printf("\n---------------------------------\n");
    }
}

int calcTtlMark(struct Pelajar studs[])
{
    int i, total;
    total= studs[i].subMark.bm+studs[i].subMark.bi+studs[i].subMark.math+studs[i].subMark.sains;
    return total;
}

int main()
{
    struct Pelajar studs[2];
    int i, sum;


    for(size_t i=0;i<2;i++)
    {
        printf("\nStudent ID: ");
        scanf("%s", &studs[i].studId);
        printf("\nStudent's name: ");
        scanf("%s", &studs[i].studName);
        printf("\nClassroom: ");
        scanf(" %[^\n]s", &studs[i].clsRoom);
        printf("\nSubject Marks:- ");
        printf("\nBahasa Melayu: ");
        scanf("%d", &studs[i].subMark.bm);
        printf("\nBahasa Inggeris: ");
        scanf("%d", &studs[i].subMark.bi);
        printf("\nMatematik: ");
        scanf("%d", &studs[i].subMark.math);
        printf("\nSains: ");
        scanf("%d", &studs[i].subMark.sains);
    }

    system("cls");
    displays(&studs[i]);

    printf("\t\tSTUDENTS TOTAL MARKS");


    for(size_t i=0;i<2;i++)
    {
        printf("\nTotal marks for student name: %s is ", studs[i].studName);
        sum=calcTtlMark(&studs[i]);
        printf("%d", sum);
    }
    printf("\n---------------------------------\n");

}

and the output I get is like this:

                STUDENTS INFO
Student ID: CB1917
Student's name: man
Classroom: mena
Subject Marks:-
Bahasa Melayu: 90
Bahasa Inggeris: 90
Matematik: 90
Sains: 90
---------------------------------

Student ID: HA3421
Student's name: to
Classroom: yaya an
Subject Marks:-
Bahasa Melayu: 40
Bahasa Inggeris: 40
Matematik: 40
Sains: 40
---------------------------------
                STUDENTS TOTAL MARKS
Total marks for student name: man is1970359234
---------------------------------

Total marks for student name: to is18494757
---------------------------------

I want the output to look like this:

                STUDENTS INFO
Student ID: CB1917
Student's name: man
Classroom: mena
Subject Marks:-
Bahasa Melayu: 90
Bahasa Inggeris: 90
Matematik: 90
Sains: 90
---------------------------------

Student ID: HA3421
Student's name: to
Classroom: yaya an
Subject Marks:-
Bahasa Melayu: 40
Bahasa Inggeris: 40
Matematik: 40
Sains: 40
---------------------------------
                STUDENTS TOTAL MARKS
Total marks for student name: man is 360
---------------------------------

Total marks for student name: to is 160
---------------------------------

How can i fix the problem at the bottom there? I tried finding solutions online but i couldn't come up to any answers

Upvotes: 1

Views: 48

Answers (1)

Yakov Galka
Yakov Galka

Reputation: 72449

Your i variable in calcTtlMark is uninitialized, thus causing it to return garbage. Frankly you don't even need i there, because you already pass the pointer to the right element. Therefore change it to:

int calcTtlMark(struct Pelajar *stud) {
    return stud->subMark.bm + stud->subMark.bi + stud->subMark.math + stud->subMark.sains;
}

This line is also accessing an uninitalized i:

displays(&studs[i]);

You're lucky that i happened to be zero here. You should still change it to:

displays(studs);

Upvotes: 2

Related Questions