PDA

View Full Version : Solved: Visual C++ Question: Math with Variables



CreganTur
11-19-2008, 12:54 PM
There were some discussions a while ago about the possability of adding a C/C++ section to the forum... I don't know if that will happen, but I figured I'd go ahead and offer a seed post:)

I'm just starting to learn Visual C++ by working through the book Microsoft Visual C++ Express for the Absolute Beginner (http://www.amazon.com/Microsoft-Express-Programming-Absolute-Beginner/dp/159200816X/ref=sr_1_8?ie=UTF8&s=books&qid=1227123985&sr=1-8). Good book so far, only gone through the first 2 chapters but I've learned a lot about the IDE.

Anyway, I'm building an improved version of Chapter 2's project (every chapter ends with some challenges to work out on your own). I've got a textbox on my Form that displays a seconds count. Currently it counts from 1 to 30 seconds. It is fed by a Timer object, which is set with an interval of 1000 miliseconds.

What I want to do is flip this around into a countdown timer, so it runs from 30 to 0. The timer's counter intervals are held in the variable intTimerCounter (code to follow). If this was VBA I would just set the text of the textbox to be "30 - intTimerCounter", but that doesn't work in C++; it gave me an error of some sort...

Here's my current code:

private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
//update the timer
intTimerCounter++;
textBox2->Text = intTimerCounter.ToString();
if( intTimerCounter == 30 )
{
//if the timer reaches 30 disable the buttons
button1->Enabled = false;
button2->Enabled = false;
timer1->Enabled = false;
}
}


The code above is for the Timer object. It increments the intTimerCounter variable until it reaches 30, at which point it disables 2 buttons on the Form and then disables the timer. It also changes the Text of textBox2 to show the current value of intTimerCounter.

Anyone have any ideas on what I can do to change this to a countdown timer?

Tommy
11-19-2008, 02:13 PM
Randy it has been a long time since I coded in C/C++. With that said try this:


private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
//update the timer
intTimerCounter--; /* You will need to set this var to 30 of course before using this function */
textBox2->Text = intTimerCounter.ToString();
if( intTimerCounter == 0 )
{
//if the timer reaches 30 disable the buttons
button1->Enabled = false;
button2->Enabled = false;
timer1->Enabled = false;
}
}



btw that is my best shot so if it doesn't work you are own your own LOL

CreganTur
11-19-2008, 02:49 PM
That fixed it, thanks!

CreganTur
11-20-2008, 01:49 PM
In the next chapter of this C++ book they showed a way to perform calculations using temporary variables. This is more along the lines of what I was looking for- it allows the original intTimerCounter variable to count up from 0 to 30, but I can use the temp var. to count the textbox down:

private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
//update the timer
intTimerCounter++;
Int16 intCountDown = 30 - intTimerCounter;
textBox2->Text = intCountDown.ToString();
if( intTimerCounter == 30 )
{
//if the timer reaches 30 disable the buttons
button1->Enabled = false;
button2->Enabled = false;
timer1->Enabled = false;
}
}

malik641
12-05-2008, 04:12 PM
I'm glad to see some C++ on this forum :) it's bring some change into the mix.

One point I'd like to make that I think is good programming practice with loops and a control counter. When checking variables, don't use ==, but rather >= or <=. The reason is because it may be possible that your program could skip over that "exact" value and your code would loop forever. Using the >= and <= will always help catch those issues.

So to modify the code a bit:
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
//update the timer
intTimerCounter++;
Int16 intCountDown = 30 - intTimerCounter;
textBox2->Text = intCountDown.ToString();
if( intTimerCounter >= 30 )
{
//if the timer reaches 30 disable the buttons
button1->Enabled = false;
button2->Enabled = false;
timer1->Enabled = false;
}
}

My professor taught it to me and said that Brian Kernighan preaches this (well known for C accomplishments).

CreganTur
12-08-2008, 07:45 PM
One point I'd like to make that I think is good programming practice with loops and a control counter. When checking variables, don't use ==, but rather >= or <=. The reason is because it may be possible that your program could skip over that "exact" value and your code would loop forever. Using the >= and <= will always help catch those issues.


I've seen this in VBA before, but when I'm controlling the counter through a simple increment can the issue still occur? I would be VERY surprised if it did.

malik641
12-08-2008, 08:10 PM
I doubt it would, but the real question is "do you want that to happen?" Obviously nobody wants to have an infinite loop they didn't mean to, and that's why I suggest using the other operators. Just to be safe.

And for someone like Brian Kernighan to suggest it, I think that it could happen, though very unlikely.