GIDForums  

Go Back   GIDForums > Computer Programming Forums > FLTK Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Dec-2004, 08:23
OETN OETN is offline
New Member
 
Join Date: Dec 2004
Posts: 6
OETN is on a distinguished road
Lightbulb

How to create integer variables


Hello,

I have recently created a basic game of battleships using C++ and am looking to adapt it to work in a FLTK GUI.

However I'm far from an expert and need a little help. To start with I am trying to create a simple FLTK piece of code that allows a user to enter two seperate integers into two seperate text boxes. On the click of a button I then want to store these integers as standard integer variables so I can perform some basic arithmatic operators on them. At present I can only figure out how to store them as strings. If anyone has any ideas or if they can provide the code, as I have a feeling it may not be all that complicated. Please help me out.

Many thanks

Oli
  #2  
Old 26-Dec-2004, 03:43
fltk2user fltk2user is offline
New Member
 
Join Date: Dec 2004
Posts: 5
fltk2user is on a distinguished road
Quote:
Originally Posted by OETN
Hello,

I have recently created a basic game of battleships using C++ and am looking to adapt it to work in a FLTK GUI.

However I'm far from an expert and need a little help. To start with I am trying to create a simple FLTK piece of code that allows a user to enter two seperate integers into two seperate text boxes. On the click of a button I then want to store these integers as standard integer variables so I can perform some basic arithmatic operators on them. At present I can only figure out how to store them as strings. If anyone has any ideas or if they can provide the code, as I have a feeling it may not be all that complicated. Please help me out.

Many thanks

Oli

Hi,
You dont say which FLTK version you are using, but here is code for 1.x:

CPP / C++ / C Code:
 Fl_Int_Input *int_input1, *int_input2; // These are initialized somewhere
 void button_callback(Fl_Widget *button, void *data)
 {
   // Button pressed, get values from inputs

   // You may want to do some sanity checks here
   if (int_input1->size() > 0 && int_input2->size() > 0) {
     long val1 = strtol(int_input1->value(),NULL,10); // Use 16 if hex value
     long val2 = strtol(int_input2->value(),NULL,10); // Use 16 if hex value
     do_calculation(val1, val2); // Perform some operation with values
   } else {
     fl_alert("Please fill both fields");
   }
 }

For FLTK2 code is same, but replace "Fl_" with "fltk::"
Hope this helps.
  #3  
Old 26-Dec-2004, 09:07
OETN OETN is offline
New Member
 
Join Date: Dec 2004
Posts: 6
OETN is on a distinguished road
Arrow

Further Query


Firstly thanks for the previous help, it help solved part of the problem and is greatly appreciated.

However I still have one small problem, I am now looking to output integer variables into a textbox. So far the code I have is:

CPP / C++ / C Code:
#include <FL/Fl.H>				
#include <FL/Fl_Window.H>
#include <Fl/Fl_Input.H>
#include <Fl/Fl_Output.H>
#include <Fl/Fl_Button.H>
#include <FL/Fl_Int_Input.H>


#include <string>
string firstname;
string greeting;


Fl_Int_Input  *nameInput;
Fl_Button *helloButton;
Fl_Output *outBox;

void doHelloButton(Fl_Widget* button, void* data) {
	 int val1 = strtol(nameInput->value(),NULL,10); 
//Stores inputinteger in variable, val1
   	 int val2=val1*4;                               
//Times val1 by four and stores in val2
 	 //output into outBox
}

int main() {
    Fl_Window *window = new Fl_Window(500,300,"My FLTK program");
  
	nameInput = new Fl_Int_Input(50,25,100,50,"Name");
	helloButton = new Fl_Button(50,150,150,50, "Say Hi");	 
	helloButton->callback(doHelloButton); 	    
  window->end();
  window->show();  
  return Fl::run();
}


The template of this program is something that came with my fltk install, however I am using it as a practise tool.

Within the callback function I want to place the contents of the 'val2' variable within the 'outBox' textbox. If I were dealing with a string I understand I would use 'outBox->value(val2.c_str());'. However this does not work for an integer so I am basically looking for the integer equivalent.

Any help anyone can provide would be greatly appreciated.

Many Thanks

Ollie

P.S: I am using the 1.10 version of fltk
  #4  
Old 26-Dec-2004, 09:31
fltk2user fltk2user is offline
New Member
 
Join Date: Dec 2004
Posts: 5
fltk2user is on a distinguished road
First of all, I suggest you to update FLTK version to atleast 1.1.6

Anyway, here's code:
CPP / C++ / C Code:
void doHelloButton(Fl_Widget* button, void* data) {
  int val1 = strtol(nameInput->value(),NULL,10); 
  //Stores inputinteger in variable, val1
  int val2=val1*4;                               
  //Times val1 by four and stores in val2
  //output into outBox

  // Print value 'val2' to 'tmp' buffer and set it to outBox
  char tmp[16]; sprintf(tmp, "%d", val2);
  outBox->value(tmp);
}

Hope this helped.

BR,
Mikko
  #5  
Old 26-Dec-2004, 11:04
OETN OETN is offline
New Member
 
Join Date: Dec 2004
Posts: 6
OETN is on a distinguished road
Thumbs up

Thanks


Absolute legend. Worked perfectly.

Thanks

Oli
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variables out of an iframe by url JUNK KED MySQL / PHP Forum 5 31-Jul-2007 10:33
Function and Array (w/ reference variables) question brookeville C++ Forum 15 07-Dec-2004 02:11
Help! Some basal questions about MFC xutingnjupt MS Visual C++ / MFC Forum 1 05-Dec-2004 04:38
Can't seem to create db Tigress7 MySQL / PHP Forum 3 19-Aug-2003 10:19

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:57.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.