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 trialSarker Jawad Islam
3,587 PointsCannot use operand "+=" for the frog code challenge for some reason
FrogStats.cs(11,17): error CS0019: Operator +=' cannot be applied to operands of type
double' and `Treehouse.CodeChallenges.Frog'
That is the error I am getting.
I tried using + just to test and it seems like any operand I use causes an error.
Not sure what I am doing wrong
namespace Treehouse.CodeChallenges
{
class FrogStats
{
public static double GetAverageTongueLength(Frog[] frogs)
{ double result;
int num = frogs.Length;
for(int i=0; i< frogs.Length; i++)
{
result += frogs[i];
}
result /= num;
return result;
}
}
}
namespace Treehouse.CodeChallenges
{
public class Frog
{
public int TongueLength { get; }
public Frog(int tongueLength)
{
TongueLength = tongueLength;
}
}
}
1 Answer
Steven Parker
231,248 PointsThe "preview" button error message is telling you that you're trying to add the frog itself, so instead use just the TongueLength:
result += frogs[i].TongueLength;
The other issue is that you cannot use the '+=' operator on variables that haven't been initialized (only declared), so give "result" an initial value:
double result = 0;
Sarker Jawad Islam
3,587 PointsSarker Jawad Islam
3,587 PointsThank you very much that solved it!.