Stupid bug i don't know how to fix

Shrek Has Swag

New member
Joined
May 22, 2017
Messages
4
Location
The Netherlands
Programming Experience
Beginner
Hey guys, this is my first post so dont go hard on me. I'm a complete rookie and i am one of the worst programmers you'll ever see.

So my code wont start, it doesn't do anything until you quit, it then prints out the first line of code.

FYI: I code on Mac and Windows, this was written on Mac and works there but doesn't on Windows. The code is in Dutch.

This is the code:
C#:
using System;


namespace TestApp
{
	class MainClass
	{
		public static void Main(string[] args)
		{
			 
			// Hier vraagt vanaf en tot welke getallen hij moet gebruiken.
			
            Console.WriteLine("Vanaf welke nummers wil je oefenen?");
            int Vrgnum1 = Convert.ToInt32 (Console.ReadLine());


            Console.WriteLine("Tot welke nummers?");
            int Vrgnum2 = Convert.ToInt32 (Console.ReadLine());
            Random rnd = new Random();
Start:
			int num01 = rnd.Next(Vrgnum1, Vrgnum2);
			int num02 = rnd.Next(Vrgnum1, Vrgnum2);
			            
            // De rekenmachine dingen
			Console.WriteLine("Hoeveel is " + num01 + " keer " + num02 + "?");
			int antwoord = Convert.ToInt32 (Console.ReadLine());


			if (antwoord == num01 * num02)
			{
				int reactie = rnd.Next(1, 4);


				switch (reactie)
				{
					case 1:
						Console.WriteLine("Goed zo, dat klopt!");
						break;


					case 2:
						Console.WriteLine("Dat is waar, goed gegokt ;)");
						break;


					case 3:
						Console.WriteLine("Goed gedaan!");
						break;


					case 4:
						Console.WriteLine("Lekker bezig!");
						break;
				}
			}
			else
			{
				// Hij reageert gebaseerd op hoe dichtbij je was.
				int diff = Math.Abs(antwoord - (num01 * num02));


				if (diff == 1)
				{
					Console.WriteLine("Bijna!");
				}
				else if (diff <= 10)
				{
					Console.WriteLine("Je kan het wel, nog even oefenen!");
				}
				else {
					Console.WriteLine("Je komt niet eens dichtbij...");
				}


			}
			Console.WriteLine();
			Console.WriteLine("Wil je nog een som doen?");
			Console.ReadKey();
			Console.Clear();
Goto Start;
		}	
	}
}
 
Hopefully it doesn't actually print out any lines of code. Can you be a bit more specific about what you actually do? When you say "quit", do you mean stopping the debugging session in VS or something else? When you say "prints out the first line of code", do you actually mean that it displays "Vanaf welke nummers wil je oefenen?" in the console window? Have you actually debugged your code, i.e. set a breakpoint and stepped through it line by line? If the code doesn't even execute then it's not a bug in your code but possibly a corrupt project or the like.
 
Two glaring thing I see with this code:
  1. There's no exit, when it reaches the bottom it simply returns to "start"
  2. You're using a GoTo statement, instead of using a While loop where you can check a variable to know if to exit the program or not
 
Alright. I you guys dont know what the intention of the program is.

With this program you can practice your maths. It asks for the range of numbers you want, it does that in the lines 12 to 16. It then creates new random numbers with that range you declared. It doesn't really need an exit because you're running the program again and again, so if it asks if you'd want to quit every time you answered a problem, it would be wayy too annoying.

Hopefully it doesn't actually print out any lines of code.

After you hard stop the program it prints: "Vanaf welke nummers wil je oefenen?".


Two glaring thing I see with this code:

  1. There's no exit, when it reaches the bottom it simply returns to "start"
  2. You're using a GoTo statement, instead of using a While loop where you can check a variable to know if to exit the program or not

1. No exit needed really.
2. GoTo is the way to go here. I also don't really know how to implement a while loop in this code.


Edit: After some testing around i figured out it does run on mono but not on .NET
 
What version of VS are you using? VS2015?

While looking at the code, I don't see how it wouldn't run on Windows either, so I plan to copy and paste it into a project on my end and see if I can't get it to run, but I also plan to put in a While loop (GoTo is NEVER a good option) that will accept a value like "x" to exit the loop and gracefully end the program.
 
This is what I came up with (VS2015 project attached):
using System;

namespace MathGame {
    public class Program {
        public static int Main(string[] args) {
            bool ExitLoop = false;
            int Vrgnum1 = 0;
            int Vrgnum2 = 0;
            int num01 = 0;
            int num02 = 0;
            int antwoord = 0;
            var rnd = new Random();

            Console.WriteLine("Eerste getal: ");
            if (int.TryParse(Console.ReadLine(), out Vrgnum1)) {
                Console.WriteLine("Tweede getal: ");
                if (int.TryParse(Console.ReadLine(), out Vrgnum2)) {
                    if (Vrgnum1 < Vrgnum2) {
                        while (!ExitLoop) {
                            num01 = rnd.Next(Vrgnum1, Vrgnum2 + 1);
                            num02 = rnd.Next(Vrgnum1, Vrgnum2 + 1);

                            // De rekenmachine dingen
                            Console.WriteLine($"Hoeveel is {num01} keer {num02}?");
                            antwoord = AnswerInput(Console.ReadLine(), ref ExitLoop);

                            if (!ExitLoop) {
                                if (antwoord == num01 * num02) {
                                    int reactie = rnd.Next(1, 4);

                                    switch (reactie) {
                                        case 1:
                                            Console.WriteLine("Goed zo, dat klopt!");
                                            break;
                                        case 2:
                                            Console.WriteLine("Dat is waar, goed gegokt ;)");
                                            break;
                                        case 3:
                                            Console.WriteLine("Goed gedaan!");
                                            break;
                                        default:
                                            Console.WriteLine("Lekker bezig!");
                                            break;
                                    }
                                } else {
                                    // Hij reageert gebaseerd op hoe dichtbij je was.
                                    int diff = Math.Abs(antwoord - (num01 * num02));
                                    if (diff == 1) {
                                        Console.WriteLine("Bijna!");
                                    } else if (diff <= 10) {
                                        Console.WriteLine("Je kan het wel, nog even oefenen!");
                                    } else {
                                        Console.WriteLine("Je komt niet eens dichtbij...");
                                    }
                                }
                                Console.WriteLine();
                            }
                        }
                    } else
                        Console.WriteLine("Getal 1 is niet minder dan getal 2");
                } else
                    Console.WriteLine("Ongeldige invoer");
            } else
                Console.WriteLine("Ongeldige invoer");

            return 0;
        }

        private static int AnswerInput(string input, ref bool ExitLoop) {
            int val = 0;
            input = input.Trim();

            if (input.Length > 0) {
                if (!int.TryParse(input, out val)) {
                    val = 0;
                    ExitLoop = (input.ToLower()[0] == 'x');
                }
            }

            return val;
        }
    }
}

First what it does is checks the initial two input numbers for the high and low values, then makes sure number1 is less than number2, if anything is wrong a message is written to the console, pardon my Dutch as I'm not very good with that language.
If everything checks out the main loop starts (note: no use of Goto), then inside the loop it's back to your code with the exception of checking whether the user types an "x", if they do the math check is ignored, the loop exits, and the program ends. Otherwise it checks for a number and then your code of checking the number for the correct answer runs.
 

Attachments

  • MathGame.zip
    9.8 KB · Views: 40
Alright, after some fiddeling around i came up with this:

using System;


namespace TestApp
{
    class MainClass
    {
        public static void Main(string[] args)
        {
             
         bool ExitLoop = false;
            int Vrgnum1 = 0;
            int Vrgnum2 = 0;
            int num01 = 0;
            int num02 = 0;
            int antwoord = 0;
            var rnd = new Random();
 
            Console.WriteLine("Eerste getal: ");
            if (int.TryParse(Console.ReadLine(), out Vrgnum1)) {
                Console.WriteLine("Tweede getal: ");
                if (int.TryParse(Console.ReadLine(), out Vrgnum2)) {
                    if (Vrgnum1 < Vrgnum2) {
                        while (!ExitLoop) {
                            num01 = rnd.Next(Vrgnum1, Vrgnum2 + 1);
                            num02 = rnd.Next(Vrgnum1, Vrgnum2 + 1);
 
                            // De rekenmachine dingen
                            Console.WriteLine("Hoeveel is" + num01 + "keer" + num02 "?");
                            antwoord = AnswerInput(Console.ReadLine(), ref ExitLoop);
 
                            if (!ExitLoop) {
                                if (antwoord == num01 * num02) {
                                    int reactie = rnd.Next(1, 4);
 
                                    switch (reactie) {
                                        case 1:
                                            Console.WriteLine("Goed zo, dat klopt!");
                                            break;
                                        case 2:
                                            Console.WriteLine("Dat is waar, goed gegokt ;)");
                                            break;
                                        case 3:
                                            Console.WriteLine("Goed gedaan!");
                                            break;
                                        default:
                                            Console.WriteLine("Lekker bezig!");
                                            break;
                                    }
                                } else {
                                    // Hij reageert gebaseerd op hoe dichtbij je was.
                                    int diff = Math.Abs(antwoord - (num01 * num02));
                                    if (diff == 1) {
                                        Console.WriteLine("Bijna!");
                                    } else if (diff <= 10) {
                                        Console.WriteLine("Je kan het wel, nog even oefenen!");
                                    } else {
                                        Console.WriteLine("Je komt niet eens dichtbij...");
                                    }
                                }
                                Console.WriteLine();
                            }
                        }
                    } else
                        Console.WriteLine("Getal 1 is niet minder dan getal 2.");
                } else
                    Console.WriteLine("Ongeldige invoer");
            } else
                Console.WriteLine("Ongeldige invoer");
 
            return;
        }
 
        private static int AnswerInput(string input, ref bool ExitLoop) {
            int val = 0;
            input = input.Trim();
 
            if (input.Length > 0) {
                if (!int.TryParse(input, out val)) {
                    val = 0;
                    ExitLoop = (input.ToLower()[0] == 'x');
                }
            }
 
            return val;
        }    
    }
}


I had to change the return at line 66 from: "return 0;" to "return;" as it wouldn't run.

I use Visual Studio Code on Windows, Xamarin Studio on Mac.

Also, did you do the Dutch by yourself or with translate? It's pretty impressive.
 
Last edited:
Alright, after some fiddeling around i came up with this:

I had to change the return at line 66 from: "return 0;" to "return;" as it wouldn't run.

I use Visual Studio Code on Windows, Xamarin Studio on Mac.

Also, did you do the Dutch by yourself or with translate? It's pretty impressive.
That change was needed because your line 8 is a void Main instead of int Main like mine, both work fine in this case.
I started with Google Translate but ran it by my Dutch friend and he corrected, Google likes to use the word "nummer" instead of the more appropriate "getal".
 
Back
Top Bottom