## help me! need help with mass conversion calc im makin

New to FreeBASIC? Post your questions here.
nosman
Posts: 15
Joined: Aug 04, 2010 17:06

### help me! need help with mass conversion calc im makin

i am currently making a calculator program(biggest yet)
and now i am adding a mass conversion.
i am starting simple with grams to oncces.
heres the code:

Code: Select all

`sub gramdim num1 as singledim num2 as singledim num3 as singledim num4 as singledim num5 as integerdim ozz as singledim num6 as integerdim num7 as integernum6 = as print"you can convert grams to ounces and pounds here"input"how many grams?";num3num1 = num4 + num3ozz = 0if num3 = 28 * num5 then num5 = > num6    print num1 ;"grams is equal to"print ozz + num5 ;"oz."end ifend sub`
j_milton
Posts: 458
Joined: Feb 11, 2010 17:35

Code: Select all

`Sub grams_to_ounces()   Dim As Single grams, ounces   Dim As Integer pounds   Print "you can convert grams to ounces and pounds here"   Input "how many grams";grams   ounces = grams / 28.3495   If ounces < 16.0 Then      pounds = 0   Else      pounds = Fix(ounces / 16.0)      ounces = ounces - (pounds * 16)   EndIf   Print grams & " grams = " & pounds & " pounds " & ounces & " ounces"End Subgrams_to_ounces()sleep`
square1
Posts: 97
Joined: Nov 12, 2007 2:27
I have got a unit converter (Windows only). It's by no means perfect, but it works for me and you might find some of it interesting. The clipboard function are shamelessly copied from somewhere in this forum.

Code: Select all

`#include "windows.bi"#Include "vbcompat.bi"Dim As Integer cmdDim As Integer x,y,nDim As Double qDim As String instring,numstring,ustring,charType conversion   usource(10) As String   utarget As String   f As DoubleEnd TypeDim As conversion c(100)Declare Function convert (c As conversion,q As Double) As DoubleDeclare Function GetClipboardAsString() As StringDeclare Function CopyToClipboard(Byref x As String) As IntegerDo   n=n+1   For x = 1 To 10      Read c(n).usource(x)   Next   Read c(n).utarget   Read c(n).fLoop Until c(n).usource(1)="Ende"n=n-1instring=getclipboardasstringFor x = 1 To Len(instring)   char=Mid(instring,x,1)   If Str(Val(char))=char Or char="." Then      numstring=numstring+char   Else      If char<>" " And char <>"," Then ustring=ustring+LCase(char) EndIf   EndIfNextq=Val(numstring)For x = 1 To n   For y = 1 To 10      If ustring=c(x).usource(y) And ustring<>"" Then cmd=x End if   NextNextIf cmd = 0 Then   Print "Value from clipboard: ";q   print   Print ustring;" not recognised, please select"   Print   For x = 1 To n      Print x;") ";c(x).usource(1);" into ";c(x).utarget   Next   Print   Print"99) quit"      Input cmd   If cmd>n Then cmd=99 End IfEndIfSelect Case cmd   Case 99      End   Case Else      x=convert(c(cmd),q)            End SelectFunction convert (c As conversion,q As Double) As Double      Dim As String s,char,outstring   Dim As Integer x   Print c.usource(1);" into ";c.utarget      If Int(q*c.f)=q*c.f And q*c.f>=1000 Then      s=Format(q*c.f,",000")   elseIf Int(q*c.f)<>q*c.f And q*c.f>=1000 Then      s=Format(q*c.f,",000.00")      ElseIf Int(q*c.f)=q*c.f Then      s=Str(q*c.f)   ElseIf Int(q*c.f)<>q*c.f Then      s=Format(q*c.f,"0.00")   EndIf      For x = 1 To Len(s)      char=Mid(s,x,1)      If char="." Then         outstring=outstring+","       ElseIf char="," Then         outstring=outstring+"."      Else         outstring=outstring+char      EndIf         Next      outstring=outstring+" "+c.utarget   Print q;" ";c.usource(1);" = ";outstring   copytoclipboard (outstring)   Return q*c.f   End FunctionData "feet","foot","ft","'","","","","","",""Data "Meter",0.3048 Data "inches","inch","in","''","i","","","","",""Data "cm",2.54Data "miles","mile","m","","","","","","",""Data "km",1.609344Data "yards","yard","yd","","","","","","",""Data "Meter",0.9144Data "squarefeet","sqfeet","sqft","squarefoot","sqfoot","","","","",""Data "qm",0.09290304Data "acres","acre","","","","","","","",""Data "ha",0.404685642Data "ounces","ounce","oz","","","","","","",""Data "kg",0.0283495231Data "pounds","pound","lbs","lb","","","","","",""Data "kg",0.45359237Data "Ende","","","","","","","","",""Data "Ende",0Function GetClipboardAsString() As String   Dim As Zstring Ptr s_ptr  Dim As HANDLE hglb  Dim As String s = ""  If( IsClipboardFormatAvailable(CF_TEXT) = 0 ) Then    Return ""  End If  If OpenClipboard( NULL ) <> 0 Then      hglb = GetClipboardData( cf_text )      s_ptr = GlobalLock( hglb )      If ( s_ptr <> NULL ) Then         s = *s_ptr        GlobalUnlock( hglb )      End If      CloseClipboard()       End If  Return s     End FunctionFunction CopyToClipboard(Byref x As String) As Integer    Function = false    Dim As HANDLE hText = NULL    Dim As Ubyte Ptr clipmem = NULL    Dim As Integer n = Len(x)    If n > 0 Then        hText = GlobalAlloc(GMEM_MOVEABLE Or GMEM_DDESHARE, n + 1)        Sleep 15        If (hText) Then            clipmem = GlobalLock(hText)            If clipmem Then                CopyMemory(clipmem, Strptr(x), n)            Else                hText = NULL            End If            If GlobalUnlock(hText) Then                hText = NULL            End If        End If        If (hText) Then            If OpenClipboard(NULL) Then                Sleep 15                If EmptyClipboard() Then                    Sleep 15                    If SetClipboardData(CF_TEXT, hText) Then                        Sleep 15                        Function = True                    End If                End If                CloseClipboard()            End If        End If    End IfEnd Function `

I haven't got time to comment or explain the code now, but if you have any questions, I'll answer them later.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

PERIODS.... don't forget the periods.

The variable naming and general structure leaves much to be desired. I have no idea what "num6 = as" means, as well... In general, I have no idea what you are actually doing in your program.

Code: Select all

`sub grams_convert () dim as double ounce_value, gram_value, pound_value print "You can convert grams to ounces and pounds here." input "How many grams? ", gram_value ounce_value = gram_value / 28.3495231 pound_value = gram_value / 453.59237 print str(gram_value) + " grams is equal to " + str(ounce_value) + " ounces, or " + str(pound_value) + " pounds. " sleep systemend subgrams_convert ()`
nosman
Posts: 15
Joined: Aug 04, 2010 17:06
i finally got it to work now i'm doing oz to g. it keeps failing
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48
There is no need to start a new thread... we can read. :\

Ounces to grams is the inverse of grams to ounces. Simple:

Code: Select all

`ounce_value = gram_value / 28.3495231'ounce_value * 28.3495231 = gram_value ' Multiply both sides by 28.3495231. This is not code.gram_value = ounce_value * 28.3495231`