How to manipulate output in MFC VC++?

  • Context: C/C++ 
  • Thread starter Thread starter sweetjones
  • Start date Start date
  • Tags Tags
    Output
Click For Summary

Discussion Overview

The discussion centers around manipulating output in MFC VC++ text controls, specifically how to format and display multiple variables in a manner similar to console output using the "count" operator. Participants explore various methods for achieving this, including using libraries and functions available in C++.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant seeks to replicate console-style output in an MFC text control using the SetWindowText function, expressing difficulty in formatting multiple variables.
  • Another participant suggests using the Qt library for string manipulation, providing an example with QString and QTextStream to format the output before sending it to SetWindowText.
  • Several participants propose using the "sprintf" function as a more primitive method to format strings, discussing its application and potential issues with buffer sizes.
  • One participant shares their experience with using itoa and sprintf, noting that commenting out certain lines resolved issues with their output.
  • A later reply clarifies the correct usage of sprintf for formatting multiple variables, specifically suggesting the format string "%d/%d" to achieve the desired output.
  • Another participant expresses gratitude for the help received and confirms that the suggested format worked for their needs.

Areas of Agreement / Disagreement

Participants generally agree on the utility of sprintf for formatting output, but there are differing opinions on the best approach, including the use of Qt versus native C++ methods. The discussion remains open to further exploration of solutions.

Contextual Notes

Some participants mention potential issues with buffer overflow and the need to ensure proper string handling, indicating that the solutions may depend on specific implementation details.

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!
 
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 ·
Replies
9
Views
2K
Replies
17
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K