Categories
Mastering Development

How can I convert a const char* to a string in CLR/C++ with FLTK to obtain the value of an FL_Input or other widget?

I have been recently constructing a program. The end goal is to get what the user has written in the Fl_Input box and write it into my database. To do this I am going to need a callback on a button that allows me to at least get the string value of an Fl_Input->value()

The expected results is that the contents of the Fl_Input will be outputted in the console window (but eventually they will be written to a database). The actual results that I am getting is an error code stating that the program is trying to write to secure/corrupted memory it is not too clear so I will paste below

An unhandled exception of type ‘System.AccessViolationException’ occurred in Start.exe
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

As eventually I would like to have multiple of these Fl_Inputs as strings and store them in some sort of vector I will leave that part of the code in as well but it is commented.

struct Info
{
    // The widgets
    Fl_Input* instr;
    Fl_Int_Input* inint;

    // Saved values
    char sval[40];
    int  ival;
};

// Callback for the done button
void done_cb(Fl_Widget* w, void* param)
{
    Info* input = reinterpret_cast<Info*>(param);

    // Get the values from the widgets
    //const char* l = input->instr->value();
    //auto my_cstr = l;
    //std::string s(my_cstr);

    strcpy_s(input->sval, input->instr->value());
    input->ival = atoi(input->inint->value());

    // Print the values
    printf("String value is %s\n", input->sval);
    printf("Integer value is %d\n", input->ival);
}

void signupScreen (void) {
    //Fl_Input *fname, *lname, *username, *password, *dob;
    //Fl_Window *w = new Fl_Window(800, 800, "signup");
    //fname = new Fl_Input(200, 100, 500, 30, "Enter first name:");
    //lname = new Fl_Input(200, 150, 500, 30, "Enter last name:");
    //username = new Fl_Input(200, 200, 500, 30, "Enter username wanted:");
    //password = new Fl_Input(200, 250, 500, 30, "Enter password wanted:");
    //dob = new Fl_Input(200, 300, 500, 30, "Enter date of birth in format dd/mm/yyyy:");
    //std::vector<std::string> v; //= { string(fname->value()), string(lname->value()), string(username->value()), string(password->value()), string(dob->value()) };
    Info input;

    // Create the window
    Fl_Window *window = new Fl_Window(200, 150);
    int x = 50, y = 10, w = 100, h = 30;
    input.instr = new Fl_Input(x, y, w, h, "Str");
    input.instr->tooltip("String input");

    y += 35;
    input.inint = new Fl_Int_Input(x, y, w, h, "Int");
    input.inint->tooltip("Integer input");

    y += 35;
    Fl_Button* done = new Fl_Button(x, y, 100, h, "Done");
    done->callback(done_cb, &input);
    window->end();


    window->show();

}

EDIT: Remove line of code

Leave a Reply

Your email address will not be published. Required fields are marked *