Home > Mobile >  While loop not stopping in C programmming
While loop not stopping in C programmming

Time:03-06

    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.

CodePudding user response:

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("Is", name);
  •  Tags:  
  • c
  • Related