need help

This is a discussion on need help within the C Programming forums, part of the General Programming Boards category; I am trying to calculate the grades for the following students and I can't figure what I am doing wrong. ...

  1. #1
    bryankorb
    Guest

    need help

    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. It will now list wrong grades but calculates the grades and class average wrong


    // 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,"%d",&IDnum);
    fscanf(datafile,"%d",&test1);
    fscanf(datafile,"%d",&test2);
    fscanf(datafile,"%d",&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
    average=(test1,test2,test3)/3.0; //calculates students average

    The comma operator is a rather stange operator, and not the one to use here. You need to add the test scores, so use +

    Please don't post a new topic in cases like this, Just reply to your old topic when you have new problems with the same code.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

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