Looping problem

I'm having problems on this code. I don't know where the problem lies but whenever I try to run the program instead of the program asking for 3 inputs (id,name and percentage) in the first for statement, it asks for 4 on the very 1st iteration.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
	int id;
	char name[200];
	float percentage;
};
int main()
{
	int n;
	struct student record[2];
	for(n=0;n<3;n++)
	{
		printf("Enter student ID, name, and percentage respectively: \n");
		scanf("%d\n",&record[n].id);
		scanf("%s\n",record[n].name);
		scanf("%f\n",&record[n].percentage);
	}
	for(int n=0;n<3;n++)
	{
		printf("Student ID %d \n",record[n].id);
		printf("Student name %s \n",record[n].name);
		printf("Student percentage %f \n",record[n].percentage);
	}
	getch();
	return 0;
}
This line:

 
getch();


Asks for a fourth input (a char). Is this it?
I tried deleting that but it's still not right
Last edited on
The loop starting in line 14 will run for n = 0, 1, 2
However, you only have TWO student records - see line 13. These will be record[0] and record[1].

Your code could do anything ... but it's not likely to be sensible ... when n=2.
@lastchance Yep, this solves it.
Topic archived. No new replies allowed.