Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Phil Lane
- 10/10/17
- Program Assignment 11
- This program is entirely my own work.
- */
- //Header files
- #include <stdio.h>
- #include <stdlib.h>
- //Function prototype to get grade
- void getGrade(int gradeNum, char** gradeStr);
- //Main function
- int main()
- {
- char response;
- do
- {
- //Grade numeric value
- int gradeNum = 0;
- //Grade letter value
- char* gradeStr = (char*)calloc(3, sizeof(char)); //allocate three bytes
- //Repeat the scanf call until it's a valid input (for error checking)
- do
- {
- printf("Input a grade from 0 to 100:\n");
- scanf("%d", &gradeNum);
- } while (gradeNum < 0 || gradeNum > 100);
- //Call the getGrade function to assign value to gradeStr
- getGrade(gradeNum, &gradeStr);
- //Print the letter grade
- printf("You got a %s!\n", gradeStr);
- do
- {
- printf("Want to go again? (y/n):\n"); //query for y/n
- scanf(" %c", &response); //get response
- if (response > 'a') //if letter is lowercase
- response -= 32; //make it uppercase
- } while (response != 'Y' && response != 'N'); //for error checking, repeat until we get a valid response
- } while (response == 'Y'); //if yes, repeat the program
- }
- //takes the grade number and a pointer to a string that represents the letter grade
- void getGrade(int gradeNum, char** gradeStr)
- {
- //a 100 messes up the following switch statements so we manually handle this
- if (gradeNum == 100)
- {
- *gradeStr = "A+";
- return;
- }
- //First we check the grade. Divide by 10 and cast to int... that way we compare the first digit
- switch ((int)(gradeNum / 10))
- {
- case 9: //if grade is in the 90s
- (*gradeStr)[0] = 'A';
- break;
- case 8: //if grade is in the 80s
- (*gradeStr)[0] = 'B';
- break;
- case 7: //if grade is in the 70s
- (*gradeStr)[0] = 'C';
- break;
- case 6: //if grade is in the 60s
- (*gradeStr)[0] = 'D';
- break;
- default: //otherwise
- (*gradeStr)[0] = 'F';
- return; //return instead of break because there are no F+'s or F-'s
- }
- //now we handle the + or -. Mod 10 to get just the last digit
- switch (gradeNum % 10)
- {
- case 7: case 8: case 9: //if it's a X7, X8, or X9 then it's a +
- (*gradeStr)[1] = '+';
- break;
- case 0: case 1: case 2: case 3: //if it's a X0, X1, X2, or X3, then it's a -
- (*gradeStr)[1] = '-';
- break;
- //no default because we want it to be 0 to terminate the string
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement