Search code examples
c++fltk

Adding and assigning values in FLTK


I've been learning FLTK on and off for the last couple of days but today I came to a point where I am stumped.

Here's how the program's supposed to work. There are two buttons: but1, but2. Each one of them has an input widget right next to it. There's also a single output widget on the bottom. Pressing a button will send the corresponding input to the output field where it is added to the value that is currently displayed.

I'm having trouble figuring out how to add the values of two widgets because apparently they're stored as strings. I came up with an idea to circumvent this by first converting the input value to int, then adding it to a variable called "sum" (its value is 0 initially), converting it back to string (ssum) and only then assigning it to output as its value. However, it apparently overloads the function. I don't know what I'm doing wrong because putting inp->value() in there works fine.

How do I do this? Is there a better way to assign a value stored elsewhere to a widget? Or maybe there is a straightforward way to add widgets' values?

Here's the whole code:

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Widget.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Output.H>
#include <string>
using namespace std;
Fl_Output* out;

int sum=0;
char ssum;

void cb_send(Fl_Widget* w, void *v)
{
    if (((Fl_Button*)w)->value() == 0)
    {
        w->hide();
        Fl_Input* inp = (Fl_Input*)v;
        sum = sum + atoi(inp->value());
        ssum = sum;
        out->value(ssum);
        //out->value(inp->value()); this works 
        //out->value( out->value() + inp->value() ); this should theoretically work?
    }
}
int main(int argc, char **argv)
{
    Fl_Window *winmain = new Fl_Window(500, 500);
    Fl_Button *but1 = new Fl_Button(100, 100, 50, 50, "1");
    Fl_Button *but2 = new Fl_Button(100, 200, 50, 50, "2");

    Fl_Input *no1 = new Fl_Input(170, 100, 150, 50, "");
    Fl_Input *no2 = new Fl_Input(170, 200, 150, 50, "");
    out = new Fl_Output(170, 300, 150, 50, "");
    but1->callback((Fl_Callback*) cb_send, no1);
    but2->callback((Fl_Callback*) cb_send, no2);
    winmain->resizable();
    winmain->end();
    winmain->show(argc, argv);

    return Fl::run();
}

Solution

  • You are right that you have to convert strings from input to integers in order to add them and then you need to convert the result back to string to display it. However your code fails on part which is supposed to convert sum to string.

    C style string is a 0-delimited array of characters. In C++ you generally should use safer class std::string. In your code ssum is not any type of string. It is a char - which is an single character, for instance 'a', 'X' and so on and it is represented by a 8-bit integer value. For converting int to string you can use std::to_string function in C++11 or std::stringstream in older versions.

    C++11 solution

    void cb_send(Fl_Widget* w, void *v)
    {
        if (((Fl_Button*)w)->value() == 0)
        {
            w->hide();
            Fl_Input* inp = (Fl_Input*)v;
            sum = sum + atoi(inp->value());
            out->value(std::to_string(sum).c_str());
        }
    }
    

    Pre C++11 solution

    #include <sstream>
    
    ...
    
    void cb_send(Fl_Widget* w, void *v)
    {
        if (((Fl_Button*)w)->value() == 0)
        {
            w->hide();
            Fl_Input* inp = (Fl_Input*)v;
            sum = sum + atoi(inp->value());
    
            std::stringstream ss;
            ss << sum;
            out->value(ss.str().c_str());
        }
    }