Sign in to follow this  
Followers 0
Venix

Write +/- to a variable

12 posts in this topic

Hey guys,

So i am coding an application for college that is an order form basically consists of a few combo boxes and a list box. They can add products from the combo box to the list box and it will calculate the price. The way the program currently calculates the price is through the use of a long switch statement that reads what value has been added to the list box and adds to the total accordingly. The problem i am having is that i will need to also take away from the price as i have a delete item function, i thought that i would be able to add a variable called addorsubtract and then that variable can be changed depending on the function i am using add or delete. However when i try to define a variable to = + or - i will get an error. Currently i cant find a fix or an alternative solution to this problem and i don't want to have to repeat the switch function as it will significantly up the amount of code.

I'm not sure if i have explained this well enough it is kind of difficult to explain if you need examples just state and i can post one.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

add a negative price

$total+= $cost*-1

I use a numeric type to referance +, -, *, /, 0, 1, 2, 3

switch $operator
case 0
   $r= $x+$y
case 1
   $r= $x-$y
case 2
   $r= $x*$y
case 3
   $r= $x/$y
endswitch

good luck with your project

Edited by Xandy

Share this post


Link to post
Share on other sites

I wrote this a while back but see if this is kinda what you are looking for. Don't worry about the colors and the start time. This was used to see if I was keeping up with the production quota.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=Icon.ico
#AutoIt3Wrapper_UseX64=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ComboConstants.au3>
#include <SendMessage.au3>
Global $GUI2, $DisplayHour, $StartMin, $AMPM, $Display = True

If StartTime() = 1 Then
GUIDelete()
MsgBox(0, "Error", "Please enter all values!", 2)
_restart()
EndIf
Splash(True)
AdlibRegister("PerHour", 60000)
Global $msg, $LayerPlus[5], $LayerMinus[5], $LayerTotal[5], $StartHour, $StartMin, $AMPM
Global $total_layer[5], $total_scrap, $total_overall, $iHoursWorked

$GUI = GUICreate("Stegg Production Keeper", 393, 409, -1, -1, BitOR($WS_POPUP, $WS_BORDER))
GUISetBkColor(0x6666FF)

For $x = 0 to 4
GUISetFont(9, 500, 0, "MS Sans Serif")
GUICtrlCreateGroup("LAYER " & $x + 1, 8, 8 + $x * 80, 193, 65)
GUISetFont(20, 400, 0, "MS Sans Serif")
$LayerPlus[$x] = GUICtrlCreateButton("+", 24, 32 + $x * 80, 41, 25)
$LayerMinus[$x] = GUICtrlCreateButton("-", 80, 32 + $x * 80, 41, 25)
GUISetFont(46, 400, 0, "MS Sans Serif")
$LayerTotal[$x] = GUICtrlCreateLabel("0", 131, 29 + $x * 80, 56, 41, BitOr($GUI_SS_DEFAULT_LABEL, $SS_CENTER))
GUICtrlCreateGroup("", -99, -99, 1, 1)
Next
Global Const $ctrl_offset = $LayerPlus[1] - $LayerPlus[0] ; get CTRLID difference between similar buttons/labels

GUISetFont(10, 500, 0, "MS Sans Serif")
GUICtrlCreateGroup("TOTAL", 224, 16, 155, 77)
GUISetFont(46, 400, 0, "MS Sans Serif")
$OverallTotal = GUICtrlCreateLabel("0", 230, 42, 144, 41, BitOr($GUI_SS_DEFAULT_LABEL, $SS_CENTER))
GUICtrlCreateGroup("", -99, -99, 1, 1)

GUISetFont(10, 500, 0, "MS Sans Serif")
GUICtrlCreateGroup("SCRAP", 224, 111, 155, 97)
GUISetFont(20, 400, 0, "MS Sans Serif")
$ScrapPlus = GUICtrlCreateButton("+", 232, 151, 41, 25)
$ScrapMinus = GUICtrlCreateButton("-", 272, 151, 41, 25)
GUISetFont(46, 400, 0, "MS Sans Serif")
$ScrapTotal = GUICtrlCreateLabel("0", 325, 149, 38, 41)
GUICtrlCreateGroup("", -99, -99, 1, 1)

GUISetFont(10, 500, 0, "MS Sans Serif")
GUICtrlCreateGroup("PARTS PER HOUR", 224, 231, 155, 81)
GUISetFont(20, 600, 0, "MS Sans Serif")
$PartsPerHour = GUICtrlCreateLabel("", 230, 250, 144, 31, BitOr($GUI_SS_DEFAULT_LABEL, $SS_CENTER))
$PartsPerHour2 = GUICtrlCreateLabel("", 230, 275, 144, 31, BitOr($GUI_SS_DEFAULT_LABEL, $SS_CENTER))
GUICtrlCreateGroup("", -99, -99, 1, 1)

GUISetFont(12, 400, 0, "MS Sans Serif")
$Reset = GUICtrlCreateButton("Reset Totals", 242, 335, 120, 31)
$Change = GUICtrlCreateButton("Change Start Time", 222, 370, 160, 31)
$Exit = GUICtrlCreateButton("X", 371, 0, 22, 20)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$Minimize = GUICtrlCreateButton("-", 350, 0, 22, 20)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")

GUISetState(@SW_SHOW)

;~ WinSetTrans($GUI, "", 180)

While 1
$msg = GUIGetMsg()
Switch $msg
Case $Exit
Exit
Case $Minimize
GUISetState(@SW_MINIMIZE)
Splash(False)
Case $GUI_EVENT_PRIMARYDOWN
On_Drag()
Case $LayerPlus[0], $LayerPlus[1], $LayerPlus[2], $LayerPlus[3], $LayerPlus[4]
$x = Abs(($msg - $LayerPlus[0]) / $ctrl_offset) ; identify which plus button was clicked
Update_total($x, 1)
Case $LayerMinus[0], $LayerMinus[1], $LayerMinus[2], $LayerMinus[3], $LayerMinus[4]
$x = Abs(($msg - $LayerMinus[0]) / $ctrl_offset) ; identify which minus button was clicked
Update_total($x, -1)
Case $ScrapPlus
$total_scrap += 1
If $total_scrap < 0 Then
GUICtrlSetColor($ScrapTotal, 0xff0000)
Else
GUICtrlSetColor($ScrapTotal, 0x000000)
EndIf
GUICtrlSetData($ScrapTotal, $total_scrap)
PerHour()
Case $ScrapMinus
$total_scrap -= 1
If $total_scrap < 0 Then
GUICtrlSetColor($ScrapTotal, 0xff0000)
Else
GUICtrlSetColor($ScrapTotal, 0x000000)
EndIf
GUICtrlSetData($ScrapTotal, $total_scrap)
PerHour()
Case $Change
If StartTime() = 1 Then
GUIDelete()
MsgBox(0, "Error", "Please enter all values!")
If StartTime() = 1 Then
GUIDelete()
MsgBox(0, "Error", "No Values, Please Restart Program!")
Exit
EndIf
EndIf
Update_total(1, -1)
Update_total(1, 1)
Case $Reset
For $x = 0 to 4
GUICtrlSetData($LayerTotal[$x], "0")
GUICtrlSetColor($LayerTotal[$x], 0x000000)
$total_layer[$x] = "0"
Next
$total_overall = "0"
$total_scrap = "0"
GUICtrlSetData($ScrapTotal, "0")
GUICtrlSetColor($ScrapTotal, 0x000000)
GUICtrlSetData($OverallTotal, "0")
GUICtrlSetColor($OverallTotal, 0x000000)
GUICtrlSetData($PartsPerHour, "")
GUICtrlSetColor($PartsPerHour, 0x000000)
GUICtrlSetData($PartsPerHour2, "")
GUICtrlSetColor($PartsPerHour2, 0x000000)
Case Else
If BitAnd(WinGetState($GUI), 8) Then Splash(True)
EndSwitch
WEnd

Func Update_total($x, $y)
$total_layer[$x] += $y
If $total_layer[$x] < 0 Then
GUICtrlSetColor($LayerTotal[$x], 0xff0000)
Else
GUICtrlSetColor($LayerTotal[$x], 0x000000)
EndIf
GUICtrlSetData($LayerTotal[$x], $total_layer[$x])
$total_overall += $y
If $total_overall < 0 Then
GUICtrlSetColor($OverallTotal, 0xff0000)
Else
GUICtrlSetColor($OverallTotal, 0x000000)
EndIf
GUICtrlSetData($OverallTotal, $total_overall)
PerHour()
EndFunc   ;==>Update_total

Func PerHour()
$iHoursWorked = _HoursTotal($StartHour) - ($StartMin / 60)
$PerHour = Round(GUICtrlRead($OverallTotal) / ($iHoursWorked + (@MIN / 60)), 2)
If $PerHour < 0 Then
GUICtrlSetData($PartsPerHour, "Error")
GUICtrlSetColor($PartsPerHour, 0xff0000)
Else
GUICtrlSetColor($PartsPerHour, 0x000000)
GUICtrlSetData($PartsPerHour, $PerHour)
EndIf
$PerHour2 = Round((GUICtrlRead($OverallTotal) + GUICtrlRead($ScrapTotal)) / ($iHoursWorked + (@MIN / 60)), 2)
If $PerHour2 < 0 Then
GUICtrlSetData($PartsPerHour2, "Error")
GUICtrlSetColor($PartsPerHour2, 0xff0000)
Else
GUICtrlSetColor($PartsPerHour2, 0x000000)
GUICtrlSetData($PartsPerHour2, $PerHour2)
EndIf
If $PerHour < 16 Then GUISetBkColor(0xFF0000)
If $PerHour > 16 And $PerHour < 18 Then GUISetBkColor(0xFFFF00)
If $PerHour >= 18 Then GUISetBkColor(0x00FF00)
EndFunc   ;==>PerHour

Func _HoursTotal($i_Start)
If @HOUR = $i_Start Then
Return 0
ElseIf @HOUR < $i_Start Then
Return((24 - $i_Start) + @HOUR)
Else
Return(@HOUR - $i_Start)
EndIf
EndFunc   ;==>_HoursTotal

Func StartTime()
$GUI2 = GUICreate("Start", 274, 93, -1, -1, BitOR($WS_POPUP, $WS_BORDER))
GUISetBkColor(0x6666FF)
GUICtrlCreateLabel("Time Started:", 16, 28, 67, 17)
Global $StartHour = GUICtrlCreateCombo("", 88, 25, 49, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "1|2|3|4|5|6|7|8|9|10|11|12")
Global $StartMin = GUICtrlCreateCombo("", 144, 25, 49, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "00|05|10|15|20|25|30|35|40|45|50|55")
$AMPM = GUICtrlCreateCombo("", 208, 25, 49, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "AM|PM")
$Continue = GUICtrlCreateButton("Continue", 48, 52, 77, 27)
$Cancel = GUICtrlCreateButton("Cancel", 138, 52, 77, 27)
GUISetState(@SW_SHOW)

;~  WinSetTrans($GUI2, "", 200)

While 1
Switch GUIGetMsg()
Case $Continue
$StartMin = GUICtrlRead($StartMin)
$DisplayHour = GUICtrlRead($StartHour)
$StartHour = $DisplayHour
$AMPM = GUICtrlRead($AMPM)
Opt("WinTitleMatchMode", 3)
If $StartHour = "" Then Return 1
If $StartMin = "" Then Return 1
If $AMPM = "" Then Return 1
If $StartHour = 12 Then
If $AMPM = "PM" Then $StartHour = 12
If $AMPM = "AM" Then $StartHour = 0
Else
If $AMPM = "PM" Then $StartHour = $StartHour + 12
EndIf
If $StartHour < 10 Then $StartHour = "0" & $StartHour
ExitLoop
Case $Cancel
Exit
Case $GUI_EVENT_PRIMARYDOWN
On_Drag2()
EndSwitch
WEnd
GUIDelete()
EndFunc   ;==>StartTime

Func On_Drag()
Local $aCurInfo = GUIGetCursorInfo($GUI)
If $aCurInfo[4] = 0 Then
DllCall("user32.dll", "int", "ReleaseCapture")
_SendMessage($GUI, $WM_NCLBUTTONDOWN, $HTCAPTION, 0)
EndIf
EndFunc   ;==>On_Drag

Func On_Drag2()
Local $aCurInfo = GUIGetCursorInfo($GUI2)
If $aCurInfo[4] = 0 Then
DllCall("user32.dll", "int", "ReleaseCapture")
_SendMessage($GUI2, $WM_NCLBUTTONDOWN, $HTCAPTION, 0)
EndIf
EndFunc   ;==>On_Drag2

Func Splash($Display)
If $Display Then
If Not WinExists("StartTime") Then
SplashTextOn("StartTime", "Started at: " & $DisplayHour & ":" & $StartMin & " " & $AMPM, 270, 60, -1, 0, 35, -1, 18, 500)
WinSetTrans("StartTime", "", 180)
EndIf
Else
SplashOff()
EndIf
EndFunc

Func _restart()
    If @Compiled = 1 Then
        Run( FileGetShortName(@ScriptFullPath))
    Else
        Run( FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath))
    EndIf
    Exit
EndFunc

Share this post


Link to post
Share on other sites

Thanks guys, ill read over this code. Always good to learn something new ;).

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Still having a little bit of trouble I am not sure if any of the examples are what I am looking for, here is a small section of my code.

I basically need to swap out $AddOrSubtract to be +/- so i don't need to write out the whole switch statement a second time round. Unfortunately i cannot define the prices outside of this statement as it would cause me to have to write out a lot of code therefore defeating the process of using the variable to change whether the function adds or subtracts.

I am not sure if this is possible however if it can be done it will make my code neater which i think is important ;).

Func ModifyPrice() ; Function is used to modify prices.
   Switch $X & " " & $Y

   ; Same process repeated for all products, so only product 0 is commented.
   Case $Product[0]
   $Total = $Total $AddOrSubtract ; Add or subtract from price.
   $FormatValue = StringFormat("%#.2f", $Total, 2) ; Reformat price to have two decimal places.
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue) ; Writing the new value to the order total.
   Case $Product[1]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[2]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[3]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[4]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[5]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[6]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[7]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[8]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[9]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[10]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[11]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[12]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[13]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[14]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[15]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[16]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[17]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[18]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[19]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[20]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[21]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[22]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[23]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[24]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[25]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   Case $Product[26]
   $Total = $Total $AddOrSubtract 0.00
   $FormatValue = StringFormat("%#.2f", $Total, 2)
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue)
   EndSwitch
   EndFunc
Edited by Venix

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

A lot of syntax error!

Yes their will be it is not the whole code it is just a part of it to get my point across. Edited by Venix

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Posted Image

Hows this? It adds, removes items to cart, calculates price.

#include <Guicombobox.au3>
#Include <GuiComboBoxEx.au3>
#include <GUIConstantsEx.au3>
global $itemmax= 3
global $itemdatamax= 2
global $item[$itemmax][$itemdatamax];[0]name[1]cost
;put this in a text data file later
$item[0][0]= "peaches"
$item[0][1]= .65
$item[1][0]= "apples"
$item[1][1]= .50
$item[2][0]= "pears"
$item[2][1]= .60
global $total= 0;
global $cartitemsmax= 12;I don't feel like adding pages to the display items in cart dialog so you can only have 12
global $cart[$cartitemsmax][$itemdatamax]
global $cartitems= 0;
$win= guicreate("Paper Street Soap Company", 420, 200)
guictrlcreatelabel("Item", 20, 20, 65, 20)
;create combo item list
$comboitemlist= guictrlcreatecombo("", 20, 40, 250, 17, $CBS_DROPDOWNLIST)
guictrlsetfont($comboitemlist, 8)
;populate combo item list
for $i= 0 to $itemmax-1
guictrlsetdata($comboitemlist, $item[$i][0])
next
_GUICtrlComboBox_setcursel($comboitemlist, 1)
guictrlcreatelabel("Price", 275, 20, 65, 20)
$x= _GUICtrlComboBox_getcursel($comboitemlist)
$inputitemprice= guictrlcreateinput($item[$x][1], 275, 40, 65, 20)
$buttonaddtocart= guictrlcreatebutton("add to cart", 345, 40, 65, 20)
$labelcartcost= guictrlcreatelabel("", 40, 80, 70, 40)
$buttonmodifycart= guictrlcreatebutton("Modify Cart", 160, 80, 70, 20)
guisetstate();show the window dialog
$redraw= 1
do
$msg= guigetmsg()
switch $msg
case $comboitemlist
  $x= _GUICtrlComboBox_getcursel($comboitemlist)
  guictrlsetdata($inputitemprice, $item[$x][1])
case $buttonaddtocart
  if $cartitems< $cartitemsmax-1 then
   $redraw= 1
   $x= _GUICtrlComboBox_getcursel($comboitemlist)
   $cart[$cartitems][0]= $item[$x][0]
   $cart[$cartitems][1]= $item[$x][1]
   $cartitems+= 1
  else
   msgbox(0, "Cart Overflow", "Too meny items in cart.  Please modify or purchase cart, before adding more items to cart")
  endif
case $buttonmodifycart
  showcart($win)
  $msg= ""
  $redraw= 1
endswitch;end $comboitemlist
if $redraw= 1 then
  $redraw= 0
  $total= 0
  for $i= 0 to $cartitems-1
   $total+= $cart[$i][1]
  next
  guictrlsetdata($labelcartcost, "Cart Cost: $"&$total)
endif
until $msg= $gui_event_close
func showcart($gui)
$wincart= guicreate("Items in Cart", 450, 400, default, default, default, default, $gui)
dim $buttonremove[$cartitemsmax]
dim $labelitemname[$cartitemsmax]
dim $labelitemprice[$cartitemsmax]
for $i= 0 to $cartitemsmax-1
  $labelitemname[$i]= guictrlcreatelabel($cart[$i][0], 20, 20+$i*20, 250, 20)
  $labelitemprice[$i]= guictrlcreatelabel($cart[$i][1], 275, 20+$i*20, 60, 20)
  $buttonremove[$i]= guictrlcreatebutton("X", 335, 20+$i*20, 15, 20)
next
guisetstate()
$redraw= 1
do
  $msg= guigetmsg()
  for $i= 0 to $cartitems-1
   if $msg= $buttonremove[$i] then
    if $cart[$i][0]<> "" then
     $redraw= 1
     for $ii= $i to $cartitems-1
      $cart[$ii][0]= $cart[$ii+1][0]
      $cart[$ii][1]= $cart[$ii+1][1]
     next
     $cartitems-= 1
    endif
   endif
  next;$i check next item in cart
  if $redraw= 1 then
   $redraw= 0
   for $i= 0 to $cartitemsmax-1
    guictrlsetdata($labelitemname[$i], "")
    guictrlsetdata($labelitemname[$i], $cart[$i][0])
    guictrlsetdata($labelitemprice[$i], "")
    guictrlsetdata($labelitemprice[$i], $cart[$i][1])
   next
  endif
until $msg= $gui_event_close
guidelete($wincart)
EndFunc;end showcart()
Edited by Xandy

Share this post


Link to post
Share on other sites

Venix,

If I am understanding you, your script will either add or subtract something from a total. You make this decision logically, somehow. Instead of trying to get a variable to contain an operator you might try flipping the sign of whatever you are accumulating, for example, to subtract a price multiply the price by -1 and then add it to the total. Have not tried this but it might work.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Thanks for all the help ill revise my code and see if i can use one of the examples ;)

Edit:

After revising my code i came up with a solution to use two smaller functions instead of one, one of the functions would call the price depending on the current variable and the other one would add or subtract these prices depending on what a variable was = to.

New modified code below.

Func PriceSwitch() ; Function is used to switch variable that contains price and link to modify price function.
   Switch $X & " " & $Y ; Switch to the statement that contains this product value.
  
   ; Refer to lines 52 to 80 to see product values.
   ; Same process repeated for all products, so only product 0 is commented.
   Case $Product[0]
   $ProductPrice = 5.25 ; Declare the product price
   ModifyPrice () ; Link to modify price statement.
   Case $Product[1]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[2]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[3]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[4]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[5]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[6]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[7]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[8]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[9]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[10]
  $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[11]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[12]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[13]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[14]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[15]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[16]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[17]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[18]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[19]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[20]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[21]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[22]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[23]
   $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[24]
  $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[25]
  $ProductPrice = 0.00
   ModifyPrice ()
   Case $Product[26]
   EndSwitch
   EndFunc
  
   Func ModifyPrice() ; Function is used to modify price.
   If $AddOrSubtract = "Add" Then ; If AddOrSubtract = "Add" then.
   $Total = $Total + $ProductPrice ; Total = Total + ProductPrice.
   $FormatValue = StringFormat("%#.2f", $Total, 2) ; Format total to contain 2 decimal places.
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue) ; Write value to order total.
   ElseIf $AddOrSubtract = "Subtract" Then ; If AddOrSubtract = "Subtract" then.
   $Total = $Total - $ProductPrice ; Total = Total - ProductPrice.
   $FormatValue = StringFormat("%#.2f", $Total, 2) ; Format total to contain 2 deciaml places.
   _GUICtrlEdit_SetText($OrderTotal, "Total: £" & $FormatValue) ; Write value to order total.
   EndIf
   EndFunc
Edited by Venix

Share this post


Link to post
Share on other sites

I like what you did. Your getting the hang of this. Is programming something you plan to equip yourself with, or just a class you have to take to get where you're going?

Share this post


Link to post
Share on other sites

I like what you did. Your getting the hang of this. Is programming something you plan to equip yourself with, or just a class you have to take to get where you're going?

I hope to go into software development, so it is more than just a class it is something i enjoy and a desired career path.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0