A community college requires students to finish at least 72 credit hours and have a GPA of 2.0 or above in order to graduate. Create a C# console application to do the following. Ask how many credit hours a student has finished and what GPA this student has. Use only one if…else statement to decide and display whether this student can graduate or not. I believe I am missing something in regards to the 'gradePointAverage' integer. I am looking for it to accept the user's input of their GPA in the format of 'x.x' . When I input a non-decimal it works but it doesn't when I put in a decimal format. I tried creating a string or decimal but I have no clue right now. Advice and guidance will be very appreciated.

Here is what I have so far:

using System;

public class Question01
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Graduation Requirements");
        Console.WriteLine("===========");
        Console.WriteLine();

        Console.Write("How many credit hours have you completed: ");
        int creditHours;
        creditHours = Convert.ToInt32(Console.ReadLine());

        Console.Write("What is your current GPA: ");
        int gradePointAverage;
        gradePointAverage = Convert.ToInt32(Console.ReadLine());

        if (creditHours >= 72 && gradePointAverage > (2.0))
        {
            Console.WriteLine("You're eligible to graduate!");
        }

        else
        {
            Console.WriteLine("You're not eligible to graduate, sorry.");
        }
    }
}

Recommended Answers

All 4 Replies

@rproffit

I have tried that but I think I have figured it out.

using System;

public class Question01
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Graduation Requirements");
        Console.WriteLine("=======================");
        Console.WriteLine();

        Console.Write("How many credit hours have you completed: ");
        int creditHours;
        creditHours = Convert.ToInt32(Console.ReadLine());

        Console.Write("What is your current GPA: ");
        int gradePointAverage;
        gradePointAverage = (int)Convert.ToDouble(Console.ReadLine());

        if (creditHours >= 72 && gradePointAverage > 2)
        {
            Console.WriteLine("You're eligible to graduate!");
        }

        else
        {
            Console.WriteLine("You're not eligible to graduate, sorry.");
        }
    }
}

Possibly solved.

I would suggest that instead of going through the expense of an extra conversion, just keep gradePointAverage as a double. This will also be more accurate, since, with your code, a GPA of 2.1 will fail, because it will be converted to 2. The conditional will stll work, but you might need to keep the value at 2.0, which is closer to what your assignment asks for.

You could also simplify your code with a Dictionary<bool,string>:

    Dictionary<bool, string> responses = new Dictionary<bool, string>
        {
            {true, "You're eligible to graduate!"},
            {false,"You're not eligible to graduate, sorry."}
        };
    double gradePointAverage;
    gradePointAverage = Convert.ToDouble(Console.ReadLine());
    Console.WriteLine(responses[(creditHours >= 72 && gradePointAverage > 2.0)]);

One advantage of this approach is maintainability. If you need to change one or both strings, this is only done in one place, regardless of how many times your code references the Dictionary.

commented: Great answer! +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.