Thread: does anyone know how to correct this problem

  1. #1
    bryankorb
    Guest

    does anyone know how to correct this problem

    I am trying to calculate the grades for the following students and I can't figure what I am doing wrong. The students grades will read from a disk but if the grade is over 100 or <0 it should ommit the grades from the class average. The program also list the students who have incorrect grades.


    // File: Pr_2.c
    // Date: 12/05/01
    // Purpose: Print the students first,last,and id number.
    //Read students three test scores from a file and compute the average.
    //The average will then be converted to a letter grade which will
    //Print out on the screen both numerical and charater grade.
    //Then compute the class average only for students who's average is less than or equal to 100
    //The program will also list the students as well as their grades who got over a 100

    #include <stdio.h>
    #include <string.h>
    int main ()
    {
    // declare the variables for program
    char firstname [20] = "";
    char lastname [20] = "";
    char fullname [40] = "";
    char grad(float);
    int test1,test2,test3,IDnum;
    int students; //file name
    int invalid;
    int k;
    int valid(int,int,int);
    float average;
    float averageclass,sum;

    FILE * datafile;

    datafile = fopen("D:\\students.txt","r");
    //Read from file
    if(datafile==NULL)
    {
    printf("could not open data file requested\n");
    return 0;
    }//end of if statement
    sum=0;
    fscanf(datafile,"%d",&students);
    for (k=1;k<=students;k++)
    {
    fscanf(datafile,"%s",firstname);
    fscanf(datafile,"%s",lastname);
    fscanf(datafile,"%s",&IDnum);
    fscanf(datafile,"%s",&test1);
    fscanf(datafile,"%s",&test2);
    fscanf(datafile,"%s",&test3);
    strcpy(fullname,lastname);
    strcat(fullname,", ");
    strcat(fullname, firstname);
    if (valid (test1,test2,test3))
    { //begins if statement
    average=(test1,test2,test3)/3.0; //calculates students average
    sum=sum+average;
    printf("%s\t%d",fullname,IDnum);
    printf("\t%d\t%d\t%d",test1,test2,test3);
    printf("\t%.2f\t%c\t\n",average,grad(average));
    } //end if
    else
    {
    invalid++;
    printf("%s\t%d",fullname,IDnum);
    printf("\tInvalid data\n\n");
    students=students-1;
    }
    }//end of loop
    fclose(datafile);
    averageclass=(sum/(students-invalid));
    printf("/n/nThe class average calculated is:");
    printf("%.2f\n\n",averageclass);

    } //end of main function
    int valid (int test1,int test2,int test3) //valid function called
    {
    int good;
    return((test1>=0)&&(test1<=100)&&(test2>=0)&&(test 2<=100)&&(test3>=0)&&(test3<=100));
    }//end of valid function
    char grad(float average) //grade function is called
    {
    char grade;
    //converts numeric grade to letter grade
    if (average<=60)grade='F';
    if (average>=60&&average<70)grade='D';
    if (average>=70&&average<80)grade='C';
    if (average>=80&&average<90)grade='B';
    if (average>=90&&average<100)grade='A';
    return (grade);
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    fscanf(datafile,"%s",&IDnum);
    fscanf(datafile,"%s",&test1);
    fscanf(datafile,"%s",&test2);
    fscanf(datafile,"%s",&test3);

    I'm reasonably sure that you keed to be using %d here
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Hmmm, this looks oddly familiar...

    Anyway look at what you have in your else statement to the end of your program:
    else
    {
    invalid++; here
    printf("%s\t%d",fullname,IDnum);
    printf("\tInvalid data\n\n");
    students=students-1; and here
    }
    }//end of loop

    fclose(datafile);
    averageclass=(sum/(students-invalid)); especially here
    printf("/n/nThe class average calculated is:");
    printf("%.2f\n\n",averageclass);

    } //end of main function
    You have a counter that increments everytime one of the test score is invalid. Print out some info then you decrement you student counter. Lets say you have 10 students in your file, so your counter, students = 10.

    Say 4 of these have invalid data. When you hit the first one (one w/invalid data that is) you enter the else, increment invalid to 1, then you decrement students to 9. Next invalid, invalid = 2, students = 8, and so forth and so on. Once you've processed all your data, invalid = 4 and students = 6.

    When you are calculating averageclass you have (sum/(students-invalid));. With students = 6 - invalid = 4, which will wind up dividing by 2, and not by 6, which is what you are probably looking for.
    DrakkenKorin

    Get off my Intarweb!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Josephus Problem
    By GCNDoug in forum C++ Programming
    Replies: 0
    Last Post: 05-12-2008, 07:47 AM
  2. How to correct the problem?
    By vsanandan in forum C Programming
    Replies: 2
    Last Post: 03-26-2008, 02:02 AM
  3. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  4. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 07:29 PM
  5. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM