-1
    int math=0,phy=0,che=0,avg=0,cprg=0,n,i=1,sum=0,large;
    char name, roll_no, dept;
    printf("\n How many students : ");
    scanf("%d", &n);
    
    while(i<=n)
     {
        printf("NAME : ");
        scanf("%s", &name);
        printf("DEPARTMENT :");
        scanf(" %s", &dept);
        printf("ROLL NUMBER :");
        scanf("%s", &roll_no);  
        printf("☻ MATHS MARK :");
        scanf(" %d", &math);
        printf("☻ PHYSICS MARK : ");
        scanf("%d", &phy);
        printf("☻ CHEMISTRY MARK :");
        scanf(" %d", &che);
        printf("☻ C Programming :");
        scanf(" %d", &cprg);
        printf("\n");
        avg = math+phy+che+cprg;
        avg=avg/4;
        append(&head, avg);
        i++;
    }
    return 0;
}

Here is some of my code. I need to run this loop for the number of times the user enters in the input, and the loop is not ending in VS Code even though it works fine in online GDB.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
FarooQ
  • 3
  • 5
  • What have you learned through debugging? And this isn't an [mcve] since we don't know what `append` does. Please [edit] your question if you need help. – Stephen Newell Mar 04 '22 at 18:07
  • print i and n in each loop – stark Mar 04 '22 at 18:07
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Mar 04 '22 at 18:10
  • Even if using a debugger does not actually solve the problem, it should at least help you to isolate the problem and to create a [mre] of the problem, so that it will be easier for other people to help you. – Andreas Wenzel Mar 04 '22 at 18:10
  • 2
    *Always a bug*: not testing the return value from scanf. – Jens Mar 04 '22 at 19:00
  • Is `i=0` or `i=1` initially? – Neil Mar 04 '22 at 20:20
  • Why are you making the user enter the name, department, roll number when you just throw the information away? (Because that's what the instructions said?). What does `append()` do? What is `head`? You should do a sanity check on `n` before using it. What's the value of `i` at the top of the loop? At the bottom? How many people in the English department do maths, physics, chemistry and C programming? – Jonathan Leffler Mar 04 '22 at 22:20

1 Answers1

2

Your variables name, roll_no, and dept are just a single character, but you treat them (with scanf) as though they are strings. I think you want to declare them as strings as follows:

char name[50], roll_no[10], dept[10];

adjusting the string sizes as needed to hold the data. Because you are writing data beyond what is allocated you get undefined behavior.

You will also need to remove the & in the scanf call for each of these variables, e.g.:

scanf("%49s", name);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
SGeorgiades
  • 1,771
  • 1
  • 11
  • 11