Jump to content
Taras

Help with C#

Recommended Posts

So I have a homework assignment to write a basic code.  I did the easy part, but the second question is asking this:

Modify problem 1.  Use a loop to allow the user to check graduation eligibility for as many users as they would.  Explain the type of loop that used.  Explain why you chose that type of loop?  No pseudocode required

This is problem 1: 

static void Main(string[] args)
        {
            double gradePointAverage;
            double creditHours;
            Console.Write("Please enter the grade point average: ");
            gradePointAverage = Convert.ToDouble(Console.ReadLine());
            Console.Write("Please enter total credit hours: ");
            creditHours = Convert.ToDouble(Console.ReadLine());
            if (creditHours > 72 && gradePointAverage > 2.5)
            {
                Console.WriteLine("Congratulations, you are eligible to graduate!");
            }
            else if (gradePointAverage < 2.5)
            {
                Console.WriteLine("You need to work on your grade point average before you can graduate!");
            }
            else
            {
                Console.WriteLine("You do not have enough hours to graduate.");
            }
        }
[/code]

I don't understand how the user can input over and over for as many times as I want.  Don't loops stop once the condition is false?  Anyone help me out?

Edited by Taras

Share this post


Link to post
Share on other sites

I did this and it worked

static void Main(string[] args)
        {
            bool quitApplication = false;
            double gradePointAverage;
            double creditHours;
            do
            {
                Console.WriteLine("Hit 'ctrl c' to exit application!");
                Console.WriteLine();
                Console.Write("Please enter the grade point average: ");
                gradePointAverage = Convert.ToDouble(Console.ReadLine());
                Console.Write("Please enter total credit hours: ");
                creditHours = Convert.ToDouble(Console.ReadLine());
                if (creditHours > 72 && gradePointAverage > 2.5)
                {
                    Console.WriteLine("Congratulations, you are eligible to graduate!");
                    Console.WriteLine();
                }
                else if (gradePointAverage < 2.5)
                {
                    Console.WriteLine("You need to work on your grade point average before you can graduate!");
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("You do not have enough hours to graduate.");
                    Console.WriteLine();
                }
            } while (!quitApplication);
            }

Is this the right way to do it?

Share this post


Link to post
Share on other sites

I did this and it worked

 

static void Main(string[] args)
        {
            bool quitApplication = false;
            double gradePointAverage;
            double creditHours;
            do
            {
                Console.WriteLine("Hit 'ctrl c' to exit application!");
                Console.WriteLine();
                Console.Write("Please enter the grade point average: ");
                gradePointAverage = Convert.ToDouble(Console.ReadLine());
                Console.Write("Please enter total credit hours: ");
                creditHours = Convert.ToDouble(Console.ReadLine());
                if (creditHours > 72 && gradePointAverage > 2.5)
                {
                    Console.WriteLine("Congratulations, you are eligible to graduate!");
                    Console.WriteLine();
                }
                else if (gradePointAverage < 2.5)
                {
                    Console.WriteLine("You need to work on your grade point average before you can graduate!");
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("You do not have enough hours to graduate.");
                    Console.WriteLine();
                }
            } while (!quitApplication);
            }

Is this the right way to do it?

If it works as intended (which it does), Then it is one of the many 'right ways' you could have done this.

To improve the code above, you could remove the variable for your while loop and just have:

 

while(true){ 
      //this will loop indefinitely.
}
 and add some exceptions for your inputs; something like this:
static void Main(string[] args)
        {
            double gradePointAverage;
            double creditHours;
            while(true) // This will loop indefinitely 
            {
                Console.WriteLine("Hit 'ctrl c' to exit application!\n");
                Console.Write("Please enter the grade point average: ");
                try
                {
                    gradePointAverage = Convert.ToDouble(Console.ReadLine());
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error trying to convert to double (GPA). Did you make a typo?");
                    //Console.WriteLine(e); //Print out the exception.
                    continue; // Go back to the start of the loop.
                }
                Console.Write("Please enter total credit hours: ");
                try
                {
                    creditHours = Convert.ToDouble(Console.ReadLine());
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error trying to convert to double (creditHours). Did you make a typo?");
                    //Console.WriteLine(e); //Print out the exception.
                    continue; // Go back to the start of the loop.
                }
                if (creditHours > 72 && gradePointAverage > 2.5)
                {
                    Console.WriteLine("Congratulations, you are eligible to graduate!\n");
                }
                else if (gradePointAverage < 2.5)
                {
                    Console.WriteLine("You need to work on your grade point average before you can graduate!\n");
                }
                else
                {
                    Console.WriteLine("You do not have enough hours to graduate.\n");
                }
            } 
        }

 

Edited by A Duck Tale

Share this post


Link to post
Share on other sites

This reply is probably too late, but what he said above should work, (I don't have C# anymore, just replaced harddrive), but if I am not mistaken, you could do something like this:

 

static void Main(string[] args)
        {
         Loop:   
                bool quitApplication = false;
                double gradePointAverage;
                double creditHours;
                Console.WriteLine("Hit 'ctrl c' to exit application!");
                Console.WriteLine();
                Console.Write("Please enter the grade point average: ");
                gradePointAverage = Convert.ToDouble(Console.ReadLine());
                Console.Write("Please enter total credit hours: ");
                creditHours = Convert.ToDouble(Console.ReadLine());
                if (creditHours > 72 && gradePointAverage > 2.5)
                {
                    Console.WriteLine("Congratulations, you are eligible to graduate!");
                    Console.WriteLine();
                }
                else if (gradePointAverage < 2.5)
                {
                    Console.WriteLine("You need to work on your grade point average before you can graduate!");
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("You do not have enough hours to graduate.");
                    Console.WriteLine();
                }
            goto Loop;
}

It's been a long time since I used labeled statements, and probably not recommended, I just like to point out unique ways that people can do things.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×