# newbie question on converting numeric grades to letter grades

• 08-13-2002
jgonzales
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....

******************************
-----------------------
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)
{

}

{

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

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)
else if (grade[i] >= 80)
else if (grade[i] >= 70)
else if (grade[i] >= 60)
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");

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

}
• 08-13-2002
moi
when posting messages on this forum, enclose your code in [code] tags to make it easier to read.
• 08-13-2002
Prelude
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:

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
• 08-13-2002
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??

Jose

******************************
-----------------------
Jones 37813440
Brown 37813480
Smith 37813520
Alazar 37813560
Forster 37813600
Joplin 37813640
Grey 37813680
*******************************

press 'ENTER' key to continue...

below is code.....
__________________________________________________

/*
************************************************** ***********************
* *
* "Input/Output Program" *
* author: Jose Gonzales *
* key: #c06 *
* ---PROGRAM HISTORY--- *
* creation date: 08/12/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)
{

}

{

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

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)
{
printf ("inside \n");
}
else if (grade[i] >= 80)
else if (grade[i] >= 70)
else if (grade[i] >= 60)
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]);
fclose (myFile);
printf (" *******************************\n");

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

}

_________________________________________________
• 08-13-2002
Hammer
Quote:

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.
• 08-13-2002
Prelude
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. :p

-Prelude
• 08-13-2002
Troll_King
Use an & in the printf function when you print an integer or a floating point.

printf("%s %d", cArray[i], &iNum[i]);
• 08-14-2002
Prelude
>Use an & in the printf function when you print an integer or a floating point.
:confused:

-Prelude
• 08-14-2002
moi
Quote:

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.
• 08-14-2002
Troll_King
Quote:

Originally posted by jgonzales

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

void main (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.
• 08-14-2002
jgonzales
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 main (void)
{
}

{
//open text file, read and display grades
char ch;
FILE *myFile;
char student[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;
}

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]);
if (grade[i] >= 90 && grade[i] <=100)
else if (grade[i] >= 80)
else if (grade[i] >= 70)
else if (grade[i] >= 60)
else
//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");

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

_________________________________________________
• 08-14-2002
Hammer
For the third time:
void main(void)
(see previous posts)

And use code tags please!
• 08-15-2002
jgonzales
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);   }```
• 08-15-2002
Monster