+ 1
How to have multiple inputs in C?
https://code.sololearn.com/cXBxbjidQfeh/?ref=app in the above code ive written, when i run it, only the first statement gets an input (ie :- mid1). All statements following it (ie :- mid2 and finals) simply get printed without ever asking for input? Why is that??
13 Réponses
+ 2
Yusha Arif I'm not sure what is wrong with the CodeBlocks, maybe you can check if the same behaviour occurs also with your other codes, or is the problem only with this code?. Anyways, I edited the code a bit, only adding some printf calls to show the inputs, try if this one works ...
#include <stdio.h>
int main() {
float mid1, mid2, finals, perct;
printf ("Please enter marks in Mid1: ");
scanf ("%f", &mid1);
printf("%f\n", mid1);
printf ("Please enter marks in Mid2: ");
scanf ("%f", &mid2);
printf("%f\n", mid2);
printf ("Please enter marks in Finals: ");
scanf ("%f", &finals);
printf("%f\n", finals);
perct = (mid1 + mid2 + finals) / 100.0;
printf ("Your Percentage is %.3f", perct );
return 0;
}
+ 4
Yusha Arif try using 'fflush(stdin);' before each scanf
+ 2
Hello Yusha Arif, did you input the numbers on separate lines in the input dialog? I tested the code works as expected, just enter the numbers in separate lines in input dialog, for example:
7.56
8.47
6.93
That will assign the values for mid1, mid2 and finals, respectively : )
Hth, cmiiw
+ 2
Unfortunately in Sololearn you have to give all inputs in advance.
+ 1
I havent tried it that way but what i wanted was that the program would 1st ask to enter marks of mid1. Once entered, it would then ask marks for mid2 and then finally for finals. After that it would print the %.
write now it only asks me for marks for mid1
+ 1
thanks alot Ipang
that really did help solve the problem. I havent tried it yet on codeblocks but the display does look better now on the Sololearn editor.
+ 1
No, all the previous codes ive written have worked well on codeblocks. I thought that maybe i was making a logical mistake in taking the inputs because of which the program wasnt working the way i wanted it to. I hope this little adjustment makes the program work
+ 1
Well yeah the only difference is that it now prints the input. But the problem with Codeblocks was that the output would look something like
Please enter marks for mid1 :
Once a enter the marks and press ENTER
the screen now shows
Please Enter marks for mid2:-
Please Enter marks for finals:-
Your Percentage is 0.00
It doesnt even ask for inputs for mid2 and finals
+ 1
its strange cuz i do mention the function scanf() and since a compiler compiles the program line by line, it shouldve stopped at the statement where asked to enter marks for mid2 and similarily for finals. But it completely ignored those lines and simply printed the statements without taking inputs
0
But ive tried this on my PC using Codeblocks and still it doesnt work
0
You're welcome Yusha Arif, hopefully it also works in CodeBlocks, if it keeps giving you hard time maybe you need to re-install it : )
0
Alright then, I hope it works there too, I didn't see any logical problem with the code and it also worked in Code Playground, even before I edited it, only difference that it now prints the inputs.
Good luck!
0
Well Idk what to say, I wish though, you had mentioned the problem was in CodeBlocks in the original post, that way people with better understanding of CodeBlocks can help. Post back here how it goes with CodeBlocks later okay?