I am making a program to calculate GPA. I have it mostly working, but I can't figure out why it is asking for 2 inputs and why the calculation of GPA is wrong. I also need to make it so that I can input uppercase or lowercase letters. If you can offer a way to rewrite, it would be greatly appreciated.
This is the program:
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace gpa
{
class Program
{
static void Main(string[] args)
{
//create a list for the grades to go into
List<double> GradeList = new List<double>();
Console.Write("\n********************************************\n");
bool LastGrade = false;
while (LastGrade == false)
{
//pass letter as parameter to get the GradePoint
double Grade = Calculations.GetGradePoint();
if (Console.ReadLine() == "")
{
LastGrade = true;
}
else
{
GradeList.Add(Grade);
}
}
//add all the gradepoints and round to 2 decimal places
double TotalGradePoints = Math.Round(GradeList.Sum(), 2);
Console.WriteLine($"your total grade points are {TotalGradePoints}");
Console.WriteLine("How many total credits did you take this semester?");
double TotalCredits = Convert.ToDouble(Console.ReadLine());
Console.WriteLine($"total credits {TotalCredits}");
double EndGPA = Calculations.GPA(TotalGradePoints, TotalCredits);
Console.WriteLine($"Your GPA this semester will be {EndGPA}");
}
}
}
This is the calculations class I added:
using System;
namespace gpa
{
public class Calculations
{
public static double GPA(double points, double credits)
{
double average = points / credits;
return average;
}
public static double GetGradePoint()
{
Console.WriteLine("Enter your letter grade for each class");
double Grade = 0;
string letter = Console.ReadLine();
if (letter == "A")
{
return 4;
}
if (letter == "A-")
{
return 3.7;
}
if (letter == "B+")
{
return 3.3;
}
if (letter == "B")
{
return 3;
}
if (letter == "B-")
{
return 2.7;
}
if (letter == "C+")
{
return 2.3;
}
if (letter == "C")
{
return 2;
}
if (letter == "C-")
{
return 1.7;
}
if (letter == "D+")
{
return 1.3;
}
if (letter == "D")
{
return 1;
}
if (letter == "F")
{
return 0;
}
return Grade;
//do not need to add looping mechanism in this fucntion - loop it in the main function
// Write function that takes a letter grade as input
// and returns the grade-point value of that letter grade
// Replace your switch statement above with a call to this function
}
}
}
This is the output: enter image description here