Click here to Skip to main content
15,886,003 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
So I'm relatively new to c# and tried to run a really simple code, but when I try to run it, it Shows an error message "} expected" at line 29 even though I don't see anywhere to put a curly bracket. any way I could resolve it and/or make my overall code better? thanks

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string Name;
            string Family;
            string Button;
            int Answer;
            Answer = 54;


            Console.WriteLine("what is your name?");

            Name = Console.ReadLine();

            Console.WriteLine("what is your family name?");

            Family = Console.ReadLine();

            Console.WriteLine("whats 9+45?");
            {
                if (Answer == 54);
                { Console.WriteLine("{0} {1} is Smart! :D ");
                } <===== //it asks me to put a '}' after the'}'
          
             else 
               { Console.WriteLine("{0} {1} isn't so Smart :( ");
                }
                Console.WriteLine("Press Enter to Close");

                Button = Console.ReadLine();
            }

            



        }
    }
}


What I have tried:

I tried putting it there like it asks but it puts out a bunch of errors
Posted
Updated 24-May-19 3:48am
v3

The semicolon after
if (Answer == 54)
is causing the error.
Please remove it and you can compile.
 
Share this answer
 
v4
Comments
CPallini 24-May-19 9:18am    
5.
TheRealSteveJudge 24-May-19 9:29am    
Thank you!
To add to what TheRealSteveJudge said: you are not using brackets properly anyway.
Console.WriteLine("whats 9+45?");
{
    if (Answer == 54);
    { Console.WriteLine("{0} {1} is Smart! :D ");
    } <===== //it asks me to put a '}' after the'}'

 else
   { Console.WriteLine("{0} {1} isn't so Smart :( ");
    }
    Console.WriteLine("Press Enter to Close");

    Button = Console.ReadLine();
}
You don't need the outer set brackets here - they don't serve any purpose. You only use them when you are "grouping together" statements to be executed - in a method for example, a loop, or a conditional statement. Adding them "at random" doesn;t make your code more readable!

As a beginner, use them for every conditional, and indent your code:
static void Main(string[] args)
    {
    Console.WriteLine("what is your name?");
    string Name = Console.ReadLine();
    Console.WriteLine("what is your family name?");
    string Family = Console.ReadLine();
    int Answer = 54;

    Console.WriteLine("whats 9+45?");
    if (Answer == 54)
        {
        Console.WriteLine("{0} {1} is Smart! :D ");
        }
    else
        {
        Console.WriteLine("{0} {1} isn't so Smart :( ");
        }
    Console.WriteLine("Press Enter to Close");

    string Button = Console.ReadLine();
    }
Or like this:
static void Main(string[] args)
{
    Console.WriteLine("what is your name?");
    string Name = Console.ReadLine();
    Console.WriteLine("what is your family name?");
    string Family = Console.ReadLine();
    int Answer = 54;

    Console.WriteLine("whats 9+45?");
    if (Answer == 54)
    {
        Console.WriteLine("{0} {1} is Smart! :D ");
    }
    else
    {
        Console.WriteLine("{0} {1} isn't so Smart :( ");
    }
    Console.WriteLine("Press Enter to Close");

    string Button = Console.ReadLine();
}
Note that I moved the definition of variables to where you are going to use them - that just makes it easier to see what is going on when you actually use them. With small methods like this, that doesn't make a lot of difference, but as they grow it becomes a pain to wade back through pages of code to find the definition. It also prevents you using them when they are "out of scope" and the error messages that causes.

In addition, names of local variables should start with lower case:
        static void Main(string[] args)
            {
            Console.WriteLine("what is your name?");
            string name = Console.ReadLine();
            Console.WriteLine("what is your family name?");
            string family = Console.ReadLine();
            int answer = 54;
...

And your next problem is your WriteLine statements:
    Console.WriteLine("{0} {1} is Smart! :D ");
    }
else
    {
    Console.WriteLine("{0} {1} isn't so Smart :( ");
    }
You tell the writeline to include variable values, but you don't supply the variables! Either do this:
    Console.WriteLine("{0} {1} is Smart! :D ", name, family);
    }
else
    {
    Console.WriteLine("{0} {1} isn't so Smart :( ", name, family);
    }
Or this
    Console.WriteLine($"{name} {family} is Smart! :D ");
    }
else
    {
    Console.WriteLine($"{name} {family} isn't so Smart :( ");
    }
If you are using a recent version of Visual Studio.
 
Share this answer
 
Comments
TheRealSteveJudge 24-May-19 9:42am    
These are valuable tips! 5*
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C#
using System;

namespace ConsoleApp1
{
  class Program
  {
    static void Main(string[] args)
    {
      string Name;
      string Family;
      string Button;
      int Answer;
      Answer = 54;


      Console.WriteLine("what is your name?");

      Name = Console.ReadLine();

      Console.WriteLine("what is your family name?");

      Family = Console.ReadLine();

      Console.WriteLine("whats 9+45?");
      {
        if (Answer == 54); // semicolumn here is mistake
        {
          Console.WriteLine("{0} {1} is Smart! :D ");
        } <===== //it asks me to put a '}' after the'}'

        else
        {
          Console.WriteLine("{0} {1} isn't so Smart :( ");
        }
        Console.WriteLine("Press Enter to Close");

        Button = Console.ReadLine();
      }
    }
  }
}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Indentation style - Wikipedia[^]
 
Share this answer
 
v3
Comments
TheRealSteveJudge 24-May-19 9:55am    
Also valuable information! 5*
P.S. I added the word 'Please'.
Otherwise Bill will complain and downvote ;-)
Patrice T 24-May-19 11:06am    
Tank you.

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