Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C#

John Weber
John Weber
6,273 Points

C# Basics: Bonus exercises (Calculator)

I am getting a specific error over and over again on a C# Bonus Exercise (Programming a Calculator). I am brand new to the language and would appreciate any help I could get.

The program is intended to take two numbers from a user, an operator, and then combine the numbers with the operator.

using System;

namespace Treehouse.bonus
{
  class Program
  {
    static void Main()
    {
      while(true) {

      Console.WriteLine("Enter a number: ");

      var number1 = Console.ReadLine();

      Console.WriteLine("Enter another number: ");

      var number2 = Console.ReadLine();

      Console.WriteLine("Enter an operator (+, -, *, /, or %): ");

      var sign = Console.ReadLine();

      try 
      {
        Double number3 = Double.Parse(number1);

        Double number4 = Double.Parse(number2);

        if(sign == "+")
          {
            Console.WriteLine(number1 + " + " + number2 + " = " (number3 + number4));
          }

        else if(sign == "-")
          {
            Console.WriteLine(number1 + " - " + number2 + " = " (number3 - number4));
          }

        else if(sign == "*")
          {
            Console.WriteLine(number1 + " * " + number2 + " = " (number3 * number4));
          }

        else if(sign == "/")
          {
            Console.WriteLine(number1 + " / " + number2 + " = " (number3 / number4));
          }

        else
          {
            Console.WriteLine("The operator you selected isn't valid... Please try again.");
            continue;
          }

        }

      catch(FormatException)

        {
          Console.WriteLine("One or more of numbers you inputed is invalid. Please try again.");
          continue;
        } 
      }
  }

}
}

The error message I am getting is this:

quiz.cs(31,59): error CS0119: Expression denotes a value', where amethod group' was expected
quiz.cs(36,59): error CS0119: Expression denotes a value', where amethod group' was expected
quiz.cs(41,59): error CS0119: Expression denotes a value', where amethod group' was expected
quiz.cs(46,59): error CS0119: Expression denotes a value', where amethod group' was expected

... in regards to the else/if operator lines of code. If I simply use an '=' as opposed to '==', I get a different set of errors... What am I missing?

Thank you in advance for your help!!!

2 Answers

Well, you seem to be missing concatenation operator before printing the result. For instance,

Console.WriteLine(number1 + " + " + number2 + " = " (number3 + number4));

in the above statement, after " = ", you can drop in a '+' so that string concatenation can happen. So, it should be as below:

Console.WriteLine(number1 + " + " + number2 + " = " + (number3 + number4));

You may do this for the lines where errors are reported (i.e. 31, 36, 41, 46).

John Weber
John Weber
6,273 Points

Thank you so much for answering both my questions, Srikar. I am just learning to program and sometimes it is really hard to catch simple mistakes without another set of eyes. You just made my night!!!

You are welcome, John: