1

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;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
tetski
  • 13
  • 4
  • What inputs do you give it? –  Sep 02 '21 at 07:38
  • Add a space to `scanf("%c",students.grade); ` to give `scanf(" %c",students.grade); ` Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Sep 02 '21 at 07:40
  • 3
    @Rookiez You have to write scanf(" %c",&students.grade); and this loop for(i=0;i – Vlad from Moscow Sep 02 '21 at 07:40
  • Indeed you forgot to input the number of students. You might have buffer overflow too from the unrestricted string inputs. You also need to make an *array* of the `struct`s. The simple answer is to remove that loop, and just work with 1 student. – Weather Vane Sep 02 '21 at 07:42

1 Answers1

2

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thanks for your solution, there was a loop for the student number on the first input but I deleted it because I was really confuse abt the error of it .Thanks It really helps me a lot !! – tetski Sep 02 '21 at 07:49