for loop counter problem

This is a discussion on for loop counter problem within the C Programming forums, part of the General Programming Boards category; When i enter the Total No of Student: 2 It will appear as 3 students on the for loop. How ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    16

    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------------------------------------");
    }
    }

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by karipap View Post
    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).
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    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!!!

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    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.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by karipap View Post
    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
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  2. problem in reading alphabets from file
    By gemini_shooter in forum C Programming
    Replies: 6
    Last Post: 03-09-2005, 12:49 PM
  3. String input problem, gets
    By willc0de4food in forum C Programming
    Replies: 13
    Last Post: 03-05-2005, 01:05 PM
  4. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 03:16 PM
  5. how to obtain first character of every other word
    By archie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2002, 12:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21