an elusive function

This is a discussion on an elusive function within the C Programming forums, part of the General Programming Boards category; I have a project that needs to be modified to omit invalid data from a function that averages grades. It ...

  1. #1
    Unregistered
    Guest

    an elusive function

    I have a project that needs to be modified to omit invalid data from a function that averages grades. It also needs to print "Invalid data" in place of the data. I am burnt out from finals and I can't think straight. Any help would be greatly appreciated




    //Purpose:
    //1 to read information from a file (name, IDnumber, test scores,1,2,3)
    //2 determine if the test scores are valid (score>=0 && <=100)
    //3 if invalid, display"invalid data" in place of data
    //4 omit invalid data from all calculations
    //5 calculate averages of scores
    //6 assign a letter grade value to score
    //7calculate class average

    #include <stdio.h>
    #include <string.h> // a string will be used for name input
    int main () {

    char firstname [40];
    char lastname [40];
    char fullname [80];
    int score1, score2, score3,idnum;
    float averageclass, sum;
    int c;
    int students; // name of file
    int valid (int,int,int);
    float average;
    char gradef (float);

    FILE * datafile;
    datafile = fopen("D://students.txt","r");
    if (datafile == NULL)
    {
    printf("Could not open data file\n");
    return 0;
    }//end if

    sum = 0;

    fscanf(datafile, "%d", &students);

    printf("\n %d Students will be read from file\n\n",students);
    for(c = 1 ;c <= students; c++)
    {

    fscanf(datafile,"%s", firstname);

    fscanf(datafile,"%s", lastname);

    fscanf(datafile,"%d", &idnum);

    fscanf(datafile,"%d",&score1);

    fscanf(datafile,"%d", &score2);

    fscanf(datafile,"%d", &score3);


    if (valid(score1, score2, score3))
    {// begin if


    average = (score1 + score2 + score3) / 3.0; //calculates average of scores

    sum = sum + average;

    strcpy(fullname, lastname);
    strcat(fullname,", ");
    strcat(fullname, firstname); // string to store name first-last and display last name first

    printf(" %s \t %d ", fullname, idnum);
    }// end if


    else
    printf(" Invalid data");// this needs to be printed instead of (test scores1,2,3 and average)
    // also, I must exclude this invalid information from the class average



    printf("\t %d \t %d \t%d", score1, score2, score3);
    printf("\t%.2f \t %c \t\n\n", average, gradef(average));


    } //end of loop

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


    }//end of main

    int valid (int score1, int score2, int score3) //valid function

    {// start of valid
    int good;
    return( (score1 >= 0) && (score1 <= 100) && (score2 >= 0) && (score2 <= 100) && (score3 >= 0) && (score3 <= 100));
    }// end of valid

    char gradef (float average)//grade function
    {
    char grade;
    if (average <= 60) grade ='F';
    if (average >= 60 && average < 70) grade = 'D'; // assigns letter grade to numerical value if (average >= 70 && average < 80) grade = 'C';
    if (average >= 70 && average < 80) grade = 'C';
    if (average >= 80 && average < 90) grade = 'B';
    if (average >= 90 && average < 100) grade ='A';

    return grade;

    }



    Current display



    8 Students will be read from file

    Clinton, Bill 1111 92 64 91 82.33 B

    Bush, George 2222 66 42 75 61.00 D

    Reagan, Ronald 3333 50 50 56 52.00 F

    Invalid data 82 -5 91 52.00 F

    Invalid data 118 64 88 52.00 F

    Nixon, Richard 6666 90 90 93 91.00 A

    Johnson, Lyndon 7777 78 90 68 78.67 C

    Kennedy, John 8888 52 96 75 74.33 C



    The class average is: 54.92

    Press any key to continue

  2. #2
    Nosepicker DrakkenKorin's Avatar
    Join Date
    Nov 2001
    Posts
    407
    Now if I'm understanding you correctly, if the record you read in has invalid test grades, all you want to print out is:
    Code:
    Invalid Data
    If this is it, then its an easy fix, notice you have the majority of the print functions outside the test for valid data, just move the printf's (where you print the test grades and average) inside the test for valid data, and that should fix it:

    Code:
    if (valid(score1, score2, score3)) 
       {// begin if 
    
    
          average = (score1 + score2 + score3) / 3.0; 
               //calculatesaverage of scores 
    
          sum = sum + average; 
    
          strcpy(fullname, lastname); 
          strcat(fullname,", "); 
          strcat(fullname, firstname); 
               // string to store name first-last and display last name first 
    
          printf(" %s \t %d ", fullname, idnum);
          printf("\t %d \t %d \t%d", score1, score2, score3); 
          printf("\t%.2f \t %c \t\n\n", average, gradef(average)); 
          
    }// end if
    Worth a shot...
    Last edited by DrakkenKorin; 12-04-2001 at 11:15 AM.
    DrakkenKorin

    Get off my Intarweb!!!!

  3. #3
    Unregistered
    Guest
    Unfortunately, It is more complicated than that. I need to display "Invalid data" in place of the data itself. I also must omit the invalid data from the calculation so the class average is not affected by the invalid data.

    The output should look like this:
    8 Students will be read from file

    Clinton, Bill 1111 92 64 91 82.33 B

    Bush, George 2222 66 42 75 61.00 D

    Reagan, Ronald 3333 50 50 56 52.00 F

    Jimmy Carter 4444 Invalid data

    Gerald Ford 5555 Invalid data

    Nixon, Richard 6666 90 90 93 91.00 A

    Johnson, Lyndon 7777 78 90 68 78.67 C

    Kennedy, John 8888 52 96 75 74.33 C


    The class average is 73.22

    Press any key to continue
    =============================================

    The current output is :


    8 Students will be read from file

    Clinton, Bill 1111 92 64 91 82.33 B

    Bush, George 2222 66 42 75 61.00 D

    Reagan, Ronald 3333 50 50 56 52.00 F

    Invalid data 82 -5 91 52.00 F

    Invalid data 118 64 88 52.00 F

    Nixon, Richard 6666 90 90 93 91.00 A

    Johnson, Lyndon 7777 78 90 68 78.67 C

    Kennedy, John 8888 52 96 75 74.33 C



    The class average is: 54.92

    Press any key to continue

  4. #4
    Nosepicker DrakkenKorin's Avatar
    Join Date
    Nov 2001
    Posts
    407
    2 ways to do this:
    1. Move your string manipulations outside the test for valid
    grades.
    -OR-
    2. Manipulate string inside the body of the else.

    for example:

    Code:
    strcpy(fullname, lastname); 
    strcat(fullname,", "); 
    strcat(fullname, firstname);
        // string to store name first-last and display last name first
    
    /*NOTICE THAT fullname WAS MANIPULATED BEFORE TESTING
       FOR VALID GRADES, THAT WAY, YOU WON'T HAVE TO DO IT
       ALL OVER AGAIN INDSIDE THE ELSE (invalid grades)*/
     
    if (valid(score1, score2, score3)) 
    {// begin if 
       average = (score1 + score2 + score3) / 3.0;
             //calculates average of scores 
    
       sum = sum + average; 
    
       printf(" %s \t %d ", fullname, idnum);
       printf("\t %d \t %d \t%d", score1, score2, score3); 
       printf("\t%.2f \t %c \t\n\n", average, gradef(average)); 
    }// end if
    else
    {
       printf("%s\t%d", fullname, idnum);
       printf("\tInvalid Data\n\n");
    }
    If one of the grades is invalid, it will hit the else, which will only print out the persons fullname, id, and "Invalid Data". No other variables are manipulated, so it shouldn't affect the class average.

    That SHOULD (no promises ) fix it.
    Last edited by DrakkenKorin; 12-04-2001 at 12:53 PM.
    DrakkenKorin

    Get off my Intarweb!!!!

  5. #5
    Unregistered
    Guest
    I tried the changes you reccommended. Now the "Invalid data" is printing in front of all the names (except the first) I would give you a copy of the output but I am working at home now and this stupid computer will not let me print screen to notepad ( I guess its one of the differences between ME and XP)

    Any other Ideas?








    #include <stdio.h>
    #include <string.h> // a string will be used for name input
    int main () {

    char firstname [40];
    char lastname [40];
    char fullname [80];
    int score1, score2, score3,idnum;
    float averageclass, sum;
    int c;
    int students; // name of file
    int valid (int,int,int);
    float average;
    char gradef (float);

    FILE * datafile;
    datafile = fopen("C:My Documents//students.txt","r");
    if (datafile == NULL)
    {
    printf("Could not open data file\n");
    return 0;
    }//end if

    sum = 0;

    fscanf(datafile, "%d", &students);

    printf("\n %d Students will be read from file\n\n",students);
    for(c = 1 ;c <= students; c++)
    {

    fscanf(datafile,"%s", firstname);

    fscanf(datafile,"%s", lastname);

    fscanf(datafile,"%d", &idnum);

    fscanf(datafile,"%d",&score1);

    fscanf(datafile,"%d", &score2);

    fscanf(datafile,"%d", &score3);

    strcpy(fullname, lastname);
    strcat(fullname,", ");
    strcat(fullname, firstname); // string to store name first-last and display last name first

    if (valid(score1, score2, score3))
    {// begin if


    average = (score1 + score2 + score3) / 3.0; //calculates average of scores

    sum = sum + average;


    printf(" %s \t %d ", fullname, idnum);
    printf("\t %d \t %d \t%d", score1, score2, score3);
    printf("\t%.2f \t %c \t\n\n", average, gradef(average));


    }// end if
    else
    printf(" %s \t %d ", fullname, idnum);
    printf("Invalid data");


    } //end of loop

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


    }//end of main

    int valid (int score1, int score2, int score3) //valid function

    {// start of valid
    int good;
    return( (score1 >= 0) && (score1 <= 100) && (score2 >= 0) && (score2 <= 100) && (score3 >= 0) && (score3 <= 100));
    }// end of valid

    char gradef (float average)//grade function
    {
    char grade;
    if (average <= 60) grade ='F';
    if (average >= 60 && average < 70) grade = 'D'; // assigns letter grade to numerical value if (average >= 70 && average < 80) grade = 'C';
    if (average >= 70 && average < 80) grade = 'C';
    if (average >= 80 && average < 90) grade = 'B';
    if (average >= 90 && average < 100) grade ='A';

    return grade;

    }

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Add some brackets.
    Code:
    else
    { 
       printf(" %s \t %d ", fullname, idnum); 
       printf("Invalid data"); 
    }

  7. #7
    Nosepicker DrakkenKorin's Avatar
    Join Date
    Nov 2001
    Posts
    407
    Loot at your if-else block. Notice anything wrong?

    if (valid(score1, score2, score3))
    {// begin if
    average = (score1 + score2 + score3) / 3.0;
    //calculates average of scores

    sum = sum + average;

    printf(" %s \t %d ", fullname, idnum);
    printf("\t %d \t %d \t%d", score1, score2, score3);
    printf("\t%.2f \t %c \t\n\n", average, gradef(average));
    }// end if
    else
    printf(" %s \t %d ", fullname, idnum);
    printf("Invalid data");
    You forgot to enclose your else block in { }. It should be:

    Code:
    if (valid(score1,score2,score3))
    {
       average = (score1 + score2 + score3)/3.0;
    
       sum += average;
    
       printf("%s \t %d", fullname, idnum);
       printf("\t %d \t %d \t %d", score1, score2, score3);
       printf("\t%.2f \t %c \t\n\n", average, gradef(average));
    } /*end if*/
    else
    {   printf(%s \t %d", fullname, idnum);
        printf("Invalid data");
    }



    ::Swoopy beat me to it ::
    DrakkenKorin

    Get off my Intarweb!!!!

  8. #8
    Unregistered
    Guest
    Thanks for the heads up, that took care of the format problem, and excluded the invlaid scores. Now I neeed to subtract those invalid students from the count that divides the scores to get class average.
    The sum of the scores is being divided by 8 instead of 6

    8 Students will be read from file

    Clinton, Bill 1111 92 64 91 82.33 B

    Bush, George 2222 66 42 75 61.00 D

    Reagan, Ronald 3333 50 50 56 52.00 F

    Carter, Jimmy 4444 Invalid data

    Ford, Gerald 5555 Invalid data

    Nixon, Richard 6666 90 90 93 91.00 A

    Johnson, Lyndon 7777 78 90 68 78.67 C

    Kennedy, John 8888 52 96 75 74.33 C



    The class average is: 54.92

    Press any key to continue
    ==============================================
    I tried (students = students -1) in the else statement but it subtracts the last two lines completely
    the output looks like:


    8 Students will be read from file

    Clinton, Bill 1111 92 64 91 82.33 B

    Bush, George 2222 66 42 75 61.00 D

    Reagan, Ronald 3333 50 50 56 52.00 F

    Carter, Jimmy 4444 Invalid data

    Ford, Gerald 5555 Invalid data

    Nixon, Richard 6666 90 90 93 91.00 A



    The class average is: 47.72

    Press any key to continue


    ==============================================



    #include <stdio.h>
    #include <string.h> // a string will be used for name input
    int main () {

    char firstname [40];
    char lastname [40];
    char fullname [80];
    int score1, score2, score3,idnum;
    float averageclass, sum;
    int c;
    int students; // name of file
    int valid (int,int,int);
    float average;
    char gradef (float);

    FILE * datafile;
    datafile = fopen("D://students.txt","r");
    if (datafile == NULL)
    {
    printf("Could not open data file\n");
    return 0;
    }//end if

    sum = 0;

    fscanf(datafile, "%d", &students);

    printf("\n %d Students will be read from file\n\n",students);
    for(c = 1 ;c <= students; c++)
    {

    fscanf(datafile,"%s", firstname);

    fscanf(datafile,"%s", lastname);

    fscanf(datafile,"%d", &idnum);

    fscanf(datafile,"%d", &score1);

    fscanf(datafile,"%d", &score2);

    fscanf(datafile,"%d", &score3);

    strcpy(fullname, lastname);
    strcat(fullname,", ");
    strcat(fullname, firstname); // string to store name first-last and display last name first

    if (valid(score1, score2, score3))
    {// begin if


    average = (score1 + score2 + score3) / 3.0; //calculates average of scores

    sum = sum + average;


    printf("%s \t %d", fullname, idnum);
    printf("\t %d \t %d \t %d", score1, score2, score3);
    printf("\t %.2f \t %c \t\n\n", average, gradef(average));


    }// end if
    else
    {
    printf("%s \t %d", fullname, idnum);printf("\t\t\tInvalid data\n\n");
    students = students -1;

    }
    } //end of loop

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


    }//end of main

    int valid (int score1, int score2, int score3) //valid function

    {// start of valid
    int good;
    return( (score1 >= 0) && (score1 <= 100) && (score2 >= 0) && (score2 <= 100) && (score3 >= 0) && (score3 <= 100));
    }// end of valid

    char gradef (float average)//grade function
    {
    char grade;
    if (average <= 60) grade ='F';
    if (average >= 60 && average < 70) grade = 'D'; // assigns letter grade to numerical value if (average >= 70 && average < 80) grade = 'C';
    if (average >= 70 && average < 80) grade = 'C';
    if (average >= 80 && average < 90) grade = 'B';
    if (average >= 90 && average < 100) grade ='A';

    return grade;

    }

  9. #9
    Nosepicker DrakkenKorin's Avatar
    Join Date
    Nov 2001
    Posts
    407
    Best way (and probably the easiest) would be to declare a new counter, say InvalidCtr, and increment it everytime you're inside the else (so it'll count up how many records had invalid data). Then, when you calculate the class average, subtract invalidCtr from the total number of students:

    Code:
    else
    {
       invalidCtr++;
       printf("%s \t %d", fullname, idnum)
       printf("\t\t\tInvalid Data\n\n");
    }
    
    /*REST OF YOUR CODE*/
    
    averageclass = sum/(students - invalidCtr);
    
    */REMAINING CODE*/
    DrakkenKorin

    Get off my Intarweb!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

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