Input numbers and add them up:

Just starting out? Need help? Post your questions and find answers here.
samxxx
User
User
Posts: 10
Joined: Sun Oct 09, 2022 5:17 pm

Input numbers and add them up:

Post by samxxx »

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
User avatar
Caronte3D
Addict
Addict
Posts: 1365
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Input numbers and add them up:

Post by Caronte3D »

:shock:

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.
User avatar
jacdelad
Addict
Addict
Posts: 2027
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Input numbers and add them up:

Post by jacdelad »

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
Olli
Addict
Addict
Posts: 1251
Joined: Wed May 27, 2020 12:26 pm

Re: Input numbers and add them up:

Post by Olli »

Small jacdelad's mistake (Result$ = Input() ) fixed :

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()
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()
User avatar
jacdelad
Addict
Addict
Posts: 2027
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Input numbers and add them up:

Post by jacdelad »

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
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Input numbers and add them up:

Post by Oso »

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
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Input numbers and add them up:

Post by Oso »

Caronte3D wrote: Sun Oct 09, 2022 9:47 pm

Code: Select all

Debug Val(InputRequester("Add","Numbre 1",""))+Val(InputRequester("Add","Numbre 2",""))
Didn't you mean número, as numbre is name ? :D
User avatar
jacdelad
Addict
Addict
Posts: 2027
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Input numbers and add them up:

Post by jacdelad »

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
User avatar
Caronte3D
Addict
Addict
Posts: 1365
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Input numbers and add them up:

Post by Caronte3D »

Sincerely I was think this post maybe from a bot, so my answer was a minimal response waiting reaction
User avatar
Caronte3D
Addict
Addict
Posts: 1365
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Input numbers and add them up:

Post by Caronte3D »

Oso wrote: Sun Oct 09, 2022 11:32 pm Didn't you mean número, as numbre is name ? :D
:lol: :lol:
Olli
Addict
Addict
Posts: 1251
Joined: Wed May 27, 2020 12:26 pm

Re: Input numbers and add them up:

Post by Olli »

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
:D
Will we see the total price of three pairs of Nikes for sale?

Doubt is still allowed...
BarryG
Addict
Addict
Posts: 4206
Joined: Thu Apr 18, 2019 8:17 am

Re: Input numbers and add them up:

Post by BarryG »

@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
User avatar
Kiffi
Addict
Addict
Posts: 1509
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Input numbers and add them up:

Post by Kiffi »

BarryG wrote: Thu Oct 13, 2022 10:14 amHas lots of good stuff for newbies to learn from studying it!
I would find it better to teach beginners to use EnableExplicit and less global variables. :P
Hygge
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Input numbers and add them up:

Post by Oso »

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.
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.

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()
And the other thing is the keyboard shortcut. I'm not sure I understand how that's triggering the add function.
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 472
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Input numbers and add them up:

Post by Mindphazer »

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...
Post Reply