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# Objects Loops and Final Touches For Loops

Won't let me use += operator for a double

I am getting an error that tells me that I cannot use the += operator, so I tried to write it as sum = sum + frogs[i]; and it still is giving me an error.

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            double sum = 0.0;
            for(int i=0; i<frogs.Length; i++){
                 sum = sum + frogs[i];
            }
            double avg = sum / frogs.Length;
            return avg;
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

        public Frog(int tongueLength)
        {
            TongueLength = tongueLength;
        }
    }
}

1 Answer

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,075 Points

It is not that you can't use the += operator with doubles. It is because of what you are trying to add to the double. Think carefully about your code, you are trying to add a frog object to a double. Basically you are trying to add a frog to a number, those are not compatible types to be trying to add together. It would be like trying to say: what does 1.2 + a cat equal?

So instead a hint on how to solve this would be getting each of the frog's tongue lengths, adding them together and then dividing by the total number of frogs.

Thank you! That helps A LOT!