# Need help with a C# windows program

This topic is 3620 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
Your question doesn't make much sense. What do you mean that "the random number is random every guess"? A guess is a guess, and has nothing to do with randomness.

Perhaps I could infer the meaning of your question from your source code if you properly formatted it with source tags, as described in the faq.

##### Share on other sites
Sorry. I mean that for every guess the random number changes, for example I typed in 4 and it was right after like 5 tries. Here is my code properly formatted. Thanks.

[/using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Programming_Assignment_4{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {        }        private void label1_Click(object sender, EventArgs e)        {        }        private void yourguess_TextChanged(object sender, EventArgs e)        {        }        private void Guess_Click(object sender, EventArgs e)        {            Random r = new Random();            int answer = r.Next(0, 10);            int counter = 0;            int guess;            bool playing = true;            guess = int.Parse(yourguess.Text);            while (guess < 101)             {                ++counter;                if (guess < answer)                 {                                         this.BackColor = System.Drawing.Color.Pink;                    this.yourguess.Clear();                     //counter++;                     break;                 }                 else if (guess > answer)                 {                                          this.BackColor = System.Drawing.Color.Blue;                     this.yourguess.Clear();                     //counter++;                     break;                 }                 else if (guess == answer)                 {                     string name = ("Congradulations.  You guessed " +  answer + "in " + counter + " tries");                    MessageBox.Show(name);                                        //counter++;                     this.yourguess.Clear();                     break;                 }                              }         }        private void High_Click(object sender, EventArgs e)        {        }        private void Low_Click(object sender, EventArgs e)        {        }    }}]

##### Share on other sites
Since this is homework, I can't give you a direct answer, but we can give you really good hints...

Here is where you are randomly generating an answer:
Random r = new Random();int answer = r.Next(0, 10);

That code appears to be executed every time the user clicks the button to submit a guess. In other words, it is performing exactly the way you wrote it.

Now, can you think of any ways you could rewrite your code to prevent it from executing that code and generating that random number every time the button is clicked?

MORE:

You are resetting the counter every time also. The way your program works right now, you are giving the user a single guess to get the correct answer.

##### Share on other sites
what if i put the random generator part in the Form1_Load part? would that work?

##### Share on other sites
That would help at least superficially. Have you covered classes and class members in your schoolwork? Remember that the form itself is a class. Perhaps you could give it some data members to keep up with those things.

##### Share on other sites
We have not covered that in depth. What if I put the random generator in main() or outside of the class?

##### Share on other sites
You can put it in any number of places. You just have to make sure you are storing the number, rather than regenerating it every time.

I'm a bit baffled by the fact that you were given this assignment without covering member variables and other object oriented ideas.

##### Share on other sites
so in order to store the number, i need to declare a variable that becomes the random number?

##### Share on other sites
Sorry, I've been gone for a while.

What have you tried while I was gone? Did you try your idea of creating a variable to hold on to the random number?

##### Share on other sites

Long story short... you can add data members to your class that are visible inside of your class.

public class TestClass{  private int x;  public TestClass()  {    x = 0;  }  public void IncrementX()  {    x++;  }  public void PrintX()  {    System.Console.Writeline(x);  }}

##### Share on other sites
This is what I did, but it still doesn't work right.

[/using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Programming_Assignment_4{    public partial class Form1 : Form    {        //Random r = new Random();        //int answer = r.Next(0, 10);        int count = 0;                public Form1()        {            InitializeComponent();                    }        public int GetRandomNumber()        {            Random r = new Random();            return r.Next(10);               }        private void Form1_Load(object sender, EventArgs e)        {        }        private void label1_Click(object sender, EventArgs e)        {        }        private void yourguess_TextChanged(object sender, EventArgs e)        {        }                        private void Guess_Click(object sender, EventArgs e)        {            //Random r = new Random();            //int answer = r.Next(0, 10);            //int counter = 0;            int guess;            //int answer = RandomNumber(0, 10);            //int answer = r.Next(0, 10);            //guess = int.Parse(yourguess.Text);            guess = Convert.ToInt32(yourguess.Text);            int answer = GetRandomNumber();             while (guess != answer){if (guess < answer){    this.BackColor = System.Drawing.Color.Pink;    this.yourguess.Clear();    count++;     break; } else if (guess > answer){    this.BackColor = System.Drawing.Color.Blue;     this.yourguess.Clear();    count++;    break; } else if (guess == answer){    this.yourguess.Clear();    MessageBox.Show("The number of guesses are " + count);break;}count++;} ]I know it's something stupid that I am not doing properly because it always is.

##### Share on other sites
Try looking back over your code for the button click. Think about what happens every time the user hits the button. Look at the GetRandomNumber method, the count is not the only variable you need in the class.

##### Share on other sites
Is it restarting the code every time the buttonclick is used? Do i need to put my code in a different place, and have button click = wherever i put the code in?

Anybody?

##### Share on other sites
Its been a while since I used C#. But I believe every time you call new Random() you are resetting your random number generator. You need to put that somewhere else, or set it up in a way that it is only seeded on the first call to the function.

static int seeded = 0;if(seeded == 0){	r = new Random();	seeded++;}

Would be one of many ways to do this.

##### Share on other sites
Is this the correct way? It still is doing the same thing as before.

[/using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Program4{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void guessbutton_Click(object sender, EventArgs e)        {            int count = 0;            int seeded = 0;            string inValue;            int guess;            inValue = textBox1.Text;            guess = int.Parse(inValue);            //Random r = new Random();            while (seeded == 0)            {                Random r = new Random();                int answer = r.Next(0, 10);                seeded++;                                if (guess < answer)                {                    this.BackColor = System.Drawing.Color.Pink;                    this.textBox1.Clear();                    count++;                    break;                }                else if (guess > answer)                {                    this.BackColor = System.Drawing.Color.Blue;                    this.textBox1.Clear();                    count++;                    break;                }                else if (guess == answer)                {                    this.textBox1.Clear();                    MessageBox.Show("The number of guesses are " + count);                    break;                }                count++;            }         }        private void textBox1_TextChanged(object sender, EventArgs e)        {        }        private void yourguess_Click(object sender, EventArgs e)        {        }    }}]

##### Share on other sites
I thought I was very clear about this yesterday. You are generating a new random number every time the "guess" button is clicked. If you don't want a new random number every time the "guess" button is clicked, quit generating a random number every time the "guess" button is clicked.

I honestly don't know how much more clearly I can explain that, without outright giving you the answer. I already even gave you code showing how to add variables to your class.

At this point, there is no excuse for you not to be able to answer this. I'm just being honest... if you were really trying and if you belonged in the class you are in, you should have the hints you need.

I will still try to help you, though. But you're going to have to show some willingness to try to understand the problem. Why are you even bothering asking if what you are doing is right when it obviously isn't? If you are making up a new random number every time someone guesses, then you are clearly doing it wrong.

Work from these facts:

1) You do not want to generate a number when someone guesses. If you do, then you are changing the number before they get a chance to guess again.

2) The number should already be determined before they even guess. The fact that they are guessing the number implies that the number exists, so it should already exist when they guess for the first time.

##### Share on other sites
Oh god, I am an idiot. lol. Smitty is right.

You want to generate the answer before you try to guess the number.

Right now think of it this way, you load up ur program and it asks you to guess the number, but the program doesn't even decide what the number is until after you have made your guess.

That generally wouldn't make much sense in a real situation, and in the same sense it shouldn't in your computer program. The answer should be generated long before a guess is ever made.

##### Share on other sites
Ok, I've been reading up quite a bit online, and I'm PRETTY sure I have it. The only problem is that it freezes after a few guesses every now and then. But the guesses I put in are working and I do not think the random number is being generated every time the "guess" button is clicked. Here is the code I'm working with here.

[/using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Program4{    public partial class Form1 : Form    {                Random r = new Random();        int answer;                                            public Form1()        {            InitializeComponent();        }                private void guessbutton_Click(object sender, EventArgs e)        {            int count = 0;            answer = r.Next(0, 10);            int guess;            guess = Convert.ToInt32(textBox1.Text);            //int count = 0;            //int seeded = 0;            //string inValue;            //int guess;            //inValue = textBox1.Text;            //guess = int.Parse(inValue);            ////Random r = new Random();            //while (seeded == 0)            //{            //    Random r = new Random();            //    int answer = r.Next(0, 10);            //    seeded++;                            //    if (guess < answer)            //    {            //        this.BackColor = System.Drawing.Color.Pink;            //        this.textBox1.Clear();            //        count++;            //        break;            //    }            //    else if (guess > answer)            //    {            //        this.BackColor = System.Drawing.Color.Blue;            //        this.textBox1.Clear();            //        count++;            //        break;            //    }            //    else if (guess == answer)            //    {            //        this.textBox1.Clear();            //        MessageBox.Show("The number of guesses are " + count);            //        break;            //    }            //    count++;            //}             do            {                count += 1;                //int guess;                //guess = Convert.ToInt32(textBox1);                if (guess != answer)                    if (guess < answer)                    {                        this.BackColor = System.Drawing.Color.Pink;                        this.textBox1.Clear();                        break;                    }                    else                    {                        this.BackColor = System.Drawing.Color.Blue;                    }                else                {                                        MessageBox.Show("The number of guesses are" + count);                }            }            while (guess != answer);                        }        private void textBox1_TextChanged(object sender, EventArgs e)        {                                           }        private void yourguess_Click(object sender, EventArgs e)        {        }    }}]

##### Share on other sites
The only code that I am still questioning is

What exactly does that do? If it selects a number randomly between 0 and 10 and it is in your Guess function then you are still picking a random number in your guess function. That probably needs to be moved out.

Edit: Also, is your program counting the guesses right? In C it destroys variables that are only in the scope of the function, which your count variable is. But C# might account for that for whatever reason. You can move it out of the function or at least in C put static before it.

##### Share on other sites
I thought in order to set a random number between, in this case, 0 and 10, i needed to use that part of the code?

##### Share on other sites
Let me see if I am understanding this properly. I need to put the
answer = r.Next(0,10) outside of the buttonclick method? Like right under where i have the Random r = new random() ?