Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRiley Sutton
889 PointsC# Compiler error
New to programming. I cant seam to find out why im getting the following an error here.
StudentsCode.cs(18,2): error CS1525: Unexpected symbol `{' StudentsCode.cs(18,3): warning CS0642: Possible mistaken empty statement Compilation failed: 1 error(s), 1 warnings
string input = Console.ReadLine();
int temperature = int.Parse(input);
if(temperature < 21)
{
Console.WriteLine("Too Cold!");
}
else if(temperature <= 22)
{
Console.WriteLine("Just right.");
}
else(temperature > 22)
{
Console.WriteLine("Too hot!");
}
2 Answers
Afrid Mondal
6,255 PointsHey Riley, you are doing great. But, remember never put condition on else block, because it's statement going to execute if any other statement is not matched the condition. I correct your code and it will run...
string input = Console.ReadLine();
int temperature = int.Parse(input);
if(temperature < 21) {
Console.WriteLine("Too Cold!");
} else if(temperature == 21 || temperature == 22){
Console.WriteLine("Just right.");
} else {
Console.WriteLine("Too hot!");
}
Steven Parker
231,269 PointsJust convert the unneeded test after the else into a comment:
if (temperature < 21)
Console.WriteLine("Too Cold!");
else if(temperature <= 22)
Console.WriteLine("Just right.");
else //(temperature > 22)
Console.WriteLine("Too hot!");
It fixes it, and it's a good programming practice. (Also for brevity I removed the braces - they aren't needed when there's only one statement anyway).
Riley Sutton
889 PointsThank you Steven!
Jordan Power
6,503 PointsJordan Power
6,503 PointsThink of the else block to be the default code block that runs when none of the above conditions are met (AKA, none of the conditional statements return true!) Afrid was spot on with this, your error is due to the conditional statement you wrote after the else keyword.
Riley Sutton
889 PointsRiley Sutton
889 PointsThank you!