Ryan Ryan - 1 year ago 134
C++ Question

Prompting a user with an input box? [C++]

My goal is to simply use a pop-up box to ask the user for an input. I've searched around quite a bit and pretty much all the results say that creating a messageBox is really easy:

MessageBox (NULL, "Hello World" , "Hello", MB_OKCANCEL);

But that creating a pop-up that takes input is more involved and there isn't a straight forward way to do it. All of the results I could find on Google were dated somewhere from 2001 to 2005. I guess I'm here asking if some more straight forward solution has come about in recent years.

Hopefully something nice and straight forward like in Java:

int number = JOptionPane.showInputDialog ("Enter an integer");

If that isn't the case, could I get a brief explanation of how to do it?

Edit: I couldn't get anything to work. :( I ended up writing the code to do the work in Java, and then wrote one line of C++ code to call the .jar file. :-/ Since the issue was time sensitive, it was better than nothing.

Answer Source

There is nothing like that for pure C++. Basically what you're trying to do can only be achieved by using an API call to the OS or by using some GUI library like Qt (which I recommend cause it's waaaaay easier then calling native APIs and it's also multi-platform)

Using Qt you can show an input dialog pretty much the same way you do it on java:

bool ok;
QString text = QInputDialog::getText(
        "MyApp 3000", "Enter your name:", QLineEdit::Normal,
        QString::null, &ok, this );
if ( ok && !text.isEmpty() ) {
    // user entered something and pressed OK
} else {
    // user entered nothing or pressed Cancel

You can download the Qt library here: qt.nokia.com/products/developer-tools/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download