Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to manipulate output in MFC VC++?

  1. Jan 21, 2008 #1
    I'm tryin to manipulate output results through a text control in MFC VC++. Just like in console apps, you can manipulate output with "cout", such as: cout << integer1 << "/" << integer2 << " + " << integer3 << "/" << integer4;

    I have a text control in MFC that I need to display a result the same way. I'm using the SetWindowText function:

    Unfortunately this way I can only output one variable with the "buf" variable. What other control is there to modify my output like the cout statement above? For example, how can I get output to look just like that cout statement? Please let me know if you need anymore info to help me. Thanx In Advance!!!
  2. jcsd
  3. Jan 21, 2008 #2
    Personally, I like the Qt library very much, it would be the easiest way to manipulate your strings and then send it to your SetWindowText( ):

    QString buf;
    QTextStream(&buf) << integer1 << "/" << integer2 << " + " << integer3 << "/" << integer4;
    (you can also try .toUtf8(), but i don't know if SetWindowText can handle it...)

    if you want to use utf16:
    wchar_t wbuf[MAX];
    //remember to make sure buf is not too big, or you'll have a buffer overflow...

    anyway, if you don't want to use Qt, just make your own textstream class and override the << opeator...
  4. Jan 21, 2008 #3


    User Avatar
    Science Advisor

    A more "primitive" way to do the same thing is to use the "sprintf". "printf" (output) the data to a string variable, then "SetWindowText(that string variable).
  5. Jan 21, 2008 #4
    I actually had that at first:
    itoa(integer1, buf, 10);
    //sprintf(buf, "%f", integer1);

    but I had to comment that line out, because for some reason it was messing up the result of my "buf" variable. Once I commented that line out I got the right results. Maybe I was using it wrong. I declared buf as such: char buf[20];.
  6. Jan 22, 2008 #5
    I finally figured out how to use the sprintf(), but I'm tryin to output something like this "5/8" using these function:
    sprintf(buf, "%d",numer,"/%d", denom);

    This only prints out the numer variable. How can I output more than one variable using the sprintf()?
  7. Jan 22, 2008 #6
    I think it's
  8. Jan 22, 2008 #7
    I just want to thank all of you for the much needed help. That format actually worked. I'm sure something else is going to pop up so please stay tuned. My fraction calculator almost complete. thanx Again!!!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook