C# random number 1-100, guessing game

Status
Not open for further replies.

ttbspyder

Estimable
Oct 15, 2014
4
0
4,510
I have this code....but generating the random number is not in the right spot, can someone tell me where it goes...this runs with no errors, just doesnt run correctly

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RandomNumberGuessingGame
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("Welcome to the Random Number Guessing Game");
}

private void guessButton_Click(object sender, EventArgs e)
{
int number;//declare int for random number
Random rand = new Random(); //create object
number = rand.Next(101); //set int number to random number from 1-100

//user has entered guess

int guess;
guess = int.Parse(usersGuessTextBox.Text);

if (guess == number)
{
MessageBox.Show("Congratulations, you guessed it!");
}
else
{
if (guess < number) { MessageBox.Show("Too low, try again!"); }
else { MessageBox.Show("Too high, try again!"); }
}
}

private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}




}
}
 

ex_bubblehead

Distinguished
Moderator
Homework assignment perhaps?

What is the expected output?
What is the actual output?
What have you tried?
What happens when you single step through the code with a watch on all variables?

Just posting a code snippet and asking what's wrong with it is not how you learn. We are not here to do your homework for you.
 

ttbspyder

Estimable
Oct 15, 2014
4
0
4,510
When I build and run, I enter a 12 in, thinking that the number has already been generated, however when I hit the submit guess button, the form generates a new random number therefore giving false output message. And yes this is a homework assignment, but when you ask your instructor and he says, "just figure it out, you will get it" is not a learning either, I am not asking you to DO the hwk, I am simply asking for tips and suggestions, I have done most of it. Thank you! and yes I believe you are right, popatim, my bad
 

ttbspyder

Estimable
Oct 15, 2014
4
0
4,510
Thats one problem I am having, I have tried to move it into different sections of the code, but then my int number doesnt register inside the button click event in my if else statements, I am having trouble figuring out where it should go in order to work properly
 
Status
Not open for further replies.