FLTK: Loading to and reading from arrays with Fl_Input widgets

Hello, I need help using arrays with the Fl_Input widget. I would like to be able to store the Fl_Input widget data to an array and also be able to load the array data back into the Fl_Input widget.

I have tried the following code, but the last line produces an error from the compiler which says that the char data type is not compatible with the Fl_Input widget's const char data type.

This leads me to think of two solutions, but I do not know enough about C++ or FLTK to implement either of them at this point: 1.) cast the array's data into a form that is compatible with the Fl_Input widget's data type or 2.) change the Fl_Input widget's data type to integer. How can either of these be done?

Code:
1
2
3
4
int arrayNum[10] = {{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}} 
Fl_Input *input = new Fl_Input(100,100,50,20);               
arrayNum[0] = input->((int)value());                         
input->value((char)arrayNum[0]);  //This produces the data type error  


Can this be used to change an Fl_Input widget's data type to integer? If so, how is it used to do so. The FLTK documentation style is to obscure for someone as new to programming as me.

input->input_type();
Last edited on
Found it. Instead of trying to cast with (char), one must cast with (LPCSTR). The latter effectively casts the int data type back into the const char data type.
As it turns out, (LPCSTR) was just one part of the puzzle. Since the Fl_Input widgets store their data as char, or at least point to their data using a const char pointer, the use of atoi() and itoa() have also had to be used in order to work with the values as integers for math processing. And when getting the data from an Fl_Input to an array, the values must be incrementally read to an array, such as by using a loop to load the values. But when sending the data back to an Fl_Input, one only needs to send an array pointer of type const char and the Fl_Input reads in the full array without having to increment it into the widget.

Getting widget data to an from an external array is an essential part of writing programs with FLTK, it is not entirely straightforward or simple to code for beginners and there are no tutorials on it that I have seen, so a code example is given bellow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//... 
#include <cstring> 
#include <cstdlib> 
//... 

void cb_calculate(Fl_Widget*o,void*)
{ 
Fl_Int_Input *v1 = (Fl_Int_Input*)o->parent()->child(20); 
Fl_Int_Input *v2 = (Fl_Int_Input*)o->parent()->child(21); 

const char *ptr = v1->value(); 
char vRec[3] = {{'a'},{'b'},{'c'}}; 
for (size_t i=0; i<strlen(ptr); i++) 
{ 
vRec[i] = ptr[i]; 
} 
ptr = &vRec[0];
int gval = atoi(ptr); 
gval *= 2; 
char vSen[4] = {{'a'},{'b'},{'c'},{'d'}}; 
itoa(gval,vSen,10); 
ptr = &vSen[0]; 
v2->value((LPCSTR)ptr); 
}
Topic archived. No new replies allowed.