Thread: Looping problem...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    67

    Looping problem...

    Please view my code below...
    Code:
    #include <winsock.h>
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define CLASS_SIZE 60
    #define subjects 5
    
        //====================================================================================//
    
    
        int main(void) {
    
            char input[CLASS_SIZE][subjects], token[81];
            int c_size; 
            int i, j;
            float gp, total_gp = 0;
            float gpa;
            int count = 0;
    
            //===================================================================================//
    
            
            // Ask for class size
            printf("Input class size (num of students): ");
            scanf("%d", &c_size);
    
            // Looping grade question
            for(i=0; i<c_size; i++)
            {
                for(j=0; j<subjects; j++)
                {
                        printf("\nInput grade for student %d in subject %d: ",i+1,j+1);
                        fflush(stdin);
                        scanf("%80s", &token);
    
                        /* good grade code entered */
                        if ((strlen(token) == 1) && (strstr("abcdfABCDF", token) != NULL))
                        {
                            /* copy the code and make it uppercase */
                            input[i][j] = toupper(token[0]); 
                        }
    
                        /* bad grade code entered */
                        while ((strlen(token) == 1) && (strstr("abcdfABCDF", token) == NULL)) {
    
                            /* Reinput question */
                            printf("\nBad grade code entered.");
                            printf("\nGrade ranges from A to D and F only."); 
                            printf("\n\nInput new grade for student %d in subject %d: ",i+1,j+1);
                            fflush(stdin);
                            scanf("%80s", &token); 
    
                            /* copy the code and make it uppercase */
                            input[i][j] = toupper(token[0]);
                        };
    
                }
    
            }
    
            
            //=================================================================================//
    
    
            for(i=0; i<c_size; i++)
            {
    
                printf("\nStudent %d\t", i+1);
    
                for(j=0; j<subjects; j++)
                {
    
                    if(input[i][j] == 'A') {
    
                        gp = 4;
                        count++; 
                
                    }
    
                    else if(input[i][j] == 'B') {
    
                        gp = 3;
                        count++;
                
                    }
    
            
                    else if(input[i][j] == 'C') {
                
                        gp = 2;
                        count++;
                
                    }
    
                    else if(input[i][j] == 'D') {
                        
                        gp = 1;
                        count++;
                        
                    }
    
                    else {
                        
                        gp = 0;
                        count++;
                        
                    }
    
                    total_gp = total_gp + gp;
            
                    printf("%c\t", input[i][j]);
    
                }
    
                gpa = (total_gp / count);
    
                printf("%.1f", gpa);
                
                printf("\n");
                
            
            }
    
            
    
            return 0;
    
        }
    Now, pls view below image...

    Looping problem...-z9235untitledk165-jpg

    My problem is the student 2 gpa should 2.0 because (2+2+2+2+2)/5 but in my program it display 3.0...so how could I solve this problem? Very thanks you.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You have couple of problems first in the following snippet:
    Code:
    fflush(stdin);
                        scanf("%80s", &token);
    The fflush() function is not designed to work with input streams, it is undefined in the standard for input streams. You do not need the ampersand when dealing with C-strings.

    The next problem is in your printout you must zero out your count and total_gp variables before going to the next student.

    Jim

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Thanks you ~ You solve my problem ^^

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    First pls view my code below...
    Code:
    #include <winsock.h>
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define CLASS_SIZE 60
    #define subjects 5
    
        //====================================================================================//
    
    
        int main(void) {
    
            char input[CLASS_SIZE][subjects], token[81];
            int c_size; 
            int i, j;
            float gp, total_gp = 0;
            float gpa, average_gpa, total_gpa = 0;
            int count = 0, countc = 0;
            float min, max;
    
            //===================================================================================//
    
            
            // Ask for class size
            printf("Input class size (num of students): ");
            scanf("%d", &c_size);
    
            // Looping grade question
            for(i=0; i<c_size; i++)
            {
                for(j=0; j<subjects; j++)
                {
                        printf("\nInput grade for student %d in subject %d: ",i+1,j+1);
                        scanf("%80s", &token);
    
                        /* good grade code entered */
                        if ((strlen(token) == 1) && (strstr("abcdfABCDF", token) != NULL))
                        {
                            /* copy the code and make it uppercase */
                            input[i][j] = toupper(token[0]); 
                        }
    
                        /* bad grade code entered */
                        while ((strlen(token) == 1) && (strstr("abcdfABCDF", token) == NULL)) {
    
                            /* Reinput question */
                            printf("\nBad grade code entered.");
                            printf("\nGrade ranges from A to D and F only."); 
                            printf("\n\nInput new grade for student %d in subject %d: ",i+1,j+1);
                            fflush(stdin);
                            scanf("%80s", &token); 
    
                            /* copy the code and make it uppercase */
                            input[i][j] = toupper(token[0]);
                        };
    
                }
    
            }
    
            
            //=================================================================================//
    
    
            for(i=0; i<c_size; i++)
            {
    
                printf("\nStudent %d\t", i+1);
    
                for(j=0; j<subjects; j++)
                {
    
                    if(input[i][j] == 'A') {
    
                        gp = 4;
                        count++; 
                
                    }
    
                    else if(input[i][j] == 'B') {
    
                        gp = 3;
                        count++;
                
                    }
    
            
                    else if(input[i][j] == 'C') {
                
                        gp = 2;
                        count++;
                
                    }
    
                    else if(input[i][j] == 'D') {
                        
                        gp = 1;
                        count++;
                        
                    }
    
                    else {
                        
                        gp = 0;
                        count++;
                        
                    }
    
                    total_gp = total_gp + gp;
            
                    printf("%c\t", input[i][j]);
    
                }
    
                // Calculate all students gpa
                gpa = (total_gp / count);
    
                
                //=================================================//
                // Find max and min
    
                max = gpa;
                min = gpa;
    
                if (gpa > max) 
                {
                    max = gpa;
                    i++;
                }
    
                else if (gpa < min)
                {
                    min = gpa;
                    i++;
                }
    
                //==================================================//
    
                // Calculate total of gpa
                total_gpa = total_gpa + gpa;
    
                // Print all students gpa
                printf("%.1f", gpa);
    
                // Count subject
                countc++;
    
                // Calculate average gpa
                average_gpa = total_gpa / countc;
    
                printf("\n");
    
                // Reset count & total_gp to 0
                count = 0;
                total_gp = 0;            
            
            }
    
            printf("\nAverage GPA = %.0f", average_gpa);
            
            printf("\nHighest GPA = %.1f by Student No %d", max, i);
    
            printf("\nLowest GPA = %.1f by Student No %d", min, i);
    
            
    
            return 0;
    
        }
    Now please view image below...

    Looping problem...-untitled-jpg

    As you can see, I face a problem which is I can't determine the highest gpa and the lowest gpa of a students. It always just display the last students gpa. So how could I solve this problem? Very thanks you.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    On line 123 and 123 within your loop you set max and min = gpa. This gpa will be the second student on the second iteration of the loop. Try intializing min and max outside the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. looping problem?
    By nyekknyakk in forum C Programming
    Replies: 9
    Last Post: 08-20-2010, 11:02 AM
  2. looping problem
    By trixxma in forum C Programming
    Replies: 10
    Last Post: 05-20-2006, 10:17 PM
  3. looping problem
    By swagatob in forum C Programming
    Replies: 7
    Last Post: 10-12-2004, 11:50 PM
  4. Looping Problem
    By simly01 in forum Windows Programming
    Replies: 1
    Last Post: 06-28-2002, 01:05 AM
  5. Looping Problem
    By ccmac in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2002, 02:46 PM