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 trial

C# C# Basics (Retired) Perfect if / else if / else

Cannot use "==" operator to compared a string or int, in statement if(input==21) do something, where input is an int

In a code challenge, I try to use an if statement to compare the variable "input" to an integer. The compiler claims there is no way "==" can be used to compare "input" to to an integer, while I have used this very method in past code challenges.

Please help.

CodeChallenge.cs
string input = Console.ReadLine();
int temperature = int.Parse(input);

if(input==21)   
    { 
        Console.WriteLine("Just right.");
    }

2 Answers

Matthew Tran
Matthew Tran
16,343 Points

In your code here, the input is still a string. You converted the input from a string to an integer and stored it into the temperature variable correctly. But then you used the input variable in your if statement instead of your newly converted temperature variable with the integer in it.

Also, this challenge tells you to say Just Right if its between two temperatures, 21 and 22, so you should be using the greater than and the less than operator at the same time.

Steven Parker
Steven Parker
229,786 Points

You'll need a few more tests and output statements.

Matthew's right about testing temperature instead of input, but you also need to add a few more tests and output statements. Your program should output one of three possible responses based on the value entered, so you need a bit more code yet.

Hint: This might be a good place for an "if...else if...else" chain.