Computer Hope

Software => Computer programming => Topic started by: scoob on February 24, 2017, 12:54:34 PM

Title: Beginner who needs some help troubleshooting my c# code
Post by: scoob on February 24, 2017, 12:54:34 PM
I have been working on making a hangman game, and have run into an issue where the error counter only counts one incorrect letter. After that it will not count any other incorrect letters and stops at 1. It is supposed to count up to 7 inccorect letters, where a message telling you that you have lost should appear. (Du har tapt og klarte det ikke denne gangen.) I cannot figure out where my code is incorrect and what elements i need to change in oreder to fix this. Hoping to get some suggestions and help with what I am doing wrong :)

Thanks in advance! -Jc

[attachment deleted by admin to conserve space]
Title: Re: Beginner who needs some help troubleshooting my c# code
Post by: BC_Programmer on February 24, 2017, 01:58:56 PM
You are using the "Feil" variable to try to count  the number of failures, correct?

But it will always be at most one, because it's declared a local variable and thus won't "survive" for subsequent clicks. You check ifi the guess is wrong,. then increment feil if it is, but feil will always be zero at that point, so the increment will only ever make it 1.

Move the declaration so that it is a member variable- remove the int Feil=0 inside the button click and move it to above the click event as

Code: [Select]
private int feil=0

Then it will count to 7... And beyond, but I suppose that will be your next problem to tackle :P
Title: Re: Beginner who needs some help troubleshooting my c# code
Post by: scoob on February 24, 2017, 02:28:06 PM
Thanks, that gave me some more insight. But now that I have added that line above the click event for the button none of my labels, textboxes or image references in the code are recognized, for example ( The name txtGjett does not excist in the current context) and the button click event now shows a ( new protected member in sealed class).
Title: Re: Beginner who needs some help troubleshooting my c# code
Post by: BC_Programmer on February 24, 2017, 04:51:09 PM
Add a semicolon.