How to manipulate output in MFC VC++?

  • Context: C/C++ 
  • Thread starter Thread starter sweetjones
  • Start date Start date
  • Tags Tags
    Output
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
6 replies · 9K views
sweetjones
Messages
44
Reaction score
0
I'm tryin to manipulate output results through a text control in MFC VC++. Just like in console apps, you can manipulate output with "count", such as: count << 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:
m_ctlAnswer.SetWindowText(buf);

Unfortunately this way I can only output one variable with the "buf" variable. What other control is there to modify my output like the count statement above? For example, how can I get output to look just like that count statement? Please let me know if you need anymore info to help me. Thanx In Advance!
 
Physics news on Phys.org
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;
m_ctlAnswer.SetWindowText(buf.toLocal8Bit());
(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...
buf.toWCharArray(wbuf);
m_ctlAnswer.SetWindowText(wbuf);


anyway, if you don't want to use Qt, just make your own textstream class and override the << opeator...
 
HallsofIvy said:
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).

I actually had that at first:
itoa(integer1, buf, 10);
//sprintf(buf, "%f", integer1);
SuppressZeros(buf);
m_ctlAnswer.SetWindowText(buf);
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];.
 
HallsofIvy said:
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).

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);
SuppressZeros(buf);
m_ctlAnswer.SetWindowText(buf);

This only prints out the numer variable. How can I output more than one variable using the sprintf()?
 
I think it's
sprintf(buf,"%d/%d",numer,denum);
 
fargoth said:
I think it's
sprintf(buf,"%d/%d",numer,denum);

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. thanks Again!