C
C#16mo ago
ok.b00mer

❔ my code keeps on crashing. help me fix the code

6 Replies
ok.b00mer
ok.b00mer16mo ago
jdoodle.cs(146,29): error CS0136: A local variable named fromUnit' cannot be declared in this scope because it would give a different meaning to fromUnit', which is already used in a parent or current' scope to denote something else jdoodle.cs(154,29): error CS0136: A local variable named toUnit' cannot be declared in this scope because it would give a different meaning to toUnit', which is already used in a parent or current' scope to denote something else jdoodle.cs(158,32): error CS0136: A local variable named value' cannot be declared in this scope because it would give a different meaning to value', which is already used in a parent or current' scope to denote something else jdoodle.cs(218,29): error CS0136: A local variable named fromUnit' cannot be declared in this scope because it would give a different meaning to `fromUnit', which is already its not working
ok.b00mer
ok.b00mer16mo ago
JimmahDean
JimmahDean16mo ago
your issue is that else if (choice == 5) is inside else if (choice == 4)
else if (choice == 4)
{
Console.WriteLine("Which unit would you like to convert from?");
Console.WriteLine("1. Pounds");
Console.WriteLine("2. Kilograms");

int fromUnit = int.Parse(Console.ReadLine());

Console.WriteLine("Which unit would you like to convert to?");
Console.WriteLine("1. Pounds");
Console.WriteLine("2. Kilograms");

int toUnit = int.Parse(Console.ReadLine());

Console.WriteLine("Enter the value to convert:");

double value = double.Parse(Console.ReadLine());

if (fromUnit == 1 && toUnit == 2)
{
value /= 2.205;
}
else if (choice == 5)
{
else if (choice == 4)
{
Console.WriteLine("Which unit would you like to convert from?");
Console.WriteLine("1. Pounds");
Console.WriteLine("2. Kilograms");

int fromUnit = int.Parse(Console.ReadLine());

Console.WriteLine("Which unit would you like to convert to?");
Console.WriteLine("1. Pounds");
Console.WriteLine("2. Kilograms");

int toUnit = int.Parse(Console.ReadLine());

Console.WriteLine("Enter the value to convert:");

double value = double.Parse(Console.ReadLine());

if (fromUnit == 1 && toUnit == 2)
{
value /= 2.205;
}
else if (choice == 5)
{
you need to add another brace on line 138
TheRanger
TheRanger16mo ago
u cant define a variable of the same name twice in the same scope u can atleast reassign a value to it without redefining it
int fromUnit = int.Parse(Console.ReadLine()); // this is declaration and assigning a value

fromUnit = int.Parse(Console.ReadLine()); // this is only assigning a value
int fromUnit = int.Parse(Console.ReadLine()); // this is declaration and assigning a value

fromUnit = int.Parse(Console.ReadLine()); // this is only assigning a value
ok.b00mer
ok.b00mer16mo ago
Thanks for helping me fix the code
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.