-2

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

1 Answers1

0

Your program logic is flawed. So. You have a loop that keeps looping and invokes a method. In this method GetGradePoint you ask for the Grade input and then read the input using Console.ReadLine().

This is fine. The issue comes after. You wait for an input from the user again and if they input anything other than an empty string, the program will add the first input grade value and then loop back around as LastGrade is still false.

This means that the program starts from the start of the loop which means it will then invoke the GetGradePoint method again which is why it asks for the input again. Also if you then enter an empty string, the grade the user input doesn't get added to the GradeList list.

You need to go through your program as the issue, as I said, is the logic of your program. I suspect the other issue of the GPA being wrong will also correct itself.

Code Tips:

Instead of doing 1,000 if statements, as you're only accepting a single character you can use else if for the rest of the statements. Although this doesn't matter a huge lot as you are returning a value. IE:

if(condition)
{
}
else if(other condition)
{
}

Your while loop accepts a Boolean value as the condition which == provides but you can instead use your LastGrade variable as the condition for the loop IE:

while(!LastPass)
{
}

As for getting input I would rewrite it to something like

//Remove the line in GetGradePoint if you're gonna put it here
Console.WriteLine("Enter your grades");
while(condition)
{
    string input = Console.ReadLine();
    if(input != "")
    {
        var grade = GetGradePoint(input);
        GradeList.Add(grade);
    }
    else LastGrade = true;
}
//Rest of code to calculate GPA

This is rough pseudo-c# code. As for accepting both lower and upper case: You can use .ToUpper() on the input to ensure the input is an uppercase letter even if the user enters a lowercase letter.

li223
  • 369
  • 3
  • 11