internal class Program { static void Main(string[] args) { double result = 0; Console.WriteLine("Welcome to the Physics calculator!"); Console.WriteLine("Please choose one of the following options"); Console.WriteLine("1.Unit Conversions"); Console.WriteLine(); Console.WriteLine("What is the current form of the number you are trying to convert?"); Console.WriteLine("1.Coulomb"); Console.WriteLine("2.mCoulomb"); Console.WriteLine("3.ÎĽCoulomb"); Console.WriteLine("4.nCoulomb"); Console.WriteLine("5.pCoulomb"); string answer1 = Console.ReadLine(); double answerbase = 0; double answerpow = 0; switch (answer1) { case "1": Console.WriteLine("Please continue to the next part"); answerbase = Convert.ToDouble(Console.ReadLine()); break; case "2": Console.WriteLine("Please enter the number: "); answerbase = Convert.ToDouble(Console.ReadLine()); break; case "3": Console.WriteLine("Please enter the number: "); answerbase = Convert.ToDouble(Console.ReadLine()); break; case "4": Console.WriteLine("Please enter the number: "); answerbase = Convert.ToDouble(Console.ReadLine()); break; case "5": Console.WriteLine("Please enter the number: "); answerbase = Convert.ToDouble(Console.ReadLine()); break; default: Console.WriteLine("That is not a valid option!"); break; } Console.WriteLine("In which for would you like to convert your number?"); Console.WriteLine("Choose one of the following options"); Console.WriteLine("1.Coulomb"); Console.WriteLine("2.mCoulomb"); Console.WriteLine("3.ÎĽCoulomb"); Console.WriteLine("4.nCoulomb"); Console.WriteLine("5.pCoulomb"); string answer2 = Console.ReadLine(); //Console.WriteLine("Please input your number"); //double answer3 = Convert.ToDouble(Console.ReadLine()); double resultconversion = 0; while (answer1 == "1") { if (answer1 == answer2) { Console.WriteLine("No conversion neeeded!"); } else if (answer1 != answer2) { switch (answer2) { case "2": answerpow = 10 ^ -3; resultconversion = Math.Pow(answerbase, answerpow); break; case "3": answerpow = 10 ^ -6; resultconversion = Math.Pow(answerbase, answerpow); break; case "4": answerpow = 10 ^ -9; resultconversion = Math.Pow(answerbase, answerpow); break; case "5": answerpow = 10 ^ -12; resultconversion = Math.Pow(answerbase, answerpow); break; } } } while(answer1 == "2") { if (answer1 == answer2) { Console.WriteLine("No conversion needed"); } else if (answer1 != answer2) switch (answer2) //line 107 { case "1": answerpow = 10 ^ 3; resultconversion = answerbase/(10 ^ 3); break; case "3": answerpow = 10 ^ 6; resultconversion = Math.Pow(answerbase, answerpow); break; case "4": answerpow = 10 ^ 9; resultconversion = Math.Pow(answerbase, answerpow); break; case "5": answerpow = 10 ^ 12; resultconversion = Math.Pow(answerbase, answerpow); break; } } Console.WriteLine($"The result in an int form: {answerbase}^{answerpow}"); Console.WriteLine($"The number {answerbase} is raised to the power of {answerpow}"); Console.WriteLine("The result in decimal for: " + resultconversion); } }
The problem that I face is that when I reach line 107 my code freezes and nothing happens. I am not sure what’s happening. Please note that the code is incomplete and the working functions as of right now are only the first and half of the second. I am a beginner and I would appreciate if you would not critisise me for not discovering the problem.
I added a while loop so for each answer specific things would happen. But inside the second while loop something seems to be broken inside the first switch case at line 107. When I input the form of the current number as mCoulomb and the form I am trying to convert it to Coulomb the program just freezes.
>Solution :
The problem is simple, you have a while loop on answer1 but you are not modifying it anywhere in your code. and that is the reason it is becoming an infinite loop. you can either add a break at the end of each while loop (you need to check this first). so modify the answer1 so that it may come out of the loop.