• 15
• 15
• 11
• 9
• 10

# Need help with a C# windows program

This topic is 3686 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?