Thread: newbie question on converting numeric grades to letter grades

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    21

    newbie question on converting numeric grades to letter grades

    Can anyone help me. I am working on a program that reads from a text file, then displays the names and grades on the screen. This is working but I need to convert the number grades to letter grades...This is where I need help. '

    Does anyone know how to do this???


    Jose

    my input text file named:grades.txt is below.........

    ........................
    Jones
    55
    Brown
    81
    Smith
    89
    Alazar
    93
    Forster
    63
    Joplin
    67
    Grey
    33
    @

    ...................

    screen output below....




    ******************************
    NAME GRADE
    -----------------------
    Jones 55
    Brown 81
    Smith 89
    Alazar 93
    Forster 63
    Joplin 67
    Grey 33
    *******************************



    ...........................

    press 'ENTER' key to continue...


    ............................

    and my program code is below.....

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 10
    #define MAXN 10

    void main (void)
    {

    display_grades();
    }


    void display_grades()
    {

    char ch;
    char lettergrade[MAXN];
    FILE *myFile;
    //char student[MAXN][MAX];
    //int grade[MAXN][MAX];
    char student[MAXN][MAX], grade[MAXN][MAX];
    int i;
    int j;
    //set file path
    myFile = fopen ("c:\\temp\\grades.txt","r");


    //display grades
    printf ("\n");
    printf ("\n");
    printf ("\n");
    printf (" ******************************\n");
    printf (" NAME GRADE \n");
    printf (" ----------------------- \n");
    i=0;

    fscanf (myFile, "%s", student[0]);
    //...until last ending character in file
    while (student[i][0] != '@' && i < MAXN -1)
    {
    fscanf (myFile, "%s", grade[i]);

    //convert to letter grade***Not working
    if (grade[i] >= 90)
    lettergrade[i] = 'A';
    else if (grade[i] >= 80)
    lettergrade[i] = 'B';
    else if (grade[i] >= 70)
    lettergrade[i] = 'C';
    else if (grade[i] >= 60)
    lettergrade[i] = 'D';
    else lettergrade[i] = 'F';
    //end of conversion
    i++;

    fscanf (myFile, "%s", student[i]);
    }
    for (j=0; j<i; j++)

    printf (" %s %s \n", student[j], grade[j]);
    fclose (myFile);
    printf (" *******************************\n");

    //add extra lines
    printf ("\n");
    printf ("\n");
    printf ("\n");
    printf ("\n");
    printf ("\n");
    printf ("press 'ENTER' key to continue...\n");
    scanf ("%c",&ch);

    }

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    when posting messages on this forum, enclose your code in [code] tags to make it easier to read.
    hello, internet!

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    //convert to letter grade***Not working
    if (grade[i] >= 90)
    lettergrade[i] = 'A';
    else if (grade[i] >= 80)
    It doesn't work because grade is a two dimensional array, you are treating it as a one dimensional array. grade[i] contains MAX grades, not just one. Try looping through all of the grades in grade[i] and getting the average, then return the letter grade of the average.

    >void main (void)
    main returns an int, nothing else. Be sure to place return 0; at the end of your main function as well.

    display_grades needs to be prototyped:
    Code:
    void display_grades();
    
    void main (void)
    {
      display_grades();
    }
    
    void display_grades()
    {
    In C an empty argument list does not mean the function takes no arguments, if that is what you want then specify it with void:

    void display_grades(void)
    Code:
    myFile = fopen ("c:\\temp\\grades.txt","r");
    Check myFile to see if fopen succeeded. If not then myFile will be NULL.

    I would modify the code to show you exactly what I mean, but I'm in a big hurry. Sorry.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    21
    I tried to modify my code but I still am not having any success, now it is printing this as the screen output....

    very frustrated because I know I am a newbie and this will end up being something easy, anyone know what I am still doing wrong??


    Jose

    ******************************
    NAME GRADE
    -----------------------
    Jones 37813440
    Brown 37813480
    Smith 37813520
    Alazar 37813560
    Forster 37813600
    Joplin 37813640
    Grey 37813680
    Grade: ▬
    *******************************


    press 'ENTER' key to continue...

    below is code.....
    __________________________________________________

    /*
    ************************************************** ***********************
    * *
    * "Input/Output Program" *
    * author: Jose Gonzales *
    * key: #c06 *
    * ---PROGRAM HISTORY--- *
    * creation date: 08/12/02 *
    * last modified: 08/13/02 *
    * lines: 72 *
    * description: The purpose of this program is to read *
    * from a text file then convert the *
    * numeric grades to letter grades and *
    * sort them in alphabetically order. *
    * *
    ************************************************** ***********************
    */


    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 10
    #define MAXN 10

    void main (void)
    {

    display_grades();
    }


    void display_grades(void)
    {

    char ch;
    char lettergrade[MAXN][MAX];
    FILE *myFile;
    //char student[MAXN][MAX];
    //int grade[MAXN][MAX];
    char student[MAXN][MAX];
    int grade[MAXN][MAX];
    int i;
    int j;
    //set file path
    myFile = fopen ("c:\\temp\\grades.txt","r");


    //display grades
    printf ("\n");
    printf ("\n");
    printf ("\n");
    printf (" ******************************\n");
    printf (" NAME GRADE \n");
    printf (" ----------------------- \n");
    i=0;

    fscanf (myFile, "%s", student[0]);
    //...until last ending character in file
    while (student[i][0] != '@' && i < MAXN -1)
    {
    fscanf (myFile, "%i", grade[i]);

    //convert to letter grade***Not working
    if (grade[i] >= 90 && grade[i] <= 100)
    {
    lettergrade[i][0] = 'A';
    printf ("inside \n");
    }
    else if (grade[i] >= 80)
    lettergrade[i][0] = 'B';
    else if (grade[i] >= 70)
    lettergrade[i][0] = 'C';
    else if (grade[i] >= 60)
    lettergrade[i][0] = 'D';
    else lettergrade[i][0] = 'F';
    //end of conversion
    i++;

    fscanf (myFile, "%s", student[i]);
    }
    for (j=0; j<i; j++)

    printf (" %s %i \n", student[j], grade[j]);
    printf ("Grade: %c \n", lettergrade[j]);
    fclose (myFile);
    printf (" *******************************\n");

    //add extra lines
    printf ("\n");
    printf ("\n");
    printf ("\n");
    printf ("\n");
    printf ("\n");
    printf ("press 'ENTER' key to continue...\n");
    scanf ("%c",&ch);

    }

    _________________________________________________

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by jgonzales
    I tried to modify my code but I still am not having any success, now it is printing this as the screen output....

    very frustrated because I know I am a newbie and this will end up being something easy, anyone know what I am still doing wrong??
    <snip>
    Ok, lesson number 1. Try to listen to all of what people tell you.
    - Code tags are needed when you post code (see my sig for an example). (As stated by moi).
    - Don't use void main. (As stated by Prelude).
    - grade[i] : (As stated by Prelude) it doesn't work because grade is a two dimensional array, you are treating it as a one dimensional array.
    - myFile = fopen("c:\\temp\\grades.txt", "r");
    Check this worked. (As stated by Prelude).

    I'm not going to continue listing the things you have changed. Try again, and if you have problem with specific areas, ask a more specific question.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Okay, I'm back. Here is what I meant with that incoherent rant earlier, don't turn in my code if this is homework. Teachers can tell if you used someone else's source.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define NUM_OF_STUDENTS 3
    #define NUM_OF_GRADES   6
    
    static void display_grades ( void );
    static char get_letter ( int grade );
    
    int main ( void )
    {
      display_grades();
      return 0;
    }
    
    static void display_grades ( void )
    {
      FILE *input;
      char student_name[BUFSIZ];
      int  student,
           grade,
           average;
      /*
      ** Open the file and check that it was successful.
      */
      if ( ( input = fopen ( "input.txt", "r" ) ) == NULL ) {
        perror ( "File open failure" );
        return;
      }
      /*
      ** Process one student for each iteration.
      */
      for ( student = average = 0; student < NUM_OF_STUDENTS; student++ ) {
        /*
        ** Read the student's name. If fscanf fails then the
        ** file is probably not formatted correctly. Report
        ** the error and terminate processing.
        */
        if ( fscanf ( input, "%s", student_name ) != 1 ) {
          fprintf ( stderr, "Invalid file format\n" );
          return;
        }
        /*
        ** Check for the end of file so we don't accidentally
        ** process another line.
        */
        if ( strcmp ( student_name, "@" ) == 0 )
          return;
        /*
        ** Find the average of all grades for a student.
        */
        while ( fscanf ( input, "%d", &grade ) == 1 )
          average += grade;
        average /= NUM_OF_GRADES;
    
        /*
        ** Calculate the letter grade and print the record.
        */
        printf ( "%s: ", student_name );
        printf ( "%c\n", get_letter ( average ) );
      }
    }
    
    static char get_letter ( int grade )
    {
      if ( grade >= 90 )      return 'A';
      else if ( grade >= 80 ) return 'B';
      else if ( grade >= 70 ) return 'C';
      else if ( grade >= 60 ) return 'D';
      else                    return 'F';
    }
    A note to the nitpickers: I normally wouldn't use fscanf in this situation, so shush.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Use an & in the printf function when you print an integer or a floating point.

    printf("%s %d", cArray[i], &iNum[i]);

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Use an & in the printf function when you print an integer or a floating point.


    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Troll_King
    Use an & in the printf function when you print an integer or a floating point.

    printf("%s %d", cArray[i], &iNum[i]);
    printf expects values not addresses.
    hello, internet!

  10. #10
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Originally posted by jgonzales


    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 10
    #define MAXN 10

    void main (void)
    {

    display_grades();
    }


    void display_grades(void)
    {
    ...
    //okay
    fscanf (myFile, "%s", student[0]);
    //error
    fscanf (myFile, "%i", grade[i]);
    ...
    Okay use the '&' before grade[i].

    fscanf (myFile, "%i", &grade[i]);

    And it will work if memory serves.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    21
    I did it, praise Jesus!

    thanks for everyone that helped. here is my finished working code..

    _________________________________________________


    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 10
    #define MAXN 10

    void display_grades(void);

    void main (void)
    {
    display_grades();
    }


    void display_grades(void)
    {
    //open text file, read and display grades
    char ch;
    char lettergrade[MAXN];
    FILE *myFile;
    char student[MAXN][MAX];
    int grade[MAXN];
    //char student[MAXN][MAX], grade[MAXN][MAX];
    int i;
    int j;

    //open file and check for file error
    if (( myFile = fopen("c:\\temp\\grades.txt","r")) == NULL)
    {
    printf ("Error Opening with File.");
    scanf ("%c",&ch);
    return;
    }

    //display grades
    printf ("\n");
    printf ("\n");
    printf ("\n");
    printf (" ******************************\n");
    printf (" NAME GRADE \n");
    printf (" ----------------------- \n");
    i=0;

    fscanf (myFile, "%s", student[0]);
    //...until last ending character in file
    while (student[i][0] != '@' && i < MAXN -1)
    {
    fscanf (myFile, "%i", &grade[i]);
    //convert number grades to letter grades
    if (grade[i] >= 90 && grade[i] <=100)
    lettergrade[i] = 'A';
    else if (grade[i] >= 80)
    lettergrade[i] = 'B';
    else if (grade[i] >= 70)
    lettergrade[i] = 'C';
    else if (grade[i] >= 60)
    lettergrade[i] = 'D';
    else
    lettergrade[i] = 'F';
    //end conversion
    i++;
    fscanf (myFile, "%s", student[i]);
    }


    for (j=0; j<i; j++)
    {
    printf (" %s %c \n", student[j], lettergrade[j]);
    }
    fclose (myFile);
    printf ("\n");
    printf (" *******************************\n");

    //add extra lines
    printf ("\n");
    printf ("\n");
    printf ("\n");
    printf ("\n");
    printf ("\n");
    printf ("press 'ENTER' key to continue...\n");
    scanf ("%c",&ch);
    }



    _________________________________________________

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    For the third time:
    void main(void)
    (see previous posts)

    And use code tags please!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    21
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 10
    #define MAXN 10
    
    
    void display_grades(void);
    
    int main (void)
      {
         display_grades();
      }
    
    
      void display_grades(void)
      {
         //open text file, read and display grades
         char ch;
         char lettergrade[MAXN];
         FILE *myFile;
         char student[MAXN][MAX];
         int grade[MAXN];
         //char student[MAXN][MAX], grade[MAXN][MAX];
         int i;
         int j;
    
         //open file and check for file error
         if (( myFile = fopen("c:\\temp\\grades.txt","r")) == NULL)
            {
             printf ("Error Opening with File.");
             scanf ("%c",&ch);
             return;
             }
    
         //display grades
        printf ("\n");
        printf ("\n");
        printf ("\n");
        printf (" ******************************\n");
        printf ("     NAME     GRADE                  \n");
        printf ("  -----------------------  \n");
        i=0;
    
         fscanf (myFile, "%s", student[0]);
               //...until last ending character in file
               while (student[i][0] != '@' && i < MAXN -1)
                     {
                     fscanf (myFile, "%i", &grade[i]);
                         //convert number grades to letter grades
                         if (grade[i] >= 90 && grade[i] <=100)
                             lettergrade[i] = 'A';
                         else if (grade[i] >= 80)
                             lettergrade[i] = 'B';
                         else if (grade[i] >= 70)
                            lettergrade[i] = 'C';
                         else if (grade[i] >= 60)
                            lettergrade[i] = 'D';
                         else
                            lettergrade[i] = 'F';
                         //end conversion
                     i++;
                            fscanf (myFile, "%s", student[i]);
                     }
    
    
                     for (j=0; j<i; j++)
                         {
                         printf ("     %s      %c \n", student[j], lettergrade[j]);
                         }
                         fclose (myFile);
                         printf ("\n");
                         printf (" *******************************\n");
    
      //add extra lines
      printf ("\n");
      printf ("\n");
      printf ("\n");
      printf ("\n");
      printf ("\n");
      printf ("press 'ENTER' key to continue...\n");
      scanf ("%c",&ch);
      }

  14. #14
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Read Preludes advice again:

    main returns an int, nothing else. Be sure to place return 0; at the end of your main function as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. Total Newbie Question
    By Kid A in forum C++ Programming
    Replies: 4
    Last Post: 06-22-2006, 05:36 AM
  3. newbie question regarding user input
    By cantore in forum C Programming
    Replies: 4
    Last Post: 03-05-2006, 08:57 PM
  4. Newbie question
    By haus in forum C Programming
    Replies: 7
    Last Post: 02-03-2002, 11:58 AM
  5. Episode II Return Of the newbie : Website IO Question
    By MagiZedd in forum Windows Programming
    Replies: 1
    Last Post: 10-18-2001, 08:58 PM