2a.wxWidgets – console

OVERVIEW

The two most common ways of displaying anything out on the console is by using wxPuts() and wxPrintf() . wxPuts() prints a string value or variable to the console. wxPrintf() formats objects and variables into a string and displays the formatted string much like the regular C++ printf function.

When using string constants, we use the wxT() function which takes care of the string settings as configured in wxWidgets, in terms of using widechar Unicode characters or the old style narrow ANSI strings. It is safer and the norm these days to handle strings as Unicode to take care of non-English character sets. So using wxT() or _T() converts a string constant to its Unicode format.

In console apps, we do not use or declare the wxApp() object, so it is recommended to call wxInitialize() at the start of the program and then call wxUninitialize() at the end. They are not compulsory as the code runs even without these functions but it is good practice to put them in your console apps.

SAMPLE CODE

In the code below, we print out a couple of string values. We also using STL to input some characters from the keyboard and then print them out as wxString.

console.cpp
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
	#include <wx/wx.h>
#endif
#include <wx/string.h>
#include <iostream>

int main(int argc, char **argv) {
	wxInitialize();
	wxPuts(wxT("String displayed using wxPuts"));
	wxPuts(wxT("Enter a 10 character alphanumeric string without spaces"));
	char str[10];
	std::cin >> str;
	wxPuts(wxT("\n\nText entered was "));
	wxPuts(str);

	wxUninitialize();
}

This is the output:

String displayed using wxPuts
 Enter a 10 character alphanumeric string without spaces
 teststring
 Text entered was 
 teststring

1 Trackback / Pingback

  1. 1.wxWidgets – Introduction – Truelogic Blog

Leave a Reply

Your email address will not be published.


*