The Student Room Group

Anyone know C# that could help?

I have this coursework that I can't wrap my head around AT ALL! Basically, I need to create a game like Hammurabi, but my code... I just don't know what's wrong with it... I mean, I know it's not working... But you know, don't know how to fix.

If anyone knows how to attempt to help me fix it, you would be a massive help!!
There's a couple of problems I can see by looking at the code:

1)
You've got a whole load of methods (functions) in your game but your 'Main()' method doesn't do anything except for write some text to the console and call 'ReadKey' - that's the reason it isn't doing anything.

Remember that 'Main' is the entry point of a C# program (i.e. the method which the program calls automatically when your program runs) -- C# only calls this method, so you'd need to call at least one of the other methods from within the body of 'Main' .

(Is StatusMessage the method you wanted to call from Main? Do you really want all those arguments/parameters?)

2)
The method called StatusMessage is never called anywhere apart from calling itself. This looks like another problem because it seems as if you've got infinite recursion here.

Recursion is a tricky kind of looping because every time you step into a nested function it creates a new "frame" on an area of memory called 'The Stack'.

Your code will keep on entering new StatusMessage methods over and over without exiting the previous ones unless you have some logic in-place to prevent your program from making these nested calls ad-infinitum. So, in your case, due to the fact that you're lacking any kind of mechanism to prevent the recursion at each stage, this will happen to your program until the program runs out of memory on 'The Stack', causing that region of memory to overflow, so you will get a type of error called a StackOverflowException.

I'd recommend avoiding Recursion since you don't really need it here -- it looks like you've already attempted to structure the program with a 'for' loop instead -- it'll be much easier for you to control what's happening by stripping out the recursion and fix your 'for' loop instead.

3)
Your program mentions user input in several places, but there's nothing in your program which actually reads any input.


Lastly-- I'd strongly recommend that you spend just a few minutes reading this and see how Debugger Breakpoints can help you see exactly what's happening in your program step-by-step: https://tutorials.visualstudio.com/vs-get-started/debugging

If you set some breakpoints (click along the left-hand column of the visual studio code editor pane to toggle big red dots) that sets an indicator to the visual studio debugger which will cause it to 'pause' your program and give you the abiity to run your program one line at a time to see the flow and observe exactly what's happening. You are probably already using the debugger (it's the green 'triangle' or 'play button' in Visual Studio - which is the same as pressing the F5 key on the keyboard or Debug->Start Debugging from the Visual Studio menu), but if you're not using that, then now is a good time to make sure you're running your program in the debugger. You can use the F10 key to step to the next line, or if the line you're on is one of your methods, then the F11 key will let you step into it.

When your program is paused on a line, there's another pane at the bottom which shows you the values of all your variables. You can also "mouse over" the variables in your code pane to see what value they're holding. This should help you see exactly what's going on in your program and troubleshoot its logic.
(edited 5 years ago)

Quick Reply

Latest

Trending

Trending