- #1
Erised
- 3
- 0
Problem in C, please help :)
Hello all, So I'm learning C nowadays and I got stuck at this rather simple program. I have no idea what the problem in the program is so I hope someone can help.
The program is for reading the number of students in a class, likewise reading their names and scores in sci, math and english and then calculating the average and displaying the names and avg.#include<stdio.h>
#include<conio.h>
main()
{
char name[25];
float Sci, Math, Eng, average;
int n,i;
printf("Enter the number of students\n");
scanf("%d",&n);
for(i=0; i<n; i++)
{
puts("\nEnter name");
gets(name);
printf("\nScience=");
scanf("%f",&Sci);
printf("\nMath= ");
scanf("%f",&Math);
printf("\nEnglish= ");
scanf("%f",&Eng);
average=(Sci+Math+Eng)/3;
puts("\n\nName:");
puts(name);
printf("\nAverage Score= %f", average);
}
getch();
}
Now the problem is-
When i run the program, it first asks me to enter the no of students, after that it prints 'enter name' and 'science=' at the same time instead of waiting for me to enter the student's name in response to the 'enter name'.
So I only have the option of entering the scores and finally it displays 'Name:' and a blank line followed by the average.Another program with a for loop only for gets and puts works fine for a specified number of students works(I removed the score and average part), the problem comes in when i put the 'enter the number of students' statement and make the program for 'n' students instead of using a number there.
I'm using Dev C++ for programming, please help me out if anyone knows what the problem is. Okay I tested it on Turbo C++ IDE and it still shows the same problem.
Hello all, So I'm learning C nowadays and I got stuck at this rather simple program. I have no idea what the problem in the program is so I hope someone can help.
The program is for reading the number of students in a class, likewise reading their names and scores in sci, math and english and then calculating the average and displaying the names and avg.#include<stdio.h>
#include<conio.h>
main()
{
char name[25];
float Sci, Math, Eng, average;
int n,i;
printf("Enter the number of students\n");
scanf("%d",&n);
for(i=0; i<n; i++)
{
puts("\nEnter name");
gets(name);
printf("\nScience=");
scanf("%f",&Sci);
printf("\nMath= ");
scanf("%f",&Math);
printf("\nEnglish= ");
scanf("%f",&Eng);
average=(Sci+Math+Eng)/3;
puts("\n\nName:");
puts(name);
printf("\nAverage Score= %f", average);
}
getch();
}
Now the problem is-
When i run the program, it first asks me to enter the no of students, after that it prints 'enter name' and 'science=' at the same time instead of waiting for me to enter the student's name in response to the 'enter name'.
So I only have the option of entering the scores and finally it displays 'Name:' and a blank line followed by the average.Another program with a for loop only for gets and puts works fine for a specified number of students works(I removed the score and average part), the problem comes in when i put the 'enter the number of students' statement and make the program for 'n' students instead of using a number there.
I'm using Dev C++ for programming, please help me out if anyone knows what the problem is. Okay I tested it on Turbo C++ IDE and it still shows the same problem.
Last edited: