Who Knows C++?
27th December 2009, 03:45
DY357LX
Who Knows C++?
Am currently messing around with a little program to read the current window title.
Getting the title from the Operating System proved to be more hassle than I expected but I got there in the end.
I'm now having a minor problem with my countDown integer. Here's the code:
So obviously, this program loops through the counter, decreasing 10 to 9 to 8 etc until it hits 0.
Once it hits 0 it decreases again, wrapping the unsigned short int back around to it's maximum value
of 65535. Clearly 65535 is greater than 10 so the "while" loop is broken and the rest of code (reading the
window title and then displaying it) is executed.
The display would look like this:
(It works but that 65535 is bugging me. The program only took me 20 minutes so I'll work
out the problems tomorrow when it's not nearly 4am.)
I'm going to mess around with for, else, if and while loops later but if anyone has any other idea's feel free to post em.
SIDENOTE TO DAVE: Your "CODE" tags don't appear to format the text... or do anything for that matter >_<
Getting the title from the Operating System proved to be more hassle than I expected but I got there in the end.
I'm now having a minor problem with my countDown integer. Here's the code:
Quote: |
#include <iostream> #include <windows.h> using namespace std; int main() { // Unsigned to prevent negative values. unsigned short int countDown = 10; // Renamed the default command box. SetConsoleTitle("Place another window on top of me!" ) ; cout << "Waiting 10 seconds for another window" << endl; // Give the user some time to place another window onto of the DOS window. while (countDown <= 10) { // Decrease the value of countDown and then wait a second. countDown--; // Repeat until countDown is 0. BUT, 0 - 1 in C++ is 65535! Sleep (1000); cout << countDown << endl; } // Set up a buffer to store the window title, read it then output it. char szLocalTitleBuffer[ MAX_PATH ]; GetWindowTextA(GetForegroundWindow(), szLocalTitleBuffer, MAX_PATH ); cout << szLocalTitleBuffer << endl; return 0; } |
So obviously, this program loops through the counter, decreasing 10 to 9 to 8 etc until it hits 0.
Once it hits 0 it decreases again, wrapping the unsigned short int back around to it's maximum value
of 65535. Clearly 65535 is greater than 10 so the "while" loop is broken and the rest of code (reading the
window title and then displaying it) is executed.
The display would look like this:

(It works but that 65535 is bugging me. The program only took me 20 minutes so I'll work
out the problems tomorrow when it's not nearly 4am.)
I'm going to mess around with for, else, if and while loops later but if anyone has any other idea's feel free to post em.
SIDENOTE TO DAVE: Your "CODE" tags don't appear to format the text... or do anything for that matter >_<
This post was last edited by DY357LX at 27th December 2009, 04:11. Edited 3 times in total.
"And so is the Golden City blackened With each step you take in my Hall. Marvel at perfection, for it is fleeting. You have brought Sin to Heaven And doom upon all the world."
28th December 2009, 16:14
Talarin
Who Knows C++?
You've cheated a little bit with the use of the unsigned integer - which isn't necessary because you're looking at such a small range for your number. Use a signed "short int" data type so that your numeric sequence will go 3, 2, 1, 0, -1, -2 etc.
Slightly change your while loop to something like (not sure that syntax would be right):
That should fix your issue.
Slightly change your while loop to something like (not sure that syntax would be right):
Quote: |
while (countDown <= 10 && countDown > 0)
{ // Decrease the value of countDown and then wait a second. countDown--; // Repeat until countDown is 0. BUT, 0 - 1 in C++ is 65535 because I've declared an unsigned integer! Sleep (1000); cout << countDown << endl; } |
That should fix your issue.
A census taker once tried to test me. I ate his liver with some fava beans and a nice Chianti.
28th December 2009, 18:41
DY357LX
Who Knows C++?
Forgot to update this, too busy playing Mass Effect.
(Gotta finish it before Mass Effect 2 hits!)
Cheers for the response Tal, I fixed this yesterday by doing this:
I tested the code and it counts down to zero and then displays the window title. (It doesn't loop back
around to 65535.) Also, the unsigned short int was declared because it uses less memory. (2 bytes
where-as an int will use 4 bytes. You GOTTA consider the memory footprint when you only have 4
gig of RAM
)
I had done the "&&" part you mentioned but it didn't feel right. It's probably more "right" than my method
but hey, as long as it works.
I remember reading through the chapters on loops, cases, for loops, infinite loops, if statements etc and thinking "Why the hell do I need 5/6 ways of doing the same thing!?". But I instantly remember the "for" loops
section when I needed to write that timer. And in my brain, that means I've learned something.

(Gotta finish it before Mass Effect 2 hits!)
Cheers for the response Tal, I fixed this yesterday by doing this:
Quote: |
// Enter an infinite loop for ( ; ; ) // Spaced these out to stop to the forum inserting smiley faces! { if (countDown <= 10) { cout << countDown << endl; countDown--; Sleep(1000); } else break; } |
I tested the code and it counts down to zero and then displays the window title. (It doesn't loop back
around to 65535.) Also, the unsigned short int was declared because it uses less memory. (2 bytes
where-as an int will use 4 bytes. You GOTTA consider the memory footprint when you only have 4
gig of RAM

I had done the "&&" part you mentioned but it didn't feel right. It's probably more "right" than my method
but hey, as long as it works.
I remember reading through the chapters on loops, cases, for loops, infinite loops, if statements etc and thinking "Why the hell do I need 5/6 ways of doing the same thing!?". But I instantly remember the "for" loops
section when I needed to write that timer. And in my brain, that means I've learned something.
This post was last edited by DY357LX at 28th December 2009, 18:42. Edited 1 times in total.
"And so is the Golden City blackened With each step you take in my Hall. Marvel at perfection, for it is fleeting. You have brought Sin to Heaven And doom upon all the world."
29th December 2009, 11:00
Talarin
Who Knows C++?
Quote: |
Also, the unsigned short int was declared because it uses less memory. (2 bytes
where-as an int will use 4 bytes. |
The signed short int will only use the same memory space but will have a different range:
-32767 to 32767 versus 0 to 65535
The unsigned bit is the reason why the value beneath 0 was 65535, but your range didn't need a value past 32767 so there was not any reason to use a unsigned value.
I'm not sure how you got to using a for loop there (I'm not familiar with the context of that command), but then I'm more familiar with web scripts like PHP which are only superficially similar to C.
A census taker once tried to test me. I ate his liver with some fava beans and a nice Chianti.
29th December 2009, 20:46
DY357LX
Who Knows C++?
I choose the unsigned short int simply because I didn't want any negative values.
Yes, true, it makes no difference in the long run because they both wrapped around.
(Weather to -32768 or 65535 doesn't matter... it's still an error in my eyes.)
The memory usage is the same as you stated.
I omitted certain things because it was 3:3am and I wasn't quite approaching the problem right.
(Which I did the next day with the infinite loop.)
I was essentially trying ALL possible types of loops, if statements, recursion etc that could be applied.
I wanted to compare results, errors etc.
At the end of the day, the program worked (thanks for the input!) and I've learned something
Yes, true, it makes no difference in the long run because they both wrapped around.
(Weather to -32768 or 65535 doesn't matter... it's still an error in my eyes.)
The memory usage is the same as you stated.
I omitted certain things because it was 3:3am and I wasn't quite approaching the problem right.
(Which I did the next day with the infinite loop.)
I was essentially trying ALL possible types of loops, if statements, recursion etc that could be applied.
I wanted to compare results, errors etc.
At the end of the day, the program worked (thanks for the input!) and I've learned something

"And so is the Golden City blackened With each step you take in my Hall. Marvel at perfection, for it is fleeting. You have brought Sin to Heaven And doom upon all the world."