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 If Statements "else if" and "else" Statements

Elfar Oliver
Elfar Oliver
3,924 Points

I've done the ifs I don't know what to return Cannot implicitly convert type 'double' to 'string'

The error Cannot implicitly convert type 'double' to 'string' always pops up and I don't know what to convert

Program.cs
using System;

class Program
{

    static string CheckSpeed(double speed)
    {
        if (speed > 65)
        {
            Console.WriteLine("too fast");   
        }
        else if (speed < 45)
        {
            Console.WriteLine("too slow");   
        }
        else 
        {
            Console.WriteLine("speed OK");   
        }
        return speed;
    }

    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));
    }

}

1 Answer

Steven Parker
Steven Parker
231,198 Points

The CheckSpeed method is defined as returning a string, but it tries to return speed, which is a double. That's what the compiler is complaining about.

Re-write it to make it return the strings instead of writing them to the console (that part is done in Main).

Elfar Oliver
Elfar Oliver
3,924 Points

Just to be sure, I keep the ifs out of the main?

Steven Parker
Steven Parker
231,198 Points

You won't modify Main for this challenge. All the work should be done in the body of CheckSpeed.

Elfar Oliver
Elfar Oliver
3,924 Points

In the body of CheckSpeed or should I alter the static string CheckSpeed?

Elfar Oliver
Elfar Oliver
3,924 Points

I could really use a hint right about now. I am no where nearer than before

Steven Parker
Steven Parker
231,198 Points

Your basic structure was pretty good, but you need to return the strings instead of writing them to the console. For example, instead of this:

        if (speed > 65)
        {
            Console.WriteLine("too fast");   
        }

... you might write this:

        if (speed > 65)
        {
            return "too fast";   
        }
Elfar Oliver
Elfar Oliver
3,924 Points

Thank you very much. As always I was just one step away(from throwing my laptop out the window)

So your "Re-write it to make it return the strings instead of writing them to the console" was actually the answer and not some huge hint