Click here to Skip to main content
15,609,498 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am very new to C# and I have been learning off of a Brackeys tutorial and I am trying to the print the average of three numbers that are selected by the user. My code is shown below.

Does anyone know why line 22 gets a syntax error? If so, please respond. The error I get is

C:\Users\perry\Desktop\My Awesome Program\Program.cs(22,42): error CS1003: Syntax error, ',' expected [C:\Users\perry\Desktop\My Awesome Program\My Awesome Program.csproj]


What I have tried:

  1  using System;
  3  namespace My_Awesome_Program
  4  {
  5      class Program
  6      {
  7          static void Main(string[] args)
  8          {
 10           int num1;
 11           int num2;
 12           int num3;
 14           Console.Write("Enter your first number: ");
 15           num1 = Convert.ToInt32(Console.ReadLine());
 16           Console.Write("Enter your second number: ");
 17           num2 = Convert.ToInt32(Console.ReadLine());
 18           Console.Write("Enter your third letter: ");
 19           num3 = Convert.ToInt32(Console.ReadLine());
 21           int avgRes = num1 + num2 + num3 / 3;
 22           Console.Write("The average of " + num1 + num2 + num3 + " is " + avgRes);
 23           // Console.Write("The average of " , num1 , num2 , num3 , " is " , avgRes);
 25             //Wait before closing
 27              Console.ReadKey();
 28          }
 29      }
 30  }
Updated 4-May-22 16:26pm
Richard Deeming 4-May-22 6:18am    
Neither version of the code you've posted produces the syntax error you describe.

However, your "average" calculation does nothing of the sort. Aside from the fact that you're performing integer division, which will truncate the result, you're also only dividing the third number by the count, rather than dividing the sum of the numbers by the count.

Eg: Enter 1, 1, 1, and your code will tell you that the average is 2, which is clearly wrong.

double avgRes = (num1 + num2 + num3) / 3D;
Console.WriteLine("The average of ({0}, {1}, {2}) is {3}", num1, num2, num3, avgRes);
Graeme_Grant 4-May-22 6:44am    
Console.WriteLine($"The average of ({num1}, {num2}, {num3} is {avgRes}");

As Richard has said, that code does not generate a compilation error, not does it calculate an average properly.

I'd suggest that you start using string interpolation though, it makes your code easier to read:
Console.WriteLine($"The average of {num1 + num2 + num3} is {avgRes}");
And produces the same result.

But don't ever use Convert methods on user input: they fail if he miskeys. Instead, use the TryParse methods:
if (!int.TryParse(Console.ReadLine(), out num1))
   ... report input problem to the user ...
With only three inputs it's not a major problem - but if you've just typed 20 values in and miskey the last one ... if the app crashes the user is not going to be a happy bunny ... Write yourself a method to get an integer and loop until the user enters a valid number.
Share this answer
In your problem statement, you are taking inputs as integer and can take avgRes as Integer but the error is you need to enclosed three variables in parenthesis "()" due to operator presidency. Without parenthesis "/" have high presidency and will calculate before others. Secondly, you can also use String.Format() method to align your values.
int avgRes = (num1 + num2 + num3) / 3;
Console.Write(String.Format("The Average of {0}, {1}, {2} is {3}", num1, num2, num3, avgRes));
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900