Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please help!

I have to create a simple random number guessing game in visual studio. It takes the input from the textbox and compares to a random number between 1 and 10, counts the number of guesses and then provides output accordingly in a label. When I test the code I have I get no output like the loop is running continuously or something.

I know this is simple stuff for you guys but I am very new to coding and have to worst possible instructor ever so any help would be greatly appreciated! Thank you.

What I have tried:

C#

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class NumberGame : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnCalculate_Click(object sender, EventArgs e)
    {
        
        Random rand = new Random();
        int winNumber = rand.Next(1, 11);
        int guess;
        int numOfGuess = 0;
        bool winner = false;



        while (winner == false)
        {

            guess = Convert.ToInt32(txtGuess.Text);
            

            if (guess > winNumber)
            {
                numOfGuess++;
                lblResult.Text = "Guess is too high, try again. ";
                lblGuess.Text = "The number of guesses is " + numOfGuess;
            }
            else if (guess < winNumber)
            {
                numOfGuess++;
                lblResult.Text = "Guess is too low, try again. ";
                lblGuess.Text = "The number of guesses is " + numOfGuess;
            }
            else if (guess == winNumber)
            {
                numOfGuess++;
                lblResult.Text = "You win! Good job. ";
                lblGuess.Text = "The number of guesses is " + numOfGuess;
                winner = true;
            }

        }
      

    }
}
Posted
Updated 15-Jul-18 10:39am

1 solution

You can't do it like that: with a while loop inside your Click event handler the user cannot change the value in the textbox, so it will never do anything except process the first input over and over again.

I can't explain it to you - this is only a little text box - so you need to re-read your course notes on how to code for Windows instead of a Console app, because they are very, very different ways of working.

But basically, remove the loop completely and it will at least work slightly better!
 
Share this answer
 
Comments
Member 13912379 15-Jul-18 17:14pm    
Haha okay I see. The course notes provided just show a couple different loops. They do not show where exactly to run it and up until this point everything has been done in the click event handler. Like I was saying not the best instruction. Any advice on where to find how to make a loop that would actually work for this? Feels like I have read every forum on the web.
OriginalGriff 16-Jul-18 3:13am    
That's the whole point: you don't write loops, and especially you don't write loops which depend on user input. Windows is event driven, not procedural, and if you don;t understand what that means, then you need to go back and re-read your course notes as nothing is going to make any sense from here on!

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