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

For loop can't compile due to error CS0019, how can this be rectified?

I am trying to find the average of an array of integers. I feel that my code meets this criteria but I keep being told that it cannot compile due to the += operator won't work on a type int and a type Treehouse.CodeChallenges.Frog.

I have tried everything I can thing of for now but I am a beginner, am I missing something obvious?

Thanks.

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)        
        {
            int total = 0;
            double average = 0;

            for(int i = 0; i < frogs.Length; i++)
            {
                 Frog frog = frogs[i];
                 total += frog;
            }

            return average = total/frogs.Length;
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

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

1 Answer

Steven Parker
Steven Parker
231,268 Points

You can't add a "frog" to a number. You probably wanted to add the tongue length:

                 total += frog.TongueLength;

And a "return" statement takes an expression but not an assignment:

            return total/frogs.Length;

So you also don't need the "average" variable.

Thanks again for your help Steven. Issue resolved with exactly those corrections. I see where I was going wrong now.