Click here to Skip to main content
15,882,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Why does this code not work? It seems that something is wrong with the swtitch bool thingy because I dont anything. When I run the program (this isnt the whole program by the way please comment if you need it to solve the problem) and finish all levels including the boss level I just get the Level Menu heading but nothing else.

Thanks for your help!

BTW sorry for using gotos please dont flame me
BTW 2 I am completely new and started 2 days ago with c#.

if (num001 == answer01)
            {

                Console.Write("What is " + num19 + " plus " + num20 + "?: ");
                int answer = Convert.ToInt32(Console.ReadLine());
                if (answer == num19 + num20)
                {

                    Console.WriteLine("Well done! Your answer is correct. \nCONGRATS! You mastered the first level \nPress any key to go to the Level selection");
                    l1finished = !l1finished;
                    Console.ReadKey();
                    

                    goto LevelMenu;


                }
                else
                {

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

                }
            }


        LevelMenu:
            Console.WriteLine("                                                       Level Menu");
            for (int i = 1; i <= 27; i++)
            {
                Console.WriteLine();
            }
            int answer02 = Convert.ToInt32(Console.ReadLine());
            if (l1finished == true)
            {
                Console.WriteLine("Press 1 to enter the Main Menu. Press 2 to enter next Level");
                if (answer02 == 1)
                {
                    goto Menu; //I know gotos aren't the best. Plaese leave me!
                }
                if (answer02 == 2)
                {
                    goto Menu; //Level 2 doesn't exist yet. Please don't flame me!
                }

                else
                {
                    Console.WriteLine("            You have to finish Level 1 first! Come back again later.");
                }
            }


What I have tried:

I also tried the l1finished == true but that also seems to be not working.
Posted
Updated 12-Jan-17 3:45am
Comments
F-ES Sitecore 12-Jan-17 9:31am    
After you print the heading you do this

int answer02 = Convert.ToInt32(Console.ReadLine());

that is waiting for input so it's not going to check if l1finished until you press enter.

Quote:
BTW sorry for using gotos please dont flame me

Ha - two days and you already know everyone's biggest bug-bear ;)

The toggle here: l1finished = !l1finished; is unnecessary. Just use l1finished = true;

In the 'LevelMenu:' segment, you write out the "Level Menu" + 27 clear lines but you ask for an input before checking if the level is finished:
int answer02 = Convert.ToInt32(Console.ReadLine());

The console will wait for an input before moving any further.
 
Share this answer
 
Comments
GrabiCraft 12-Jan-17 10:07am    
Thanks. It is working now! (;
Andy Lanng 12-Jan-17 10:09am    
NP ^_^
Please take a look at Solution 3. Learning to use the debugger in invaluable!
Thanks to the goto's, it is impossible to know what your code do without the whole program, the only advice is to learn to use the debugger.

You should learn to use the debugger as soon as possible, it is an incredible learning tool. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
F. Xaver 13-Jan-17 5:21am    
5fed, coz I see no reason why this has 2 1Star votes..
Patrice T 13-Jan-17 5:28am    
Thank you.
It is a pity to see how recommending the debugger is disliked.
F. Xaver 16-Jan-17 7:20am    
debbuging means that you have to do the work yourself ;)
to read input try like this:

int op = 0;
string in = string.Empty;
do
{
  Console.WriteLine("enter choice");
  in = Console.ReadLine();
} while (!int.TryParse(in, out op));
 
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