tetski
tetski

Reputation: 13

why is my program stopped working after the scanf?

i try to make a simple program to enter student information using struct and I'm so confused why is my program stopped right after i input the information here's the code

#include <stdio.h>
   struct studentScore {
   char nim[11];
   char name [30];
   char subjectCode [5];
   int sks;
   char grade;
   }students;


       int main(){
       int i,studNum;

        printf("Name : ");
        scanf("%s",students.name);

        printf("\nNIM :");
        scanf("%s",students.nim);

        printf("\nSubject Code :");
        scanf("%s",students.subjectCode);

        printf("\nSKS :");
        scanf("%d",&students.sks);    // Program Crash after i input the SKS

        printf("\nGrade :");
        scanf("%c",students.grade); 
        

    for(i=0;i<studNum;i++){
        printf("\nName : %s\n",students.name);
        printf("NIM  : %s\n",students.nim);
        printf("Subject Code  : %s\n",students.subjectCode);
        printf("SKS  : %d\n",students.sks);
        printf("Grade  : %c\n",students.grade);
    }
    return 0;
}

Upvotes: 1

Views: 115

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310960

The argument in this statement

scanf("%c",students.grade);

must be a pointer to the target object and place a blank before the conversion specifier

scanf( " %c", &students.grade );
       ^^^   ^^^

Also this for loop

for(i=0;i<studNum;i++){

does not make a sense at least because the variable studNum is not initialized and you do not have an array that requires a loop to output its elements.

Upvotes: 3

Related Questions