Taras 0 #1 Posted February 16, 2016 (edited) 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 February 16, 2016 by Taras Share this post Link to post Share on other sites
Taras 0 #2 Posted February 16, 2016 I did this and it workedstatic 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
A Duck Tale 0 #3 Posted February 18, 2016 (edited) 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 February 18, 2016 by A Duck Tale Share this post Link to post Share on other sites
Mayhem' 0 #4 Posted March 7, 2016 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