Page 1 of 2

Input numbers and add them up:

Posted: Sun Oct 09, 2022 9:07 pm
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

Re: Input numbers and add them up:

Posted: Sun Oct 09, 2022 9:47 pm
by Caronte3D
:shock:

Code: Select all

Debug Val(InputRequester("Add","Number 1",""))+Val(InputRequester("Add","Number 2",""))

Re: Input numbers and add them up:

Posted: Sun Oct 09, 2022 9:54 pm
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()

Re: Input numbers and add them up:

Posted: Sun Oct 09, 2022 10:05 pm
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()

Re: Input numbers and add them up:

Posted: Sun Oct 09, 2022 10:55 pm
by jacdelad
Aye yes, thanks Olli. I typed it on my phone... shouldn't have done that.

Re: Input numbers and add them up:

Posted: Sun Oct 09, 2022 11:20 pm
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

Re: Input numbers and add them up:

Posted: Sun Oct 09, 2022 11:32 pm
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

Re: Input numbers and add them up:

Posted: Mon Oct 10, 2022 6:38 am
by jacdelad
The task was to help to get started, so I think all solutions are legit.

Re: Input numbers and add them up:

Posted: Mon Oct 10, 2022 8:45 am
by Caronte3D
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:

Posted: Mon Oct 10, 2022 8:47 am
by Caronte3D
Oso wrote: Sun Oct 09, 2022 11:32 pm Didn't you mean número, as numbre is name ? :D
:lol: :lol:

Re: Input numbers and add them up:

Posted: Mon Oct 10, 2022 11:02 am
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...

Re: Input numbers and add them up:

Posted: Thu Oct 13, 2022 10:14 am
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

Re: Input numbers and add them up:

Posted: Thu Oct 13, 2022 11:42 am
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

Re: Input numbers and add them up:

Posted: Sat Oct 15, 2022 12:03 pm
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.

Re: Input numbers and add them up:

Posted: Sat Oct 15, 2022 2:39 pm
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()