Input numbers and add them up:
Input numbers and add them up:
if someone can HELP me get started with a little simple program with purebasic to input numbers and add them up then display the total.
Thanks
Thanks
Re: Input numbers and add them up:

Code: Select all
Debug Val(InputRequester("Add","Number 1",""))+Val(InputRequester("Add","Number 2",""))
Last edited by Caronte3D on Mon Oct 10, 2022 8:46 am, edited 1 time in total.
Re: Input numbers and add them up:
Code: Select all
OpenConsole()
Print("First number:")
fn=Input()
Print ("Second number:")
sn=Input()
PrintN("Result of fn+sn:"+Str(fn+sn))
PrintN("Press enter to exit...")
Input()
Good morning, that's a nice tnetennba!
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: Input numbers and add them up:
Small jacdelad's mistake (Result$ = Input() ) fixed :This example shows the two functions which convert datas between string and numeric types.
myNumber = Val(myString$)
myString$ = Str(myNumber)
Also, if you want to input your values in the OS GUI, you can test and modify the example of StringGadget()
Code: Select all
OpenConsole()
Print("First number:")
fn=Val(Input() )
Print ("Second number:")
sn=Val(Input() )
PrintN("Result of fn+sn:"+Str(fn+sn))
PrintN("Press enter to exit...")
Input()
myNumber = Val(myString$)
myString$ = Str(myNumber)
Also, if you want to input your values in the OS GUI, you can test and modify the example of StringGadget()
Re: Input numbers and add them up:
Aye yes, thanks Olli. I typed it on my phone... shouldn't have done that.
Good morning, that's a nice tnetennba!
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: Input numbers and add them up:
Just a thought though, shouldn't we be prompting for a set of numbers, not just two? Also the OP may want non-integers.
Code: Select all
OpenConsole()
tot.d = 0 ; Zeroise running total (double)
Repeat
Print("Enter a number or press Enter To display total : ")
ans.s=Input() ; Input a string
If ans.s = "" ; Input is blank, display total and exit
PrintN("The total is : " + StrD(tot.d)) ; Display running total
Input() ; Press Enter to exit
End
Else
tot.d = tot.d + ValD(ans.s) ; Convert ans.s string into double and add to tunning total
EndIf
ForEver
Re: Input numbers and add them up:
Didn't you mean número, as numbre is name ?Caronte3D wrote: Sun Oct 09, 2022 9:47 pmCode: Select all
Debug Val(InputRequester("Add","Numbre 1",""))+Val(InputRequester("Add","Numbre 2",""))

Re: Input numbers and add them up:
The task was to help to get started, so I think all solutions are legit.
Good morning, that's a nice tnetennba!
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: Input numbers and add them up:
Sincerely I was think this post maybe from a bot, so my answer was a minimal response waiting reaction
Re: Input numbers and add them up:
Caronte3D wrote: Mon Oct 10, 2022 8:45 am Sincerely I was think this post maybe from a bot, so my answer was a minimal response waiting reaction

Will we see the total price of three pairs of Nikes for sale?
Doubt is still allowed...
Re: Input numbers and add them up:
@samxxx: Here's a more complex windowed example for you. Has lots of good stuff for newbies to learn from studying it! Has events, maths, keyboard shortcuts, read-only text fields, etc.
Code: Select all
Enumeration
#gad_number_desc
#gad_number
#gad_add
#gad_clear
#gad_total_desc
#gad_total
#gad_average_desc
#gad_average
#gad_tape
EndEnumeration
#Enter_Pressed=1
Global num.d,total.d,average.d,count
Procedure AddNumber()
num.d=ValD(GetGadgetText(#gad_number))
If num<>0
total.d+num
SetGadgetText(#gad_total,StrD(total))
AddGadgetItem(#gad_tape,-1,StrD(num))
SetGadgetState(#gad_tape,count)
count+1
average.d=total/count
SetGadgetText(#gad_average,StrD(average))
SetGadgetText(#gad_number,"")
EndIf
EndProcedure
Procedure ClearEverything()
total=0
average=0
count=0
SetGadgetText(#gad_number,"")
SetGadgetText(#gad_total,"0")
SetGadgetText(#gad_average,"0")
ClearGadgetItems(#gad_tape)
SetActiveGadget(#gad_number)
EndProcedure
OpenWindow(0,0,0,430,100,"Number Adder",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
TextGadget(#gad_number_desc,10,12,100,20,"Next number:")
StringGadget(#gad_number,95,10,100,20,"",#PB_String_Numeric)
ButtonGadget(#gad_add,205,9,100,22,"Add number")
ButtonGadget(#gad_clear,315,9,100,22,"Clear everything")
TextGadget(#gad_total_desc,10,42,100,20,"Total so far:")
StringGadget(#gad_total,95,40,100,20,"0",#PB_String_ReadOnly)
TextGadget(#gad_average_desc,10,72,100,20,"Average so far:")
StringGadget(#gad_average,95,70,100,20,"0",#PB_String_ReadOnly)
ListViewGadget(#gad_tape,205,40,210,50)
SetActiveGadget(#gad_number)
AddKeyboardShortcut(0,#PB_Shortcut_Return,#Enter_Pressed)
Repeat
ev=WaitWindowEvent()
If ev=#PB_Event_Menu
Select GetActiveGadget()
Case #gad_number,#gad_add : AddNumber()
Case #gad_clear : ClearEverything()
EndSelect
ElseIf ev=#PB_Event_Gadget
Select EventGadget()
Case #gad_add : AddNumber()
Case #gad_clear : ClearEverything()
EndSelect
EndIf
Until ev=#PB_Event_CloseWindow
Re: Input numbers and add them up:
I would find it better to teach beginners to use EnableExplicit and less global variables.BarryG wrote: Thu Oct 13, 2022 10:14 amHas lots of good stuff for newbies to learn from studying it!

Hygge
Re: Input numbers and add them up:
There's a lot I like about this example, as a new user. I like how the routine is self-contained and it's easier to see the structure. I also like the use of string gadgets to display the values (with read-only set), as it looks a lot better than using text gadgets. That was something I hadn't seen before.BarryG wrote: Thu Oct 13, 2022 10:14 am @samxxx: Here's a more complex windowed example for you. Has lots of good stuff for newbies to learn from studying it! Has events, maths, keyboard shortcuts, read-only text fields, etc.
The only lines of code I don't follow, are the below Case statement. I understand #gad_add, because that's the button, but in what circumstances is #gad_number invoked?
Code: Select all
Case #gad_number,#gad_add : AddNumber()
- Mindphazer
- Enthusiast
- Posts: 472
- Joined: Mon Sep 10, 2012 10:41 am
- Location: Savoie
Re: Input numbers and add them up:
If you press Enter and the active gadget is #gad_number, or if you press the button (#gad-add) then you call AddNumber()
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
...and unfortunately... Windows at work...