C/C++ How to manipulate output in MFC VC++?

  • Thread starter Thread starter sweetjones
  • Start date Start date
  • Tags Tags
    Output
AI Thread Summary
The discussion focuses on manipulating output results in an MFC application similar to how it's done with "cout" in console applications. The user is trying to display formatted output in a text control using the SetWindowText function but is initially limited to a single variable. Suggestions include using the Qt library for easier string manipulation, where QString and QTextStream can format the output effectively. For those not using Qt, creating a custom text stream class or utilizing "sprintf" for formatting strings is recommended. The user successfully implements "sprintf" to format multiple variables, specifically achieving the desired output format like "5/8" by using the correct syntax. The conversation highlights the importance of proper formatting functions and the potential pitfalls of buffer management in C++. The user expresses gratitude for the assistance received, indicating progress on a fraction calculator project.
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 "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:
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 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!
 
Technology 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...
 
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).
 
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!
 

Similar threads

Replies
9
Views
2K
Replies
14
Views
5K
Replies
3
Views
2K
Replies
15
Views
2K
Replies
1
Views
2K
Replies
30
Views
7K
Back
Top