
-----------------------------------
ThatTuringNoob
Thu May 07, 2015 6:55 pm

cant seem to get my clear button to work on my calculator
-----------------------------------
What is it you are trying to achieve?
I'm trying to get the clear button C to reset all variables and restart the program


What is the problem you are having?
I cant get the clear button to work


Describe what you have tried to solve this problem
looked at many forums on here for help




Please specify what version of Turing you are using
4.1.1a

-----------------------------------
Insectoid
Thu May 07, 2015 7:20 pm

RE:cant seem to get my clear button to work on my calculator
-----------------------------------
I'm gonna cut you some slack because calculators are actually pretty tough compared to other common beginner projects, but there is a lot wrong with your code that makes it difficult to read and therefore difficult to debug. Consider the following segment:
[code]if num1 = 0 and num = 0 then
	num5 := 0
    elsif num1 = 0 and num = 1 then
	num5 := 1
    elsif num1 = 0 and num = 2 then
	num5 := 2
    elsif num1 = 0 and num = 3 then
	num5 := 3
    elsif num1 = 0 and num = 4 then
	num5 := 4
    elsif num1 = 0 and num = 5 then
	num5 := 5
    elsif num1 = 0 and num = 6 then
	num5 := 6
    elsif num1 = 0 and num = 7 then
	num5 := 7
    elsif num1 = 0 and num = 8 then
	num5 := 8
    elsif num1 = 0 and num = 9 then
	num5 := 9[/code]

This is terrible. I'm not exaggerating. It's horrifying. It makes my eyes bleed. This entire list can be reduced to 2 simple lines.
[code]if num1 = 0 then
    num5 := num[/code]

Is this not so much easier to read? You can do this with like 90% of your code. All of your giant if statements can be reduced to much smaller ones. A very important rule of thumb when programming is 'if you are repeating yourself, you're doing something wrong'. You shouldn't have a dozen if statements that look nearly identical. There is almost always a way to reduce it. Does this solve the problem with your 'clear' button? No, but it will make it far easier for you to fix it, and any other problems you might run into.

There's also a magic command in Turing that makes calculator buttons really, really easy: strint(). strint() takes a string as an argument and outputs that string as an integer. Try this code out:
[code]
var num : int
var word : string
word := "12345"
num := strint (word)
put num[/code]

Can you think of a way to input your numbers using this command instead of the gigantic if statements you have as is?

Finally, I suggest you rename your functions so they are more easily understood. I have no idea what buttons1, buttons1, collab1, etc do. I should be able to read a procedure name and just know what it does. Fix at least this, add some comments,  then post your code (copy & paste it here, don't attach it as a file) and then I'll have a look at your button problem again.

-----------------------------------
ThatTuringNoob
Thu May 07, 2015 7:24 pm

RE:cant seem to get my clear button to work on my calculator
-----------------------------------
ok will do sorry about all those of statement I only really learnt the program last week or so

-----------------------------------
Insectoid
Thu May 07, 2015 7:27 pm

RE:cant seem to get my clear button to work on my calculator
-----------------------------------
Aha, all right, I'll go really easy on you from now on. Don't be discouraged, the learning curve for programming is extremely steep at the beginning. For only having done this a week you're doing well.

-----------------------------------
ThatTuringNoob
Thu May 07, 2015 7:56 pm

RE:cant seem to get my clear button to work on my calculator
-----------------------------------
Ok so I can't seem to figure out how to shorten the giant if statements using that small code you gave me but I did shorten that other lengthy stuff heres the little bit of edited code





setscreen ("graphics:250;310,nobuttonbar")

var text,calc,num1,num2,ans,num,num4,num5,num6: int
text := Font.New ("Times_New_Roman:36")
calc := Font.New ("Impact:16")
var symbol:string := ""
var x, y,button : int 
var ans1:string
var sq: real

%num is the var after num1
%num4 is the var after num2

proc graphic
    Font.Draw ("1", 20, 80, text, red)
    Font.Draw ("2", 80, 80, text, red)
    Font.Draw ("3", 140, 80, text, red)
    Font.Draw ("4", 20, 140, text, red)
    Font.Draw ("5", 80, 140, text, red)
    Font.Draw ("6", 140, 140, text, red)
    Font.Draw ("7", 20, 200, text, red)
    Font.Draw ("8", 80, 200, text, red)
    Font.Draw ("9", 140, 200, text, red)
    Font.Draw ("0", 80, 20, text, red)
    Font.Draw (".", 20, 20, text, red)
    Font.Draw ("C", 20, 260, text, red)
    Font.Draw ("=", 140, 20, text, red)
    Font.Draw ("/", 210, 20, text, red)
    Font.Draw ("*", 205, 65, text, red)
    Font.Draw ("-", 210, 140, text, red)
    Font.Draw ("+", 200, 200, text, red)
    Draw.ThickLine (200, 275, 205, 275, 4, red)
    Draw.ThickLine (205, 275, 210, 260, 4, red)
    Draw.ThickLine (210, 260, 215, 290, 4, red)
    Draw.ThickLine (215, 290, 230, 290, 4, red)

    %line 1 down buttons
    Draw.Box (10, 10, 60, 60, black)
    Draw.Box (10, 70, 60, 120, black)
    Draw.Box (10, 130, 60, 180, black)
    Draw.Box (10, 190, 60, 240, black)
    Draw.Box (10, 250, 60, 300, black)
    %line 2 down buttons
    Draw.Box (70, 10, 120, 60, black)
    Draw.Box (70, 70, 120, 120, black)
    Draw.Box (70, 130, 120, 180, black)
    Draw.Box (70, 190, 120, 240, black)
    %line 3 down buttons
    Draw.Box (130, 10, 180, 60, black)
    Draw.Box (130, 70, 180, 120, black)
    Draw.Box (130, 130, 180, 180, black)
    Draw.Box (130, 190, 180, 240, black)
    %line 4 down buttons
    Draw.Box (190, 10, 240, 60, black)
    Draw.Box (190, 70, 240, 120, black)
    Draw.Box (190, 130, 240, 180, black)
    Draw.Box (190, 190, 240, 240, black)
    Draw.Box (190, 250, 240, 300, black)
    %display screen
    Draw.Box (70, 250, 180, 300, black)
end graphic


proc digit1
    loop
        Mouse.Where(x,y,button) 
        if button = 1 and x>10 and x < 60 and y>70 and y < 120 then
            num1 := 1
            Draw.Text ("1",75,270,calc,red)
            exit
        elsif button = 1 and x>70 and x < 120 and y>70 and y 130 and x < 180 and y>70 and y 10 and x < 60 and y>130 and y < 180 then
            num1 := 4 
            Draw.Text ("4",75,270,calc,red)
            exit
        elsif button = 1 and x>70 and x < 120 and y>130 and y 130 and x < 180 and y>130 and y 10 and x < 60 and y>190 and y < 240 then
            num1 := 7 
            Draw.Text ("7",75,270,calc,red)
            exit
        elsif button = 1 and x>70 and x < 120 and y>190 and y 130 and x < 180 and y>190 and y 70 and x < 120 and y>10 and y 10 and x < 60 and y>70 and y < 120 then
            num := 1
            Draw.Text ("1",88,270,calc,red)
            exit
        elsif button = 1 and x>70 and x < 120 and y>70 and y 130 and x < 180 and y>70 and y 10 and x < 60 and y>130 and y < 180 then
            num := 4 
            Draw.Text ("4",88,270,calc,red)
            exit
        elsif button = 1 and x>70 and x < 120 and y>130 and y 130 and x < 180 and y>130 and y 10 and x < 60 and y>190 and y < 240 then
            num := 7
            Draw.Text ("7",88,270,calc,red)
            exit
        elsif button = 1 and x>70 and x < 120 and y>190 and y 130 and x < 180 and y>190 and y 70 and x < 120 and y>10 and y 10 and x < 10 and y>60 and y 190 and x < 240 and y>130 and y 190 and x < 240 and y>190 and y 190 and x < 240 and y>10 and y 190 and x < 240 and y>70 and y 190 and x < 240 and y>250 and y 10 and x < 60 and y>70 and y < 120 then
            num2 := 1
            Draw.Text ("1",112,270,calc,red)
            exit
        elsif button = 1 and x>70 and x < 120 and y>70 and y 130 and x < 180 and y>70 and y 10 and x < 60 and y>130 and y < 180 then
            num2 := 4 
            Draw.Text ("4",112,270,calc,red)
            exit
        elsif button = 1 and x>70 and x < 120 and y>130 and y 130 and x < 180 and y>130 and y 10 and x < 60 and y>190 and y < 240 then
            num2 := 7
            Draw.Text ("7",112,270,calc,red)
            exit
        elsif button = 1 and x>70 and x < 120 and y>190 and y 130 and x < 180 and y>190 and y 70 and x < 120 and y>10 and y 10 and x < 60 and y>70 and y < 120 then
            num4 := 1
            Draw.Text ("1",125,270,calc,red)
            exit
        elsif button = 1 and x>70 and x < 120 and y>70 and y 130 and x < 180 and y>70 and y 10 and x < 60 and y>130 and y < 180 then
            num4 := 4 
            Draw.Text ("4",125,270,calc,red)
            exit
        elsif button = 1 and x>70 and x < 120 and y>130 and y 130 and x < 180 and y>130 and y 10 and x < 60 and y>190 and y < 240 then
            num4 := 7
            Draw.Text ("7",125,270,calc,red)
            exit
        elsif button = 1 and x>70 and x < 120 and y>190 and y 130 and x < 180 and y>190 and y 70 and x < 120 and y>10 and y 10 and x < 10 and y>60 and y 130 and x < 180 and y>10 and y 