Hottest Forum Q&A on CodeGuru - December 8th


Lots of hot topics are covered in the Discussion Forums on CodeGuru. If you missed the forums this week, you missed some interesting ways to solve a problem. Some of the hot topics this week include:

Is it possible to compare two numbers without using < and >? (top)


PiyeNewe want to know whether it is possible to compare two numbers without using < and >.

Hi Gurus,
Its somewhat frustrating question.
Is there any way to compare two numbers without using any
conditional operator like ( <,> )?

What do you think? Is there such a possiblity? But, first of all why, should somebody want to do that? Well, PiyeNewe's reason is understandable. He wants to write an assembly language code for one specific microprocessor that doesn't contain any CMP ('compare' ), JL ('jump if less'), or JG (jump if greater) instructions. So, back to the question: Is it possible?

Well, it is if you have a carry flag in a FLAGS register. You can subtract the two numbers and see whether a borrow was needed. Souldog did a search on the Internet and found the following information.

2.2.1 Comparisons
Control structures decide what to do, based on comparisons of data. In assembly, the result of a comparison is stored in the FLAGS register to be used later. The 80x86 provides the CMP instruction to perform comparisons. The FLAGS register is set based on the difference of the two operands of the CMP instruction. The operands are subtracted and the FLAGS are set based on the result, but the result is not stored anywhere. If you need the result, use the SUB instead of the CMP instruction. For unsigned integers, there are two flags (bits in the FLAGS register) that are important: the zero (ZF) and carry (CF) flags. The zero flag is set (1) if the resulting difference would be zero. The carry flag is used as a borrow flag for subtraction. Consider a comparison like:

  • cmp
  • vleft
  • vright

The difference of vleft - vright is computed and the flags are set accordingly. If the difference of the of CMP is zero, vleft = vright, ZF is set (i.e. 1) and the CF is unset (i.e. 0). If vleft > vright, ZF is unset and CF is unset (no borrow). If vleft < vright, ZF is unset and CF is set (borrow).

For signed integers, there are three flags that are important: the zero (ZF) flag, the overflow (OF) flag, and the sign (SF) flag. The overflow flag is set if the result of an operation overflows (or underflows). The sign flag is set if the result of an operation is negative. If vleft = vright, ZF is set (just as for unsigned integers). If vleft > vright, ZF is unset and SF = OF. If vleft < vright, ZF is unset and SF 6= OF. Do not forget that other instructions also can change the FLAGS register, not just CMP.

How can I access the string vector that holds numeric data? (top)


Mike Pliam wrote a little class which works perfect except one problem.

I wrote a little C++ class to implement the parsing of text
into matrices. I works pretty well except that I can't seem to
access the string vector that holds the numeric data as strings.
It doesn't make sense to me because I can demonstrate that in at
least one function of the class, the string vector is properly
initialized. But, when I try to acces it in another class function
(the retrieval function), the vector has size = 0.
Here's some pseudo-code to further explain my dilemma:
// lex.h
#include <string>
#include <vector>
using namespace std;

class CLex
  CLex(){ m_vs.resize(0);}
  virtual ~CLex(){}
  vector<string> m_vs;
  void parse(string s) 
  {    // ...
    m_vs.push_back("numeric string...");
  vector<string> get_svec() { return m_vs; }
The problem here is that the member vector, m_vs gets properly 
initialized in the CLex:Parse function, but when I try to use
CLex::get_svec() to access the data, it comes up empty. Checking
the value(s) of m_vs in the function get_svec, m_vs is empty.
So, what happened to it?
#include "lex.h"
int main()
  string s;
  vector<string> vs;
  CLex lx;

  s = "1 2 3\r\n4 5 6\r\n";
  vs = lx.get_svec() ;
  cout << vs[0] << endl;
The class member vector, m_vs, will have values of "1", "2", "3",
"4", "5", "6" within the CLex:Parse() function, but will be empty
(have zero size) in the CLex::get_svec() function. Can anyone
show me what I am doing wrong here? I'm sure it's something
fairly simple and stupid as usual.

Do you know where the error is? Well, the error is that he adds the string "numeric string" in the vector and not the string "1 2 3\r\n4 5 6\r\n". So changing the line from:

m_vs.push_back("numeric string...");



should solve the problem.

Is it possible to remove the Close button from the top right corner of the Dialog? (top)


danslavik needs to remove the X button in the top right corner of a dialog. But he does not know how. Do you know?

I'm trying to remove the close button from the top right of my
dialog with no luck. I want to keep the icon on the top left of the
window and I don't want to just disable the Close button; I need
to remove it. Is there any way of doing this? I can't find a
solution anywhere.

There are several solutions to disable the Close button, but this will only disable the button and not remove it. If you want to remove the button, the only solution is to handle the WM_NCPAINT message and paint the caption yourself, including the sysmenu icon, but don't paint the Close button.

How do I receive a WM_KEYDOWN message in a complicated dialog? (top)


andyinlondon is working on a dialog box in which he has several controls. Here he wants to deal with ON_WM_KEYDOWN, but didn't know how. Here is the exact problem:

My dialog box has many different controls—buttons, list-boxes,
radios etc. (but no edit controls). It is designed to be part of
a hand-held device simulator (it looks like a calculator). I have
added ON_WM_KEYDOWN to my dialog's message map but all key
messages are intercepted by the controls long before my dialog
can deal with them. Other than sub-classing all of these different
control classes and then adding ON_WM_KEYDOWN to each of their
seperate message maps, is there a better way of capturing key
messages? In other words, how can I make it so that when the '6'
key on my PC is pressed, my OnKeyDown method can deal with it,
and when it is released, my OnKeyUp method is informed?

The solution is to build an accelerator table for each button, setting the ID of the accelerator the same as the button ID. For example, for button 6 IDC_6, set ID_6 for key 6, and so forth. Then, call the function LoadAccelerators in the OnInitDialg function, and, at last, Override PreTranslateMessage by adding the line:

if(TranslateAccelerator(m_hWnd, m_hAcc, pMsg)){ 
    return TRUE;
    // m_hAcc is a member variable holding handle to loaded
    // accelerator table.

What is the equivalent of AfxGetApp to the Application Document? (top)


vcstarter is working on an SDI document with several dialogs. Here is his problem:

I have an SDI document that has several dialogs. I declare a member
variable from my app.h. I access this member from any dialog of the
application using AfxGetApp.

What I want to do, I want to declare a member variable on my doc.h
where I want to access that member from other dialog. Because
AfxGetApp returns a pointer to the application class and I am
working in the document class, I wonder if I can use
GetDocTemplate to access the member of the document class from
any other dialog as I did from the application class.

First of all, you might take a look at the following table: Get any object from anywhere FAQ.

vcstarter played a little bit with the code and found the following solution:

  1. Add a static member function to the document header file on the form of:
    class CMyDocumentClass: public CDocument
          static CMyDocumentClass *GetDoc();
  2. Implement the following function in doc.cpp
    CMydocumentClass *CMyDocumentClass::GetDoc()
       CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);
          return (CMyDocumentClass *) pFrame->GetActiveDocument();
  3. Now, access the member variable from doc.h on the form. Also, add doc.h to where you call the function below.


About the Author

Sonu Kapoor

Sonu Kapoor is an ASP.NET MVP and MCAD. He is the owner of the popular .net website DotNetSlackers publishs the latest .net news and articles - it contains forums and blogs as well. His blog can be seen at:


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Microsoft® Office 365 is a top choice for enterprises that want a cloud-based suite of productivity/ collaboration applications. With Office 365, you get access to Microsoft™ Office solutions practically anytime, anywhere, on virtually any device. It's a great option for current Microsoft users who can now build on their experience with Microsoft™ solutions while enjoying the flexibility of a cloud-based delivery. But even organizations with no previous investment in Microsoft will find that …

  • On-demand Event Event Date: July 28, 2016 Jepsen tests are third-party tests for distributed databases that validate vendors' guarantees about how they perform under various failure scenarios, especially network partitions. These have proven their value as tools in any distributed system tester's arsenal. When the creator of Jepsen, Kyle Kingsbury, started his Jepsen-for-Hire business last fall, VoltDB immediately got in line, and over the past two months, our solution was given the most stringent Jepsen …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date