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 trialJohn Weber
6,273 PointsC# Basics: Bonus exercises (Averager)
I am trying to build one of the 'bonus projects' for C# Basics... I am brand new to the language and I can't understand why this won't compile.
The program is intended to take numbers from the user, and average them.
using System;
namespace Treehouse.ExtraProjects
{
class Program
{
static void Main()
{
int a = 0;
while(true){
Console.Write("Enter numbers to be averaged... type 'quit' when you want a total: ");
var input = Console.ReadLine();
if (input.ToLower = "quit")
{
break;
}
else
try
{
var b += Double.Parse(input);
a += 1;
continue;
}
catch(FormatException)
{
Console.WriteLine("That is not valid input.");
continue;
}
}
Console.WriteLine(b / a);
}
}
}
I am getting error messages regarding the "+=" with variable b, as well an error message regarding the "else-try" statement.
I know this is probably an obvious mistake, but any help you guys could give me would be much appreciated.
Thanks in advance!
EDIT:
I have caught one of my mistakes, and declared
var b = 0.0;
at the top of my code.. but I am still getting:
averager.cs(20,23): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer
... and I don't know how to fix it. This is error is in regards to:
if (input.ToLower = "quit")
1 Answer
srikarvedantam
8,369 PointsThere are a couple of errors in the below conditional check:
if (input.ToLower = "quit")
- After "input.ToLower", function call operator "()" is missing.
- You should use "Equals" method rather than '=' to check for string equality.
Therefore, the corrected conditional check should be:
if (input.ToLower().Equals("quit"))
John Weber
6,273 PointsJohn Weber
6,273 PointsThanks a million!!! My program works perfectly!!!