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

Fredrik Rönnehag
Fredrik Rönnehag
2,342 Points

Can't compile, code should work?

Error FrogStats.cs(12,16): error CS0019: Operator +=' cannot be applied to operands of typeint' and `Treehouse.CodeChallenges.Frog' Compilation failed: 1 error(s), 0 warnings

But it shouldn't be an int with a class Frog. Since I sum it up with the indexvalue of frogs[i] ? and the Frog[] stores int values. My mind is boggled...

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

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

           double average = (double)sum / frogs.Length;
           return average;

        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

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

2 Answers

Fredrik Rönnehag
Fredrik Rönnehag
2,342 Points

Ops, found the error.

 sum += frogs[i].TongueLength;

Somtimes it's hard without the red squiggly line ;)

HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
21,058 Points

Please try this answer

 double total=0;
            for(int i=0;i<frogs.Length;i++)
            {
                total+=frogs[i].TongueLength;
            }
            return total/frogs.Length;

If you need explaination leave a comment please

Enjoy Coding