Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. /*
  2. Phil Lane
  3. 10/10/17
  4. Program Assignment 11
  5. This program is entirely my own work.
  6. */
  7.  
  8. //Header files
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. //Function prototype to get grade
  13. void getGrade(int gradeNum, char** gradeStr);
  14.  
  15. //Main function
  16. int main()
  17. {
  18.     char response;
  19.  
  20.     do
  21.     {
  22.         //Grade numeric value
  23.         int gradeNum = 0;
  24.         //Grade letter value
  25.         char* gradeStr = (char*)calloc(3, sizeof(char)); //allocate three bytes
  26.  
  27.         //Repeat the scanf call until it's a valid input (for error checking)
  28.         do
  29.         {
  30.             printf("Input a grade from 0 to 100:\n");
  31.             scanf("%d", &gradeNum);
  32.         } while (gradeNum < 0 || gradeNum > 100);
  33.  
  34.         //Call the getGrade function to assign value to gradeStr
  35.         getGrade(gradeNum, &gradeStr);
  36.  
  37.         //Print the letter grade
  38.         printf("You got a %s!\n", gradeStr);
  39.  
  40.         do
  41.         {
  42.             printf("Want to go again? (y/n):\n"); //query for y/n
  43.             scanf(" %c", &response); //get response
  44.             if (response > 'a') //if letter is lowercase
  45.                 response -= 32; //make it uppercase
  46.         } while (response != 'Y' && response != 'N'); //for error checking, repeat until we get a valid response
  47.  
  48.     } while (response == 'Y'); //if yes, repeat the program
  49. }
  50.  
  51. //takes the grade number and a pointer to a string that represents the letter grade
  52. void getGrade(int gradeNum, char** gradeStr)
  53. {
  54.     //a 100 messes up the following switch statements so we manually handle this
  55.     if (gradeNum == 100)
  56.     {
  57.         *gradeStr = "A+";
  58.         return;
  59.     }
  60.  
  61.     //First we check the grade. Divide by 10 and cast to int... that way we compare the first digit
  62.     switch ((int)(gradeNum / 10))
  63.     {
  64.     case 9: //if grade is in the 90s
  65.         (*gradeStr)[0] = 'A';
  66.         break;
  67.     case 8: //if grade is in the 80s
  68.         (*gradeStr)[0] = 'B';
  69.         break;
  70.     case 7: //if grade is in the 70s
  71.         (*gradeStr)[0] = 'C';
  72.         break;
  73.     case 6: //if grade is in the 60s
  74.         (*gradeStr)[0] = 'D';
  75.         break;
  76.     default: //otherwise
  77.         (*gradeStr)[0] = 'F';
  78.         return; //return instead of break because there are no F+'s or F-'s
  79.     }
  80.  
  81.     //now we handle the + or -. Mod 10 to get just the last digit
  82.     switch (gradeNum % 10)
  83.     {
  84.     case 7: case 8: case 9: //if it's a X7, X8, or X9 then it's a +
  85.         (*gradeStr)[1] = '+';
  86.         break;
  87.     case 0: case 1: case 2: case 3: //if it's a X0, X1, X2, or X3, then it's a -
  88.         (*gradeStr)[1] = '-';
  89.         break;
  90.         //no default because we want it to be 0 to terminate the string
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement