Guessing game random number to static - visual-studio-2012

I have created a guessing game that generates a random number, takes the user's guess, and basically outputs whether the user's guess is too high or too low(outputs blue, colder) or closer (warmer, outputs red) using both a text box background color and label, as you can see in my code. The issue I am having difficulties with is that every time I click submit, the program generates a new random number. I would like for the program to use the same number until the user's guess is correct, then it can generate a new number if the user would like to play again.
I'm thinking about using a while loop, such as
How could I possible make the random number stay static (same) until it's guessed correctly and if I do need a while loop, where would be the ideal place to place it?

Assign the generated random number to a variable, and then use that variable until you need a new random number.
In this particular instance, the line of code number = rand() % 1000 + 1; needs to be outside of your button click method. Otherwise, every time you click the button, it will generate a new random number.

What you want to do is move your number variable's declaration to the top of the class, outside any functions, like so:
class NumberGuessingGame
{
public: int number = 0;
}
Then, in your MyForm_Load method, you can generate the random number, and set it's value to that variable, like:
private: System::Void MyForm_Load()
{
//Set the value of number here to a newly generated random integer
}
You should then be able to access the number variable inside your button1_Click function:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if (input > this->number)
{
//code
}
//Rest of your ifs
}

Related

Why CreateSolidBrush returns random number instead of constant number in c++?

Here is my code :
int clrresult = (int)CreateSolidBrush(RGB(20, 30, 40));
std::wstringstream wss;
wss << clrresult;
Edit_SetText(CLtbx,wss.str().c_str());
every time , I clicked on button , it return rand number with 10 length like this : 1341117845
but the color of the background window is the same. why this happened ?
Why CreateSolidBrush returns random number instead of constant number ?
Thanks.
According the the MSDN documentation, CreateSolidBrush returns an HBRUSH object, which is a handle to a brush, not a meaningful value. You aren't meant to use the numerical value of it directly. It's essentially a pointer to the actual brush object which is managed by Windows.

How to use the hasNext multiple times

So I am working on a program that will use Scanner to read through a text file and count the number of words, sentences, etc. I have code that works, but only to some degree and it is confusing me greatly. I have these two classes,
public void sentences()
{
while(text.hasNext())
{
// code to check for and count sentence ends
}
}
public void words()
{
while(text.hasNext())
{
// code to count for words
}
}
The confusing (to me) part is when I run both of these methods they work. However, (the order doesn't matter) the second one will not work. So if I were to write
w.words();
w.sentences();
the sentences method will do nothing and give me a value of 0. I have done some testing with system.out.println() and the sentences method will get called, but it will skip over the loop. So I feel like there something to do with the hasNext() method. Any thoughts?
The loop in words finishes when hasNext returns false. Unless you do something before the call to sentences it will still return false there, and never enter the loop.

start app on a different layout each time?

I want the app when it is executed to start a different layout, I have this code that allows me to choose the layout where to start but want it to be random thanks.
myPager.setCurrentItem(0);
PD: excuse my English, not speaking, he used the translator
You can use Java's built-in pseudo random number generator to get a random value in a defined range. Use this to choose a random value for setCurrentItem. For example:
Random r = new Random();
int randomValue = r.nextInt( 5 );
myPager.setCurrentItem( randomValue );
The value you pass to the nextInt method is one more than the maximum integer you want to receive; the example above will return a random value between 0 and 4 (excluding 5).

Zip code size test C#

I have a small piece of code that I have been trying to get to work, but nope. So I've come to the experts. I'm trying to test a zip code entry and limit it to 5 digits only. My first issue is with the attached code. It doesn't seem to be counting the length properly. Also, how can I limit the user so if they try to type a 6th character, it won't show or be accepted?
private void textBoxZip_TextChanged(object sender, EventArgs e)
{
String userInputString;
int length,
max = 5;
userInputString = textBoxZip.Text;
numberTest(userInputString);
length = userInputString.Length;
if (length > max)
{
labelErrorMessage.Text = "Maximum length 5 numbers";
}
}
I think using a MaskedTextBox would be an easy solution. This will allow you to easily accept only a certain # of characters in a specified format.
The MaskedTextBox is a standard .NET control in the control toolbar in Visual Studio.
You should look at events KeyDown and KeyPress. See example in MSDN Control.KeyPress Event
If you are in a WinForms application, you can limit a textbox's length by setting the MaxLength property.

New programmer needs help increasing an integer. Visual C++

I'm currently stuck on making the title screen to a game I've been planning.
The form loads and TMRopI (a timer) starts ticking, Increasing the opacity of the form, creating a fade in effect, and revealing a logo on the form. every time the timer ticks, an integer increases by 1 (or at least it's supposed to). A picturebox should become visible after the integer reaches 150 (the form is at full opacity when the integer equals 100) creating a pause before the picture is changed. The only problem is, it seems to be comepleteley ignoring my integer.
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
TMRopI->Enabled=true;
}
private: System::Void TMRopI_Tick(System::Object^ sender, System::EventArgs^ e) {
int num=0;
num+=1;
this->Opacity+=0.01;
if (num >= 150)
PBXtitle->Visible=true;
}
Another option is to change the definition of num to static. However, making num a member variable of the class is the more C++ way to do it.
static int num=0;
num+=1;
this->Opacity+=0.01;
if (num >= 150)
PBXtitle->Visible=true;
This way, num gets set to 0 on the first call to the function and it's value is saved between subsequent calls.
int num=0
sets num to 0 every time, does it not?
As written, num will always be 1 in the TMRopI_Tick method. You should declare it in the calling method and pass it as a parameter by reference. Or, as #Matt points out in the comment, declare it as a member of the class.
Edit Your current declaration of num creates it as a local variable. It is "created" each time the method is called and "destroyed" as soon as the method ends. So every time the method is called it starts over at zero and then gets incremented to one. If TMRopI_Tick is called repeatedly inside a loop in another method, for example, you could define num in that calling method as a local variable. Then it would exist for the duration of that method. Then if you passed it to TMRopI_Tick (by reference), it would then be incremented and its scope (think of it as its "life") would exist throughout (and beyond) each call.

Resources