Help me with this code please. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help me with this code please.

I'm having problems with this code: #include <stdio.h> int main(void) { char nme[10]; int a,b,c,d,r,f; r=(a+b+c+d); f=r/4; printf("Name: "); scanf("%s",&nme); printf("Math grade: \n"); scanf("%d",&a); printf("Biology grade: \n"); scanf("%d",&b); printf("Chemestry grade: \n"); scanf("%d",&c); printf("History grade: \n"); scanf("%d",&d); printf("%s - %d\n",nme,f); if(f>=10){ puts("Passed!"); } else{ puts("Flunked!"); } return 0; } The output I get is weird, I am using linux but I don't think that shlould affect it. Do you an error in the syntax?

8th Mar 2019, 3:12 AM
Juan Sixto Nathan
Juan Sixto Nathan - avatar
2 Answers
+ 12
Hello Juan, The problem is that the code is summing <a>, <b>, <c> and <d> into <r> while their values has not been read from input by scanf(). Also the <f> value is calculated from <r> which was a sum of uninitialized variables (having undetermined values). Solution is to move the lines: r=(a+b+c+d); f=r/4; To a row above the following line, where all <a>,<b>,<c>, and <d> had been read and initialized. if(f>=10){ Hth, cmiiw
8th Mar 2019, 3:28 AM
Ipang
+ 1
Ipang i'll try and make the changes you suggested and see if it works Edt: I tried what you told me and it worked! I moved *r* and *f* and put it just after all the scanf()'s got their input, thanks very much.
8th Mar 2019, 3:38 AM
Juan Sixto Nathan
Juan Sixto Nathan - avatar