"Top-level statements must precede namespace and type declarations"
Asked Answered
D

3

10

So I haven't been coding for long so I'm not so experienced, I recently ran into a problem on replit.com where the console would print out:

error CS8803: Top-level statements must precede namespace and type declarations.
using System;

could anyone suggest the problem? Here is my code for anyone wondering:

int English;
int Science;
int AverageCalc;

AverageCalc = Convert.ToInt32(Console.ReadLine());

class Program {
  public static void Main (string[] args) {
    Console.WriteLine("Write your math grades");

      Math = Convert.ToInt32(Console.ReadLine());

      Console.WriteLine("Write your english grades");

      English = Convert.ToInt32(Console.ReadLine());

      Console.WriteLine("Write your science grades");

      Science = Convert.ToInt32(Console.ReadLine());

      AverageCalc = (Math+English+Science/3);
  }
}

if (AverageCalc > 80)
{
  Console.WriteLine("You passed with A mark!");
}
else if (AverageCalc < 80)
{
  Console.WriteLine("You passed with B mark!");
}
else if (AverageCalc < 65)
{
  Console.WriteLine("You passed with C mark!");
}
else if (AverageCalc < 60)
{
  Console.WriteLine("You passed with D mark!");
}
else if (AverageCalc < 55)
{
  Console.WriteLine("You got lower than D mark, try better next time.");
}
Dunigan answered 17/10, 2021 at 5:42 Comment(1)
B
9

As mentioned by @Caius in his answer you are fixing top level statement and classical way both in your code.

You can follow the approach suggested by him or just remove the below part from your code.

class Program
{
    public static void Main (string[] args) 
    {

And closing } of Program class and Main method .

Example taken from documentation.

Below codes are same.

using System;

namespace Application
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

TLS

Console.WriteLine("Hello, World!");
Bourgeoisie answered 17/10, 2021 at 11:56 Comment(1)
This new top-level thing is useless. Unless one is just writing a few hello-world lines, which I think is far uncommon, one sooner or later need to add this Main structure anyway, and VS Code's "Quick Fix" can't automatically add that structure. What was the point of removing Main from the template? It only wastes more time.Minoan
F
7

You're mixing top level statements and non-TLS. TLS essentially allows you to do away with all the namespace/class/static main and just write a C# program as if it were the contents of the Main method - TLS tutorial

In essence, this means program structure is a bit wonky because you start with some TLS, then go more regular style, then back to TLS; switching away from a TLS structure (I would suggest you just get used to the namespace/class/main fluff; it's still heavily used, and to some extent it's a reasonable intro to curly braces and scope). It looks like:

namespace X{
    class Program {
        public static void Main (string[] args) {
    
            int English;
            int Science;
            int AverageCalc;
        
            Console.WriteLine("Write your math grades");
        
            Math = Convert.ToInt32(Console.ReadLine());
        
            Console.WriteLine("Write your english grades");
        
            English = Convert.ToInt32(Console.ReadLine());
        
            Console.WriteLine("Write your science grades");
        
            Science = Convert.ToInt32(Console.ReadLine());
        
            AverageCalc = (Math+English+Science/3);
    
    
            if (AverageCalc > 80)
            {
              Console.WriteLine("You passed with A mark!");
            }
            else if (AverageCalc < 80)
            {
              Console.WriteLine("You passed with B mark!");
            }
            else if (AverageCalc < 65)
            {
              Console.WriteLine("You passed with C mark!");
            }
            else if (AverageCalc < 60)
            {
              Console.WriteLine("You passed with D mark!");
            }
            else if (AverageCalc < 55)
            {
              Console.WriteLine("You got lower than D mark, try better next time.");
            }

            Console.WriteLine("Press ENTER to exit (hah)");
            Console.ReadLine();
        }
    }
}

A simple program like this is conducted entirely within the scope of the Main method's curly braces. Later, when you start getting into actual object oriented things and having more than one student whose grades you're tracking, you'll move stuff out of the static Main, and write inside other curly brace blocks, but this will do for now.

You've forgotten to declare a variable for math - I'll leave that as an exercise for you to sort out. Also, when variables are declared inside a method (Main is a method) you should name them using camelCase rather than PascalCase. It might not seem important now, but it's convention and following it helps later on when code gets more complex. PascalCase is typically used for public methods, properties, casses and namespaes, and camel for private or local.

In short, your variables should be called english, science, and averageCalc

Foursome answered 17/10, 2021 at 5:50 Comment(2)
Thanks, although I want a full explanation as to why it displayed the error messageDunigan
Edited into the start of the answerFoursome
C
0

You need not declare Program Class and Main method again by default in the latest versions you are already inside Main method , Below will work

int English;
int Science;
int Maths;
int AverageCalc;

//AverageCalc = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Write your math grades");

Maths = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Write your english grades");

English = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Write your science grades");

Science = Convert.ToInt32(Console.ReadLine());

AverageCalc = (Maths + English + Science / 3);

if (AverageCalc > 80)
{
    Console.WriteLine("You passed with A mark!");
}
else if (AverageCalc < 80)
{
    Console.WriteLine("You passed with B mark!");
}
else if (AverageCalc < 65)
{
    Console.WriteLine("You passed with C mark!");
}
else if (AverageCalc < 60)
{
    Console.WriteLine("You passed with D mark!");
}
else if (AverageCalc < 55)
{
    Console.WriteLine("You got lower than D mark, try better next time.");
}

Console.ReadLine();
Cholecystitis answered 26/12, 2022 at 14:49 Comment(1)
This is only available after .NET 6 which includes C# version 10.0: learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/…Unrounded

© 2022 - 2024 — McMap. All rights reserved.