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#

ahmed walid
ahmed walid
493 Points

why 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
Steven Parker
229,732 Points

A 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
ahmed walid
493 Points

sorry, 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
Steven Parker
229,732 Points

The method needs to return the string instead of printing it out.

ahmed walid
ahmed walid
493 Points

well am not getting the hang of it, have you tried solving it with a return method ?

Steven Parker
Steven Parker
229,732 Points

If 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
Chris Freeman
Treehouse Moderator 68,423 Points

Hi ahmed walid, I have just followed Steven Parker's advice and got your code to pass the challenge. Specifically,

  • replace "Console.WriteLine" with "return" in the three usages
  • Fix string to match expected value of "speed OK" instead of "speed is ok"

With these two changes, your code above will pass the challenge. Good Luck!

Steven Parker
Steven Parker
229,732 Points

In the original code the challenge starts with, the framework for the function is provided like this:

    static string CheckSpeed(double speed)
    {
        // YOUR CODE HERE
    }

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.

ahmed walid
ahmed walid
493 Points

since 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
Steven Parker
229,732 Points

There's a return type mismatch. See the comment I added to my answer.

ahmed walid
ahmed walid
493 Points

This worked. thank you guys for your effort. Wish you the best.