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 trialahmed walid
493 Pointswhy isnt the code running in this quiz ?
this quiz on c#, i did all the code and it is running on my VS code but not on the website giving the following the result
( too fast
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at Test.Main(String[] args) in /workdir/Test.cs:line 30 )
the link of the challenge ( https://teamtreehouse.com/library/c-basics-2/if-statements/else-if-and-else-statements )
\This is my code
using System;
class Program
{
static void CheckSpeed(double speed)
{
// YOUR CODE HERE
if(speed>65)
{
Console.WriteLine("too fast");
}
else if(speed<45)
{
Console.WriteLine("too slow");
}
else
{
Console.WriteLine("speed is ok");
}
}
static void Main(string[] args)
{
// This should print "speed is ok".
CheckSpeed(53);
// This should print "too fast".
CheckSpeed(88);
}
}
3 Answers
Steven Parker
231,248 PointsA single "=" character is an assignment operator, but in a conditional expression you need a comparison operator.
Also, be careful about the choice of operator because the challenge wants you to detect a range of values and not just an equality.
And when doing challenge code outside of the challenge, it's easy to "trick" yourself into thinking code is working. The challenge will be looking for very specific results to a variety of inputs when it tests your code.
ahmed walid
493 Pointssince the last time i checked the challenge has been updated this is the challenge link
https://teamtreehouse.com/library/c-basics-2/if-statements/else-if-and-else-statements
and this is the code i wrote
using System;
class Program
{
static double CheckSpeed(double speed)
{
// YOUR CODE HERE
if(speed>65)
{
return ("too fast");
}
else if(speed<45)
{
return ("too slow");
}
else
{
return ("speed OK");
}
}
static void Main(string[] args)
{
// This should print "too slow".
Console.WriteLine(CheckSpeed(44));
// This should print "too fast".
Console.WriteLine(CheckSpeed(88));
// This should print "speed OK".
Console.WriteLine(CheckSpeed(55));
}
}
and this is the error i got is:
Program.cs(11,21): error CS0029: Cannot implicitly convert type 'string' to 'double' [/workdir/workspace.csproj]
Program.cs(15,21): error CS0029: Cannot implicitly convert type 'string' to 'double' [/workdir/workspace.csproj]
Program.cs(19,21): error CS0029: Cannot implicitly convert type 'string' to 'double' [/workdir/workspace.csproj]
The build failed. Please fix the build errors and run again.
any idea on how to convert the type ?
Steven Parker
231,248 PointsThere's a return type mismatch. See the comment I added to my answer.
ahmed walid
493 PointsThis worked. thank you guys for your effort. Wish you the best.
ahmed walid
493 Pointsahmed walid
493 Pointssorry, when i uploaded i did upload the wrong version of code, however i did fix it and updated the question with the working code, still same problem exist.
this is the error message ( Program.cs(6,17): error CS0161: 'Program.CheckSpeed(double)': not all code paths return a value [/workdir/workspace.csproj]
The build failed. Please fix the build errors and run again. )
Steven Parker
231,248 PointsSteven Parker
231,248 PointsThe method needs to return the string instead of printing it out.
ahmed walid
493 Pointsahmed walid
493 Pointswell am not getting the hang of it, have you tried solving it with a return method ?
Steven Parker
231,248 PointsSteven Parker
231,248 PointsIf you change the "
Console.WriteLine
"s in the code above to "return
"s that will solve the error you were getting.The only other thing is that one of the strings in this code is "speed is ok", but the challenge is expecting to see "speed OK" instead.
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsHi ahmed walid, I have just followed Steven Parker's advice and got your code to pass the challenge. Specifically,
Console.WriteLine
" with "return
" in the three usagesWith these two changes, your code above will pass the challenge. Good Luck!
Steven Parker
231,248 PointsSteven Parker
231,248 PointsIn the original code the challenge starts with, the framework for the function is provided like this:
Somehow, the return type of "string" got changed to "double" in your latest example, which is not something asked for by the instructions (and it causes the error).
Be sure to add your new code inside the function without changing any of the provided code. Then you'll pass.