Page 1 of 1

My little proggy temperature converter problems

Posted: Mon Feb 23, 2004 2:33 pm
by Amon

Code: Select all

; PureBasic Visual Designer v3.81 build 1321

IncludeFile "GeneratedIncludeFile.pb"

Global conv_type 

Global celcius 

Global fahrenheit 

conv_type = 1

celcius = #string_C

fahrenheit = #string_F


Open_Window_0()

Repeat
  
  Event = WaitWindowEvent()
  
  If Event = #PB_EventGadget
    
    ;Debug "WindowID: " + Str(EventWindowID())
    
    GadgetID = EventGadgetID()
    
    If GadgetID = #chk_celc
     
     conv_type = 1
      
    ElseIf GadgetID = #chk_fahr
    
    conv_type = 0
      
    EndIf
      
    If GadgetID = #Convert_0 And conv_type = 1
    
      fahrenheit = (9/5)*(#string_C) +32
      
    
    ElseIf GadgetID = #Convert_0 And conv_type = 0
    
      celcius = (5/9)*(#string_F-32)
      
    EndIf
    
  EndIf
  
Until Event = #PB_EventCloseWindow

End
;
Ok heres my little proggy I just started. There are no errors when it compiles.

What I tried to make it do

Ok.. The check boxes are to enable a selection between either Celcius to fahrenheit or fahrenheit to celcius. I want to be able to only check one of the options. When the option is selected and the user inputs the relevent info in the correct box, then clicks go, the calculation is made and the result to show in the other box.

confused, not me coz i get it, i just cant explain it well.

Any hints as to where and how I'm going about this incorrectly

:)

Posted: Mon Feb 23, 2004 4:17 pm
by Amon
Ok, as a round up I have managed to set states with setgadgetstate() so that only one chk_box is checked at a time. :)

Now as far as the input goes, since im using numeric variables I cannot use getgadgettext() or getgadgetitemtext() as they deal with only text. o_O.

Ok, i can silently here you all saying "READ THE DOCUMENTATION NOOOB" :D

I am, but at the same time if you can sneak in a little hint it would be great :D

Posted: Mon Feb 23, 2004 4:57 pm
by freak
You can use the Val() function, to convert the string you get from
GetGadgetText() to a number.

Timo

Posted: Mon Feb 23, 2004 7:24 pm
by ebs
Amon,

Another hint: If you use OptionGadgets instead of CheckBoxGadgets, you won't need any code to enforce setting only one. The OptionGadgets do this automatically, by themselves.

Eric

Posted: Mon Feb 23, 2004 10:15 pm
by Amon
thx for the replies.

@ebs

I was using optiongadgets, but i will double check. :)

The problem now is when I use getgadgettext to get the input from a gadget, after the conversion I would like to output the info to the user. Whats the best way of doing this? I have treid setgadgettext but because im dealing with string gadgets it wont let me output numbers.

ie. after the conversion i store the result in a variable name fahrenheit.

if I use setgadgettext(gadgetid,fahrenheit) i get an error stateing that a string is expected.

Posted: Mon Feb 23, 2004 11:06 pm
by Snooze
Now, you can use:
Result$ = Str(Value)

So:

SetGadgetText(gadgetid,Str(fahrenheit))

Just take a look at the purebasic reference manual (PureBasic String) :wink:

Posted: Tue Feb 24, 2004 12:32 am
by Amon
I'm still having problems when I try to make the calculation as it wont let me mix string values with numerical.

I have set the string gadgets to numerical only. I want to grab what the number the user types inside it and then perform the calculation to then be outputted to the relevent text gadget.

I have also pulled out a chunk of haior as a sacrifice O_o

here are Da COdez 1010010101

Code: Select all

; PureBasic Visual Designer v3.81 build 1321

IncludeFile "GeneratedIncludeFile.pb"

Global celcius 

Global fahrenheit 


Open_Window_0()

Repeat
  
  Event = WaitWindowEvent()
  
  If Event = #PB_EventGadget
    
    ;Debug "WindowID: " + Str(EventWindowID())
    
    GadgetID = EventGadgetID()
    
    If GadgetID = #Convert_0
      
      temp1 = GetGadgetText(#string_Celc)
      fahrenheit = fahrenheit + (9/5)*(temp1) +32 
      SetGadgetText(#Output_C, Int(fahrenheit))
      
    EndIf
      
    If GadgetID = #Convert_1
      
      temp2 = getdgettext(#string_Fahr)
      celcius = celcius + (5/9)*(temp2-32)
      SetGadgetText(#Output_F,Str(celcius))
      
    endif
  
      
   
  EndIf
  
Until Event = #PB_EventCloseWindow

End
;

re: convert back and forth string and numberical

Posted: Tue Feb 24, 2004 3:33 am
by wdurden
Hi:

The confusion is arising because you need to do the conversion on items you grab as text to make them numerical to use in calculations. Then to put your result into text you have to convert that to a string... For instance in your code:

temp2 = getdgettext(#string_Fahr)
celcius = celcius + (5/9)*(temp2-32)
SetGadgetText(#Output_F,Str(celcius))

after line 1 your temp2 holds a string. To use it as a number in your line 2 you have to convert it to a number using the val() function, i.e. val(temp2) so that your line would look like :

celcius=celcius + (5/9)*(val(temp2)-32)
Then to put the result in your line three you use the STR() as you have done or possibly STRF() ... look at the reference for if it's not an integer (I can't recall offhand). In other words in your example above it's the use of temp2 which captured a string which needs to be converted to a value (numerical)...

Hope this helps clear it up... its what folks above said, but sometimes said a different way it clicks. Good luck!