2a.wxWidgets – console


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.


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.

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

int main(int argc, char **argv) {
	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 "));


This is the output:

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

1 Trackback / Pingback

  1. 1.wxWidgets – Introduction – Truelogic Blog

Leave a Reply

Your email address will not be published.