0

I am learning to code in C using K&R II. I am tired of console apps and decided to get into a GUI environment. Decided to use Code Blocks and wxWidgets. All is installed properly and working. [Windows 7 x86, Code Blocks 13.12, wxWidgets 3.0.0]

I am following the Tutorials on WxWidgets. I am in Tutorial 9. I have it working, finally; there are mistakes in the instructions.

I modified my app to have 2 text boxes and a button vs one text box for the output and one combo box for the input.

Visual C++ environment is totally foreign to me.

For the Button click I would like instead of printing, "O brave new world!\n", I would like to read what has been entered in textbox1 and print it into textbox2.

The instruction:

wxString Text = TextCtrl1->GetValue();

gets the string that has been entered in textbox1

I have a call to the function

void printg(char *fmt, ...);

I need to know how/what to change the ... argument to so it will passes the wxString Text in the form of an array, I think, to that printg function. I am sure the first thing I need to do is change the Text string to an array, or some way to pass the string itself.

UPDATE 01/08/13 3:35 PM

I cut the code from the Textbox Enter event and pasted it into the ButtonClick event and now I can get the text in Box one to box two.

Now, I need a way to pass the text from textbox 1 to one of my C files, do whatever the exercise is about and pass it back to the click event to be passed to the printg function.

NOTE: I see confusion about printg. I think it is a feature of wxWidgets that lets you print back to a GUI form rather than a console as printf does. It works.

I would put the code on here, but I do not know how. Tried before and get a message about it not being formatted properly.

Thanks for the replies.

  • I think you mean `printf` not `printg` – woolstar Jan 08 '14 at 18:36
  • The "..." is the ellipsis operator: http://stackoverflow.com/questions/3792761/what-is-ellipsis-operator-in-c. – shikamaru Jan 08 '14 at 18:39
  • If you want to put it in another text box, why would you want to use `printf` (assuming you meant that with `printg`)? – leemes Jan 08 '14 at 18:50
  • down voters, at least indicate why. And mis-spelling a word would probably be a stretch considering the field we are in, don't you think? – ryyker Jan 08 '14 at 18:56

4 Answers4

3

Text.ToUTF8().data() gives you const char *

Also if you only want char* instead of const char * you can use const_cast<char *>(Text.ToUTF8().data())

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
2

The most convenient thing to do is to use wx equivalents of standard functions, i.e. wxPrintf() in this case, because they allow you to pass wxString (and also std::string, std::wstring, char* and wchar_t*) objects directly, without doing anything special. So you could simply write

wxString s = ...;
wxPrintf("My string is %s\n", s);

OTOH using either printf() or wxPrintf() is generally not very useful in GUI applications, you probably want wxLogMessage() or something similar.

VZ.
  • 21,740
  • 3
  • 39
  • 42
  • Thanks, I Searched the "wx equivalents of standard functions" and found the string class reference page where I will spend some time trying to learn more. I'd vote this up but I do not have enough points on this site. – user3057562 Jan 09 '14 at 10:06
0

If you have a string: char *str = {"this is my string"};

Then you can use printf() like this:

printf("%s\n", str);

Note, there is no printg() in the C language. And the prototype of the printf() statement allows for multiple arguments to be passed as long as there is a format specifier for each argument. For example, this shows 3 format specifiers, and three arguments:

printf("%s %d %f\n", str, 10, 13.5);
ryyker
  • 22,849
  • 3
  • 43
  • 87
0

The "..." argument is called the ellipse argument. It's covered in the K&R book, section 7.3 (in my edition anyway). It can have 0 or more arguments in it, as described by the *fmt argument.

If you already have a string ready, just call it like this:

printf("%s",str);
Nerf Herder
  • 416
  • 3
  • 11