|
I'm coding in C++, but just using basic C++ libraries (ie. not MFC or WTL etc.).
I would like to know if there is a more elegant/better way to do what I have coded below, just to learn something.
Basically, given a "code" I want to verify that each "digit" is Octal (ie. 0-7), and that the "code" is exactly 4 characters.
I'm using this as an opportunity for a "code review" by you peers to see if I can learn something.
bool Xpdr::IsValidCode(const wchar_t* code)
{
// Has 4 numeric digits
if (wcslen(code) != 4)
{
return false; // Code MUST be 4 digits
}
for (int i=0; i<4; i++)
{
// Check each digit is octal
if (code[i] < 0x30 || code[i] > 0x37)
{
return false; // Code digits must be 0 - 7
}
}
return true;
}
Thanks for your thoughts on how to make this better / more efficient.
|
|
|
|
|
I think a possible alternative solution is:
bool Xpdr::IsValidCode(const wchar_t * code) const
{
for( int i = 0; i < 4; ++i, ++code)
{
if( *code < '0' || *code > '7')
{
return false;
}
}
return *code == 0;
}
I hope this works. In addition you can "unroll" this short loop and replace it with a series of "if"-s and autoincremented code pointer.
-- modified at 5:47 Tuesday 28th November, 2006
|
|
|
|
|
Ah, very interesting. Nice touch verifying that there is a terminating NULL.
By unroll are you meaning something like this:
bool Xpdr::IsValidCode(const wchar_t * code) const
{
if( *code < '0' || *code > '7') { return false; }
code++;
if( *code < '0' || *code > '7') { return false; }
code++;
if( *code < '0' || *code > '7') { return false; }
code++;
if( *code < '0' || *code > '7') { return false; }
return *code == 0;
}
|
|
|
|
|
Stick^ wrote: [...]By unroll are you meaning something like this[...]
Yes (actually you need one more ++ : return *++code == 0 ). Or even like this:
inline bool Xpdr::IsValidCode(const wchar_t * code) const
{
return
code[0] >= '0' && code[0] <= '7' &&
code[1] >= '0' && code[1] <= '7' &&
code[2] >= '0' && code[2] <= '7' &&
code[3] >= '0' && code[3] <= '7' &&
code[4] == 0;
}
|
|
|
|
|
Stick^ wrote: for (int i=0; i<4; i++)
{
// Check each digit is octal
if (code[i] < 0x30 || code[i] > 0x37)
{
return false; // Code digits must be 0 - 7
}
}
Have you considered wcstol(code, ..., 8) ?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
No, but I am thinking that function won't help me, because what I am trying to do is verify that input (a WCHAR*) typed by a user is in the range 0000 - 7777, with only octal (ie 0-7) digits. They could enter anything on their keyboard, so I need to validate it, not just convert it.
Thanks guys.
|
|
|
|
|
Stick^ wrote: No, but I am thinking that function won't help me, because what I am trying to do is verify that input (a WCHAR*) typed by a user is in the range 0000 - 7777...
Which is exactly what it does. If you sent it "7778", it would return an error status.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hi,
I do not know how to use the UpdateResource() function to create an ICON
resource within an .exe file. I have managed to create raw data resources
with this function, but not ICONs. Does anyone know how to make this work
given a file path to an .ico file. I need to change the icon in an .exe
file when it is displayed in explorer.
Many thanks
|
|
|
|
|
Atom wrote: I do not know how to use the UpdateResource() function to create an ICON
resource within an .exe file.
Was there any problem? Have you tried creating an RT_GROUP_ICON instead.
For more information read the article Icons in Win32 in MSDN .
|
|
|
|
|
Hi all,
I am writting a program which would like to detect if the PC is waken-up when it is in "stand-by" mode. Is there any method to do so? any reference source/material is welcome, thanks~
|
|
|
|
|
StarMeteor wrote: any reference source/material is welcome, thanks~
Look up WM_POWER , WM_POWERBROADCAST , WM_POWERCHANGE_NEEDACK in MSDN .
|
|
|
|
|
Is it possible to set hook for Joystick?
Please help!
|
|
|
|
|
Hello people
Can anybody explain why my CSocket Receive call fails when i use it with threading
With Rgds
Girish
Software Developer
|
|
|
|
|
Hi,
check error code
-----------
Mila
|
|
|
|
|
Oh interesting, someone else with CSocket and Thread probs....
Had lately also probs with CSocket and CAsyncSocket....sending works fine but no Messages were send/received so I was unable to receive .....
I went back to C based socket communication and there it works
|
|
|
|
|
If you had working code why would you use SOCKET wrapper classes?
CSocket, IMO, is only useful in a simple app.
CAsyncSocket is better (lower level) but unless you're using window messages for socket
notifications (why would you do that in a multithread app?) then you don't get much but a thin
wrapper.
Both work fine as designed and documented but they're not for everyone
Cheers!
Mark
|
|
|
|
|
Girish601 wrote: Can anybody explain why my CSocket Receive call fails when i use it with threading
Can you explain HOW the receive call fails?
|
|
|
|
|
can someone gibe me source code or algorithem for solving electronic circuits in
computer
|
|
|
|
|
echeshari wrote: can someone gibe me source code or algorithem
Click here[^]
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
|
echeshari wrote: can someone gibe me source code
another member asking for source code. i am a newbie too and i am surprised to see the requests for the source code for getting their task done. in fact, i have seen it more often these days.
Look, you will never get an help if u ask for a source code. if u need any idea on anything, then ask for the advice and then implement it on your own. and if u still find anything difficult while implementing, then u can ask for more advice. else, no one will be writing the code for u.
vijay.
|
|
|
|
|
there's a link i like to post in such case... here lol...
|
|
|
|
|
So you are saying if I ask...
"HELP PLEASE!!! Hi. I'm a newbie and I need to write a server app that supports multiple
client connections and runs on both Mac and PC. The client apps need to use Direct3D to draw
the user interface because GDI is so slow. My boss says I need this done in 3 days. What are
"window messages"? Please provide code snippet that does this NOW!"
...that I won't get an answer?
Disclaimer: The above paragraph is ficticious. Any similarity to persons real or dead was purely
intentional.
|
|
|
|
|
I tried feeding this urgent request to Google and got nearly 58.000 answers. I guess at least one of them should be usable. The answer I would like most is: get your boss sacked for hiring you.
|
|
|
|
|