for loop counter problem

• 05-05-2009
karipap
for loop counter problem
When i enter the Total No of Student: 2
It will appear as 3 students on the for loop.

How to fix this?
thank you.

Code:

```void main() {     char code[15], name[15], grade;     int student, A=0, B=0, C=0, F=0, y=1, x, i=0;     float midterm, final, total, quizm, assignment;         printf("\nEnter Subject Code : ");     gets(code);     printf("Enter Subject Name : ");     gets(name);         printf("Enter Total No of Student : ");     scanf("%d",&student);                 for(i=0; i<=student; i++);     {           printf("\n------------------------------------");           printf("\n          STUDENT %d                ", i);           printf("\n------------------------------------"); } }```
• 05-05-2009
BEN10
Quote:

Originally Posted by karipap
When i enter the Total No of Student: 2
It will appear as 3 students on the for loop.

How to fix this?
thank you.

Code:

```void main() {     char code[15], name[15], grade;     int student, A=0, B=0, C=0, F=0, y=1, x, i=0;     float midterm, final, total, quizm, assignment;         printf("\nEnter Subject Code : ");     gets(code);     printf("Enter Subject Name : ");     gets(name);         printf("Enter Total No of Student : ");     scanf("%d",&student);                 for(i=0; i<=student; i++);     {           printf("\n------------------------------------");           printf("\n          STUDENT %d                ", i);           printf("\n------------------------------------"); } }```

You enter the total number of students as variable "student" but in the for loop you are printing the variable "i"(and think that it'll give the total number of students).
• 05-05-2009
karipap
Code:

```void main() {     char code[15], name[15], grade;     int student, A=0, B=0, C=0, F=0, y=1, x, i=0;     float midterm, final, total, quizm, assignment;         printf("\nEnter Subject Code : ");     gets(code);     printf("Enter Subject Name : ");     gets(name);         printf("Enter Total No of Student : ");     scanf("%d",&student);                 for(i=0; i<=student; i++);     {           printf("\n------------------------------------");           printf("\n          STUDENT %d                ", student);           printf("\n------------------------------------"); } }```
thank you!!!
• 05-05-2009
EVOEx
The problem is that you set i to 0 and then continue up until the number of students. Let's say the number of students is 2 that means i will become 0, 1, 2.
If you want i to become 1 and 2, start with "i = 1" in stead of "i = 0". If youwant i to become 0 and 1, do "i < students" in stead of "i <= students".

Whatever you prefer.
• 05-05-2009
BEN10
Quote:

Originally Posted by karipap
Code:

```void main() {     char code[15], name[15], grade;     int student, A=0, B=0, C=0, F=0, y=1, x, i=0;     float midterm, final, total, quizm, assignment;         printf("\nEnter Subject Code : ");     gets(code);     printf("Enter Subject Name : ");     gets(name);         printf("Enter Total No of Student : ");     scanf("%d",&student);                 for(i=0; i<=student; i++);     {           printf("\n------------------------------------");           printf("\n          STUDENT %d                ", student);           printf("\n------------------------------------"); } }```
thank you!!!

By the way don't use void main, it has a return type int and also don't use gets to input strings.
For both of the above see the FAQ