Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
My error code: CS1513 C# } expected. When I do this I get another bunch of errors.
So heres my code,


using System;
using System.Diagnostics;

namespace Next

{

    class MainClass

    {

        public static void Main(string[] args) // this is a method called "Main". It is called when the program starts.

        {
            Random numberGenerator = new Random();
            int num01 = numberGenerator.Next(1, 21);
            int num02 = numberGenerator.Next(1, 21);
            int num001 = 1;
            int num002 = 2;
            int num003 = 3;
            int num004 = 4;
            int num005 = 5;

            Console.WriteLine("                                                  Welcome to XMATH!");
            Console.WriteLine("------------------------------------------------------------------------------------------------------------------------");
            Console.WriteLine("                                                  Difficulty Selection \n1 \n2 \n3 \n4 \n5 \n6 \n7 \n8 \n9");
            Console.WriteLine("To START the first level press 1 \nTo ENTER a code press 2\nTo EXIT the game press 3 \nTo see the RANKLIST press 4 \nTo see the CREDITS press 5");
            Console.WriteLine();
            Console.WriteLine("------------------------------------------------------------------------------------------------------------------------");


            int answer01 = Convert.ToInt32(Console.ReadLine());
            if (num001 == answer01)
            {
                Console.WriteLine();
                Console.WriteLine("------------------------------------------------------------------------------------------------------------------------");
                Console.WriteLine();
                Console.Write("What is " + num01 + " plus " + num02 + "?: ");
                int answer = Convert.ToInt32(Console.ReadLine());
                if (answer == num01 + num02)
                {

                    Console.WriteLine("Well done! Your answer is correct.");

                }
                else
                {

                    Console.WriteLine("Are you even trying?");

                }
            }


            int answer02 = Convert.ToInt32(Console.ReadLine());
            else if (num002 == answer02)
            {
                Console.WriteLine("------------------------------------------------------------------------------------------------------------------------");
                Console.WriteLine("                                                  Still in progress. \n                                                  Please wait for updates");
                Console.WriteLine("------------------------------------------------------------------------------------------------------------------------");
            }
            Console.ReadKey();
            Console.WriteLine();
        }

    }

}


Thanks for any helping hand

What I have tried:

I already added the curly bracket but than I get another bunch of errors.
Posted
Updated 21-Nov-20 1:47am
Comments
Michael_Davies 11-Jan-17 11:01am    
You have int answer02 defined between the closing if true brackets and the else if so the else if is not tethered to the if. As you do not seem to use answer02 you do not need it put the readline into the else if.

} //Closing IF true section

int answer02 = Convert.ToInt32(Console.ReadLine());
else if (num002 == answer02)

Try
} //Closing IF true section
else if (num002 == Convert.ToInt32(Console.ReadLine()))

Also if the user types anything other than an integer your convert.ToInt32 will crash.
#realJSOP 21-Apr-20 8:24am    
Honestly, I lost interest when I saw this - "but than I get another bunch of errors." You had me right up until the last sentence.

You are declaring a variable in the middle of the if/else if structure.

int answer02 = Convert.ToInt32(Console.ReadLine()); is in the wrong spot.

C#
if (){

}
// you cannot put code here.
else if ()
{

}
 
Share this answer
 
The problem is your else if statement after your answer02 declaration. An else if statement always has to follow an if statement immediately, which is not the case here because the answer02 declaration is in between.

There are two things that you can do to fix the error, and you'll have to see which one matches the best what you want to do with your application. The first way would be to replace the "else if" with simply "if". The other way would be to put the "int answer02 = ..." statement before your first if statement. Both applications will compile, but their workings will be different. Decide which one you want.
 
Share this answer
 
You have an else if without an immediately preceding if (immediately after the int answer02 = ... declaration). Remove that else.
 
Share this answer
 
v2

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