Kylie
Kylie

Reputation: 1

I am having trouble figuring out why my code will not print the calculation for the GPA

using System;

namespace gpa
{
    class gpa
    {
        static void Main(string [] args)
        {
            double credit = 0;
            double totalCreditHours = 0;

        char grade = ' ';
        double gradePoints = 0;
        double totalGradePoints = 0;

        int counter = 0;
        double gpa = 0;

        do 
        {
            Console.Write("Enter letter grade for class #{0} \n(use A, B, C, or D. Type 0 after all classes entered.): ", counter += 1);
            char userInput = char.Parse(Console.ReadLine());

            if (userInput == '0')
            {
                break;
            }

            else 
            {
                grade = userInput;
                Console.Write("Enter your credit hours: ");
                credit = int.Parse(Console.ReadLine());

                switch (grade)
                {
                    case 'A': gradePoints = 4;
                        break;
                    case 'B': gradePoints = 3;
                        break;
                    case 'C': gradePoints = 2;
                        break;
                    case 'D': gradePoints = 1;
                        break;

                }

                totalGradePoints = totalGradePoints + (credit * gradePoints);
                totalCreditHours = totalCreditHours + credit;

            } 

        } while (grade != 0);

            gpa = CalculateGPA(totalGradePoints, totalCreditHours);
            Console.Write("Your GPA is ", gpa);

            Console.ReadKey();
        }


        static double CalculateGPA(double totalGradePoints, double totalCreditHours)
        {
            return (totalGradePoints / totalCreditHours);

        }
    }
}

----this last part is what i think is wrong, but i am not sure how to fix it. It will not output the actual calculated gpa

Upvotes: 0

Views: 46

Answers (1)

James Dev
James Dev

Reputation: 3009

You need this line instead -

Console.Write("Your GPA is " + gpa);

Or

Console.Write("Your GPA is {0}" ,gpa);

Upvotes: 2

Related Questions