Advertisement
Zzyzx

Calculate GPA Using a Sentinel-Controlled Loop

Apr 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.34 KB | None | 0 0
  1. /* Name:        Zzyzx Wolfe
  2.  * Course:      CIS162AD 28929
  3.  * MEID:        ZZY2042543
  4.  * Assignment:  Lesson 7
  5.  * Date:        2018-04-20
  6.  * Purpose:     Calculate GPA from a variable number of entered letter grades. */
  7.  
  8. using System.Linq;
  9. using static System.Console;
  10.  
  11. namespace CIS162ADLesson7
  12. {
  13.     class CalculateGPA
  14.     {
  15.         static void Main()
  16.         {
  17.             string input = "";
  18.             decimal cumulativeGPA = 0.000M;
  19.             decimal averageGPA = 0.000M;
  20.             int totalScores = 0;
  21.             // Define valid responses.
  22.             var validInput = new[] { "A", "B", "C", "D", "F", "X" };
  23.             // Prompt for input.
  24.             WriteLine("Enter scores as A, B, C, D or F. Enter X when done.");
  25.             // Run sentinel-controlled loop to collect grades until user enters X.
  26.             while(input != "X")
  27.             {
  28.                 // Get the input.
  29.                 input = ReadLine();
  30.                 // Convert the input to upper case so user can enter in mixed case.
  31.                 input = input.ToUpper();
  32.                 // Make sure input matches a valid value.
  33.                 if (validInput.Contains(input))
  34.                 {
  35.                    
  36.                     switch (input)
  37.                     {
  38.                         case "A":
  39.                             // Increment total scores entered by one.
  40.                             ++totalScores;
  41.                             // Add 4 to the cumulative GPA.
  42.                             cumulativeGPA = cumulativeGPA + 4;
  43.                             break;
  44.                         case "B":
  45.                             // Increment total scores entered by one.
  46.                             ++totalScores;
  47.                             // Add 3 to the cumulative GPA.
  48.                             cumulativeGPA = cumulativeGPA + 3;
  49.                             break;
  50.                         case "C":
  51.                             // Increment total scores entered by one.
  52.                             ++totalScores;
  53.                             // Add 2 to the cumulative GPA.
  54.                             cumulativeGPA = cumulativeGPA + 2;
  55.                             break;
  56.                         case "D":
  57.                             // Increment total scores entered by one.
  58.                             ++totalScores;
  59.                             // Add 1 to the cumulative GPA.
  60.                             cumulativeGPA = cumulativeGPA + 1;
  61.                             break;
  62.                         case "F":
  63.                             // Increment total scores entered by one.
  64.                             ++totalScores;
  65.                             // Add 0 to the cumulative GPA.
  66.                             cumulativeGPA = cumulativeGPA + 0;
  67.                             break;
  68.                         default:
  69.                             // If they entered X or an invalid value, don't increment the number of scores or change the cumulative GPA.
  70.                             break;
  71.                     }
  72.                 }
  73.                 else
  74.                 {
  75.                     // If they didn't enter A, B, C, D, F or X, prompt them to enter something correct.
  76.                     WriteLine("Invalid response. Enter scores as A, B, C, D or F. Enter X when done.");
  77.                 }
  78.             }
  79.             // Make sure they made at least one valid input so we don't divide by 0.
  80.             if (totalScores > 0)
  81.             {
  82.                 // Calculate the GPA by dividing the cumulative GPA by the total number of scores entered.
  83.                 averageGPA = cumulativeGPA / totalScores;
  84.                 // Output the GPA to three decimal places. (This seems to be how the GPA is displayed at this school, so I'm going with that as the formatting assumption.)
  85.                 WriteLine("GPA is {0:N3}.", averageGPA);
  86.             }
  87.             // If they never entered any scores, at least let them know why they're not getting a result rather than printing a GPA of 0.000.
  88.             else
  89.             {
  90.                 WriteLine("You didn't enter any valid scores.");
  91.             }
  92.             // Prompt them to press a key to dismiss the console.
  93.             WriteLine("\n\nPress any key to continue...");
  94.             // Leave the console up until they press a key so they can see the output.
  95.             ReadKey();
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement