Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Name: Zzyzx Wolfe
- * Course: CIS162AD 28929
- * MEID: ZZY2042543
- * Assignment: Lesson 7
- * Date: 2018-04-20
- * Purpose: Calculate GPA from a variable number of entered letter grades. */
- using System.Linq;
- using static System.Console;
- namespace CIS162ADLesson7
- {
- class CalculateGPA
- {
- static void Main()
- {
- string input = "";
- decimal cumulativeGPA = 0.000M;
- decimal averageGPA = 0.000M;
- int totalScores = 0;
- // Define valid responses.
- var validInput = new[] { "A", "B", "C", "D", "F", "X" };
- // Prompt for input.
- WriteLine("Enter scores as A, B, C, D or F. Enter X when done.");
- // Run sentinel-controlled loop to collect grades until user enters X.
- while(input != "X")
- {
- // Get the input.
- input = ReadLine();
- // Convert the input to upper case so user can enter in mixed case.
- input = input.ToUpper();
- // Make sure input matches a valid value.
- if (validInput.Contains(input))
- {
- switch (input)
- {
- case "A":
- // Increment total scores entered by one.
- ++totalScores;
- // Add 4 to the cumulative GPA.
- cumulativeGPA = cumulativeGPA + 4;
- break;
- case "B":
- // Increment total scores entered by one.
- ++totalScores;
- // Add 3 to the cumulative GPA.
- cumulativeGPA = cumulativeGPA + 3;
- break;
- case "C":
- // Increment total scores entered by one.
- ++totalScores;
- // Add 2 to the cumulative GPA.
- cumulativeGPA = cumulativeGPA + 2;
- break;
- case "D":
- // Increment total scores entered by one.
- ++totalScores;
- // Add 1 to the cumulative GPA.
- cumulativeGPA = cumulativeGPA + 1;
- break;
- case "F":
- // Increment total scores entered by one.
- ++totalScores;
- // Add 0 to the cumulative GPA.
- cumulativeGPA = cumulativeGPA + 0;
- break;
- default:
- // If they entered X or an invalid value, don't increment the number of scores or change the cumulative GPA.
- break;
- }
- }
- else
- {
- // If they didn't enter A, B, C, D, F or X, prompt them to enter something correct.
- WriteLine("Invalid response. Enter scores as A, B, C, D or F. Enter X when done.");
- }
- }
- // Make sure they made at least one valid input so we don't divide by 0.
- if (totalScores > 0)
- {
- // Calculate the GPA by dividing the cumulative GPA by the total number of scores entered.
- averageGPA = cumulativeGPA / totalScores;
- // 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.)
- WriteLine("GPA is {0:N3}.", averageGPA);
- }
- // 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.
- else
- {
- WriteLine("You didn't enter any valid scores.");
- }
- // Prompt them to press a key to dismiss the console.
- WriteLine("\n\nPress any key to continue...");
- // Leave the console up until they press a key so they can see the output.
- ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement