Henry said:
I was doing this program for an exercise in a book. The point was to create
a program that would take a numerical grade from a user and convert it to a
letter grade (yeah really easy). I tried to incorporate the while loop to
keep the program running continuously so the user could keep entering grades
to get the letter number, but I had little success and just kept creating
infinite loops. My question is:
How could I modify my program to read the last letter of the input and add +
or - to the end of the letter grade..
1-3 = -
4-6 = <blank>
7-9 = +
Here is my program so far:
#include <stdio.h>
#include <string.h>
main()
{
int letter_grade;
int numeric_grade;
int grade[100];
(void)printf("Enter the Numeric grade: ");
(void)fgets(grade, sizeof(grade), stdin);
(void)sscanf(grade, "%d", &numeric_grade);
if (numeric_grade <= 60)
letter_grade = "F";
if (numeric_grade > 60 && numeric_grade <= 70)
letter_grade = "D";
if (numeric_grade > 70 && numeric_grade <= 80)
letter_grade = "C";
if (numeric_grade > 80 && numeric_grade <= 90)
letter_grade = "B";
if (numeric_grade > 90 && numeric_grade <= 100)
letter_grade = "A";
(void)printf("The Letter grade is: %s\n", letter_grade);
}
Thank you for your post. You've made an excellent try. Here
is a program that works, except when you enter non-numeric input.
Maybe someone else can fix that part.
--Steve
#include <stdio.h>
int main()
{
char *letter_grade; /* Use the asterisk when you want to hold */
/* more than one character (i.e., */
/* char *letter_grade; not */
/* char letter_grade; */
int numeric_grade;
int grade[100];
printf("Enter the numeric grade (or negative number to quit): ");
scanf("%d", &numeric_grade);
if (numeric_grade > 100) {
printf("Grade entered must be less than 101. Try again: ");
scanf("%d", &numeric_grade);
}
while (numeric_grade >= 0) {
if (numeric_grade > 100) {
printf("Grade entered must be less than 101. Try again: ");
scanf("%d", &numeric_grade);
continue;
}
if (numeric_grade <= 60)
letter_grade = "F";
if (numeric_grade > 60 && numeric_grade < 64)
letter_grade = "D-";
if (numeric_grade > 63 && numeric_grade < 67)
letter_grade = "D";
if (numeric_grade > 66 && numeric_grade <= 70)
letter_grade = "D+";
if (numeric_grade > 70 && numeric_grade < 74)
letter_grade = "C-";
if (numeric_grade > 73 && numeric_grade < 77)
letter_grade = "C";
if (numeric_grade > 76 && numeric_grade <= 80)
letter_grade = "C+";
if (numeric_grade > 80 && numeric_grade < 84)
letter_grade = "B-";
if (numeric_grade > 83 && numeric_grade < 87)
letter_grade = "B";
if (numeric_grade > 86 && numeric_grade <= 90)
letter_grade = "B+";
if (numeric_grade > 90 && numeric_grade < 94)
letter_grade = "A-";
if (numeric_grade > 93 && numeric_grade < 97)
letter_grade = "A";
if (numeric_grade > 96 && numeric_grade <= 100)
letter_grade = "A+";
printf("The letter grade is: %s\n", letter_grade);
printf("Enter the numeric grade, (or negative number to quit): ");
scanf("%d", &numeric_grade);
}
return 0;
}