How to manipulate output in MFC VC++?

  • C/C++
  • Thread starter sweetjones
  • Start date
  • Tags
    Output
In summary, the conversation discusses the use of various methods to manipulate output results through a text control in MFC and how to output multiple variables using the sprintf() function. The suggested methods include using the Qt library, creating a custom textstream class, and using the sprintf() function with proper formatting. The conversation ends with the OP expressing gratitude for the help received and mentioning that their fraction calculator is almost complete.
  • #1
sweetjones
44
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
  • #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;
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...
 
  • #3
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).
 
  • #4
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];.
 
  • #5
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()?
 
  • #6
I think it's
sprintf(buf,"%d/%d",numer,denum);
 
  • #7
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!
 

What is MFC VC++?

MFC (Microsoft Foundation Class) is a C++ library that provides a framework for building Windows-based applications. VC++ (Visual C++) is a specific implementation of the C++ programming language by Microsoft. MFC VC++ is a combination of these two, allowing developers to create Windows applications using the MFC framework in the Visual C++ environment.

How can I manipulate output in MFC VC++?

To manipulate output in MFC VC++, you can use various classes and functions provided by the MFC library. Some commonly used methods include the CWnd::GetDC() function to obtain a device context, the CDC::TextOut() function to output text, and the CDC::Rectangle() function to draw shapes.

Can I customize the appearance of the output in MFC VC++?

Yes, you can customize the appearance of the output in MFC VC++ by using various formatting options and properties. For example, you can change the font, color, and size of the output text using the CDC::SelectObject() function, or set the background color using the CDC::SetBkColor() function.

How can I save the output in MFC VC++ to a file?

To save the output in MFC VC++, you can use the CFile class to create and write to a file. First, you need to create an instance of the CFile class and specify the file name and mode. Then, you can use the CFile::Write() function to write the output to the file. Finally, remember to close the file using the CFile::Close() function.

Are there any other ways to manipulate output in MFC VC++?

Yes, there are other ways to manipulate output in MFC VC++. You can also use the GDI (Graphics Device Interface) functions, such as TextOut() and Rectangle(), to manipulate output directly. Additionally, MFC provides the CArchive and CDocument classes for handling file input and output, which can also be used to manipulate output in MFC VC++.

Similar threads

  • Computing and Technology
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • STEM Academic Advising
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
274
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top