How to assign the value of a variable to a Fl_Output widget

36 views
Skip to first unread message

jorgem...@gmail.com

unread,
Apr 23, 2015, 6:09:43 PM4/23/15
to fltkg...@googlegroups.com
I have a global integer variable in my code (called intFlipCount) that I need to assign to a Fl_Output widget. The following is the Fl_Output control:

Fl_Output* outFlipCount = new Fl_Output(400, 550, 50, 25, "Flip count");

I searched for information about how to get what I need and found the following link: https://www.gidforums.com/t-5915.html. However, I do not know how to apply it for my case.
I will very much appreciate any help you can provide.

Respectfully,
Jorge Maldonado


Greg Ercolano

unread,
Apr 23, 2015, 7:04:12 PM4/23/15
to fltkg...@googlegroups.com
On 04/23/15 15:09, jorgem...@gmail.com wrote:
> I have a global integer variable in my code (called intFlipCount) that
> I need to assign to a Fl_Output widget. The following is the Fl_Output control:
>
> Fl_Output* outFlipCount = new Fl_Output(400, 550, 50, 25, "Flip count");

You can use sprintf() to get it into a string, then use Fl_Output's
value() method to apply that string to it. e.g.

char s[40];
sprintf(s, "%d", intFlipCount);
outFlipCount->value(s);


jorgem...@gmail.com

unread,
Apr 23, 2015, 8:40:49 PM4/23/15
to fltkg...@googlegroups.com, erco_...@seriss.com
I am very grateful for all the help you have provided to this and other 2 questions because you have solved critical issues for a final project that is extremely important to me. I hope you do not mind if I keep asking additional questions, although before posting a new one I always try to solve it by myself. Sometimes I find an answer but sometimes I do not.

Best regards,
Jorge Maldonado

Greg Ercolano

unread,
Apr 23, 2015, 9:16:04 PM4/23/15
to fltkg...@googlegroups.com
On 04/23/15 17:40, jorgem...@gmail.com wrote:
> I am very grateful for all the help you have provided to this and other 2 questions because you have solved critical issues for a final project that is extremely important to me. I hope you do not mind if I keep asking additional questions, although before posting a new one I always try to solve it by myself. Sometimes I find an answer but sometimes I do not.

Fire away.. if I can't reply, other folks here will..

Edzard Egberts

unread,
Apr 24, 2015, 2:22:40 AM4/24/15
to 'ed' via fltk.general
Greg Ercolano wrote:
> On 04/23/15 15:09, jorgem...@gmail.com wrote:
>> I have a global integer variable in my code (called intFlipCount) that
>> I need to assign to a Fl_Output widget. The following is the Fl_Output control:
>>
>> Fl_Output* outFlipCount = new Fl_Output(400, 550, 50, 25, "Flip count");
>
> You can use sprintf() to get it into a string, then use Fl_Output's
> value() method to apply that string to it. e.g.
>
> char s[40];
> sprintf(s, "%d", intFlipCount);
> outFlipCount->value(s);
>
std::ostringstream OS;
OS << intFlipCount;
outFlipCount->value(OS.str().c_str());

The main difference - this kind of code never will cause a buffer
overflow. This time s[40] for sure is big enough for an integer, but as
far as I know, estimating buffer lengths continues to cause vulnerable
programs.


Greg Ercolano

unread,
Apr 24, 2015, 2:36:53 AM4/24/15
to fltkg...@googlegroups.com
On 04/23/15 23:22, Edzard Egberts wrote:
> Greg Ercolano wrote:
>> You can use sprintf() to get it into a string, then use Fl_Output's
>> value() method to apply that string to it. e.g.
>>
>> char s[40];
>> sprintf(s, "%d", intFlipCount);
>> outFlipCount->value(s);
>>
> std::ostringstream OS;
> OS << intFlipCount;
> outFlipCount->value(OS.str().c_str());
>
> The main difference - this kind of code never will cause a buffer
> overflow. This time s[40] for sure is big enough for an integer, but as
> far as I know, estimating buffer lengths continues to cause vulnerable
> programs.

Tis true..

"snprintf()" (emphasis on 'n') can get you around some of that.
But the STL approach is certainly better; no worries for space.



Martin McDonough

unread,
Apr 24, 2015, 1:26:46 PM4/24/15
to fltkg...@googlegroups.com, erco_...@seriss.com
Or just use std::to_string if you don't want to involve stream objects :)
Reply all
Reply to author
Forward
0 new messages