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.
CodePudding user response:
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). or modify the answer1
so that it may come out of the loop.
CodePudding user response:
Vivek's answer is correct as to what's happening in your program. You should mark his answer as correct.
I'm posting this as a way for you to see how this code can get written.
string unit = "Coulomb";
var factors = new[]
{
new { name = "", magnitude = 0 },
new { name = "m", magnitude = -3 },
new { name = "μ", magnitude = -6 },
new { name = "n", magnitude = -9 },
new { name = "p", magnitude = -12 },
};
string[] header1 = new string[] { "Welcome to the Physics calculator!", "Please choose one of the following options", "1.Unit Conversions", "", "What is the current form of the number you are trying to convert?", };
string[] header2 = new string[] { "In which for would you like to convert your number?", "Choose one of the following options", };
int GetFactorIndex(string[] header)
{
Console.WriteLine(String.Join(Environment.NewLine, header.Concat(factors.Select((x, n) => $"{n 1}. {x.name}{unit}"))));
return int.Parse(Console.ReadLine()) - 1;
}
int index1 = GetFactorIndex(header1);
Console.WriteLine("Please enter the number: ");
double value = double.Parse(Console.ReadLine());
int index2 = GetFactorIndex(header1);
int magnitude = factors[index1].magnitude - factors[index2].magnitude;
double factor = Math.Pow(10.0, magnitude);
double answer = value * factor;
Console.WriteLine($"The number {value} {factors[index1].name}{unit} is multiplied by {factor}");
Console.WriteLine($"The result in value of: {answer} {factors[index2].name}{unit}");
That's it. I think it is working correctly. Let me know if there are any issues you discover.