Thank you for sharing your ideas and experiences. Today, I asked ChatGPT and Mistral (Le Chat) to write a Body mass index calculator in PureBasic.
Here is the prompt which is exact the same for ChatGPT, Le Chat (Mistral) and Grok:
Can you write an application in PureBasic to calculate someone’s BMI. Please use a small window were the user enters weight in kilogram and height in cm. Once both values are entered, the result is automatically given. Restrict the input to numeric values. Give the window a cool layout.
Let’s go!
ChatGPT made a minor error (when entering a value in the input field, for example, 70, the input field converted that to 07), but Le Chat (Mistral) made serious errors.
After asking to fix the error, ChatGPT fixed it, and it is working as expected. However, Le Chat (Mistral) failed terribly after several questions and suggestions to fix it.
This is the working code ChatGPT gave me:
EnableExplicit
Procedure.f CalculateBMI(weight.f, height.f)
If height > 0
ProcedureReturn weight / ((height / 100) * (height / 100))
Else
ProcedureReturn 0
EndIf
EndProcedure
Procedure RestrictNumericInput(GadgetID)
Protected text.s = GetGadgetText(GadgetID)
Protected newText.s, i, dotCount = 0
For i = 1 To Len(text)
If Mid(text, i, 1) >= "0" And Mid(text, i, 1) <= "9"
newText + Mid(text, i, 1)
ElseIf Mid(text, i, 1) = "." And dotCount = 0
newText + "."
dotCount = 1
EndIf
Next
If newText <> text
SetGadgetText(GadgetID, newText)
EndIf
EndProcedure
Procedure UpdateBMI()
Protected weight.f = ValF(GetGadgetText(1))
Protected height.f = ValF(GetGadgetText(2))
Protected bmi.f = CalculateBMI(weight, height)
SetGadgetText(3, StrF(bmi, 2))
EndProcedure
If OpenWindow(0, 100, 100, 250, 180, “BMI Calculator”, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget)
SetWindowColor(0, $1E1E1E) ; Dark background
TextGadget(10, 20, 20, 100, 20, "Weight (kg):", #PB_Text_Right)
StringGadget(1, 130, 20, 80, 25, "")
TextGadget(11, 20, 60, 100, 20, "Height (cm):", #PB_Text_Right)
StringGadget(2, 130, 60, 80, 25, "")
TextGadget(12, 20, 100, 100, 20, "BMI:", #PB_Text_Right)
StringGadget(3, 130, 100, 80, 25, "", #PB_String_ReadOnly)
Repeat
Define Event = WaitWindowEvent()
If Event = #PB_Event_Gadget
If EventGadget() = 1 Or EventGadget() = 2
RestrictNumericInput(EventGadget())
UpdateBMI()
EndIf
EndIf
Until Event = #PB_Event_CloseWindow
EndIf
The above code is working.
I supplied Grok with the same prompt a few moments ago and copied the generated code in a new PureBasic coding window. The code ran from the first time, without any error.
I agree with you, Grok is amazingly good!
Here is the code Grok generated:
; BMI Calculator by Grok 3
#WINDOW_MAIN = 0
#TEXT_WEIGHT = 1
#TEXT_HEIGHT = 2
#INPUT_WEIGHT = 3
#INPUT_HEIGHT = 4
#TEXT_RESULT = 5
#TEXT_BMI = 6
Procedure UpdateBMI()
Protected weight.f, height.f, bmi.f
; Get values from input fields
weight = ValF(GetGadgetText(#INPUT_WEIGHT))
height = ValF(GetGadgetText(#INPUT_HEIGHT))
; Calculate BMI if both values are valid
If weight > 0 And height > 0
height = height / 100 ; Convert cm to meters
bmi = weight / (height * height)
SetGadgetText(#TEXT_BMI, "BMI: " + FormatNumber(bmi, 2))
Else
SetGadgetText(#TEXT_BMI, “BMI: --.–”)
EndIf
EndProcedure
; Create the window
If OpenWindow(#WINDOW_MAIN, 0, 0, 300, 200, “BMI Calculator”, #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
; Set window color to a cool dark theme
WindowColor = RGB(40, 44, 52)
SetWindowColor(#WINDOW_MAIN, WindowColor)
; Create gadgets with a modern look
TextGadget(#TEXT_WEIGHT, 20, 20, 100, 20, “Weight (kg):”, #PB_Text_Right)
StringGadget(#INPUT_WEIGHT, 130, 20, 150, 25, “”, #PB_String_Numeric)
SetGadgetColor(#INPUT_WEIGHT, #PB_Gadget_BackColor, RGB(60, 63, 65))
SetGadgetColor(#INPUT_WEIGHT, #PB_Gadget_FrontColor, RGB(255, 255, 255))
TextGadget(#TEXT_HEIGHT, 20, 60, 100, 20, “Height (cm):”, #PB_Text_Right)
StringGadget(#INPUT_HEIGHT, 130, 60, 150, 25, “”, #PB_String_Numeric)
SetGadgetColor(#INPUT_HEIGHT, #PB_Gadget_BackColor, RGB(60, 63, 65))
SetGadgetColor(#INPUT_HEIGHT, #PB_Gadget_FrontColor, RGB(255, 255, 255))
TextGadget(#TEXT_RESULT, 20, 120, 100, 20, “Result:”, #PB_Text_Right)
TextGadget(#TEXT_BMI, 130, 120, 150, 20, “BMI: --.–”)
; Set text colors
SetGadgetColor(#TEXT_WEIGHT, #PB_Gadget_FrontColor, RGB(158, 206, 255))
SetGadgetColor(#TEXT_HEIGHT, #PB_Gadget_FrontColor, RGB(158, 206, 255))
SetGadgetColor(#TEXT_RESULT, #PB_Gadget_FrontColor, RGB(158, 206, 255))
SetGadgetColor(#TEXT_BMI, #PB_Gadget_FrontColor, RGB(255, 215, 0))
; Event loop
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case #INPUT_WEIGHT, #INPUT_HEIGHT
If EventType() = #PB_EventType_Change
UpdateBMI()
EndIf
EndSelect
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
CloseWindow(#WINDOW_MAIN)
EndIf
So the winners of this BMI test are
- Grok
- ChatGPT
- Le Chat (Mistral)
Grok is based in the United States, which is not a big problem. In the next days I will test Grok further and see how it performs.
Thank you again for sharing your insights, which I appreciate.
Chris