Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 cant seem to get my clear button to work on my calculator
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ThatTuringNoob




PostPosted: Thu May 07, 2015 6:55 pm   Post subject: 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



Calculator.T
 Description:

Download
 Filename:  Calculator.T
 Filesize:  19.65 KB
 Downloaded:  226 Time(s)


Calculator.T
 Description:
Again sorry for the length

Download
 Filename:  Calculator.T
 Filesize:  19.65 KB
 Downloaded:  190 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Thu May 07, 2015 7:20 pm   Post subject: 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


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


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


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




PostPosted: Thu May 07, 2015 7:24 pm   Post subject: 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




PostPosted: Thu May 07, 2015 7:27 pm   Post subject: 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




PostPosted: Thu May 07, 2015 7:56 pm   Post subject: 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



Turing:


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 <120 then
            num1 := 2
            Draw.Text ("2",75,270,calc,red)
            exit
        elsif button = 1 and x>130 and x < 180 and y>70 and y <120 then
            num1 := 3
            Draw.Text ("3",75,270,calc,red)
            exit
        elsif button = 1 and x>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 <180 then
            num1 := 5
            Draw.Text ("5",75,270,calc,red)
            exit
        elsif button = 1 and x>130 and x < 180 and y>130 and y <180 then
            num1 := 6
            Draw.Text ("6",75,270,calc,red)
            exit
        elsif button = 1 and x>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 <240 then
            num1 := 8
            Draw.Text ("8",75,270,calc,red)
            exit
        elsif button = 1 and x>130 and x < 180 and y>190 and y <240 then
            num1 := 9
            Draw.Text ("9",75,270,calc,red)
            exit
        elsif button = 1 and x>70 and x < 120 and y>10 and y <60 then
            num1 := 0
            Draw.Text ("0",75,270,calc,red)
            exit         
        end if
    end loop
end digit1

procedure digit2
    loop
        Mouse.Where(x,y,button)
        %line 1
        if button = 1 and x>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 <120 then
            num := 2
            Draw.Text ("2",88,270,calc,red)
            exit
        elsif button = 1 and x>130 and x < 180 and y>70 and y <120 then
            num := 3
            Draw.Text ("3",88,270,calc,red)
            exit
        elsif button = 1 and x>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 <180 then
            num := 5
            Draw.Text ("5",88,270,calc,red)
            exit
        elsif button = 1 and x>130 and x < 180 and y>130 and y <180 then
            num := 6
            Draw.Text ("6",88,270,calc,red)
            exit
        elsif button = 1 and x>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 <240 then
            num := 8
            Draw.Text ("8",88,270,calc,red)
            exit
        elsif button = 1 and x>130 and x < 180 and y>190 and y <240 then
            num := 9
            Draw.Text ("9",88,270,calc,red)
            exit
        elsif button = 1 and x>70 and x < 120 and y>10 and y <60 then
            num := 0
            Draw.Text ("0",88,270,calc,red)
        elsif button = 1 and x>10 and x < 10 and y>60 and y <60 then
            num := 90
            Draw.Text (".",100,270,calc,red)
            exit
        end if
    end loop
end digit2

proc signs
    loop
        Mouse.Where (x,y,button)
        if button = 1 and x>190 and x < 240 and y>130 and y <180 then   
            symbol := "-"
            Draw.Text ("-",100,270,calc,red)
            exit
        elsif button = 1 and x>190 and x < 240 and y>190 and y <240 then
            symbol := "+"   
            Draw.Text ("+",100,270,calc,red)
            exit
        elsif button = 1 and x>190 and x < 240 and y>10 and y <60 then
            symbol := "/"
            Draw.Text ("/",100,270,calc,red)
            exit
        elsif button = 1 and x>190 and x < 240 and y>70 and y <120 then
            symbol := "*"
            Draw.Text ("*",100,270,calc,red)
            exit
        elsif button = 1 and x>190 and x < 240 and y>250 and y <300 then
            symbol := "|"
            Draw.ThickLine (110, 275, 115, 275, 4, red)
            Draw.ThickLine (115, 275, 120, 260, 4, red)
            Draw.ThickLine (120, 260, 125, 290, 4, red)
            Draw.ThickLine (125, 290, 135, 290, 4, red)
            exit
        end if
    end loop
end signs

   




procedure digit3
    loop
        Mouse.Where(x,y,button)
        %line 1
        if button = 1 and x>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 <120 then
            num2 := 2
            Draw.Text ("2",112,270,calc,red)
            exit
        elsif button = 1 and x>130 and x < 180 and y>70 and y <120 then
            num2 := 3
            Draw.Text ("3",112,270,calc,red)
            exit
        elsif button = 1 and x>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 <180 then
            num2 := 5
            Draw.Text ("5",112,270,calc,red)
            exit
        elsif button = 1 and x>130 and x < 180 and y>130 and y <180 then
            num2 := 6
            Draw.Text ("6",112,270,calc,red)
            exit
        elsif button = 1 and x>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 <240 then
            num2 := 8
            Draw.Text ("8",112,270,calc,red)
            exit
        elsif button = 1 and x>130 and x < 180 and y>190 and y <240 then
            num2 := 9
            Draw.Text ("9",112,270,calc,red)
            exit
        elsif button = 1 and x>70 and x < 120 and y>10 and y <60 then
            num2 := 0
            Draw.Text ("0",112,270,calc,red)
            exit
        end if
    end loop
end digit3

procedure digit4
    loop
        Mouse.Where(x,y,button)

        if button = 1 and x>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 <120 then
            num4 := 2
            Draw.Text ("2",125,270,calc,red)
            exit
        elsif button = 1 and x>130 and x < 180 and y>70 and y <120 then
            num4 := 3
            Draw.Text ("3",125,270,calc,red)
            exit
        elsif button = 1 and x>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 <180 then
            num4 := 5
            Draw.Text ("5",125,270,calc,red)
            exit
        elsif button = 1 and x>130 and x < 180 and y>130 and y <180 then
            num4 := 6
            Draw.Text ("6",125,270,calc,red)
            exit
        elsif button = 1 and x>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 <240 then
            num4 := 8
            Draw.Text ("8",125,270,calc,red)
            exit
        elsif button = 1 and x>130 and x < 180 and y>190 and y <240 then
            num4 := 9
            Draw.Text ("9",125,270,calc,red)
            exit
        elsif button = 1 and x>70 and x < 120 and y>10 and y <60 then
            num4 := 0
            Draw.Text ("0",125,270,calc,red)
        elsif button = 1 and x>10 and x < 10 and y>60 and y <60 then
            num4 := 90
            Draw.Text (".",125,270,calc,red)
            exit
        end if
    end loop
end digit4



%shortened the eye bleeding code here thanks for the tip just had to use my head a bit as well as, re lable the procedures to be more clear
proc doubledigitset2
    if num1 = 0 then
        num5 := num
    elsif num1 = 1 then
        num5 := 10+num
    elsif num1 = 2 then
        num5 := 20+num
    elsif num1 = 3 then
        num5 := 30+num
    elsif num1 = 4 then
        num5 := 30+num
    elsif num1 = 5 then
        num5 := 50+num
    elsif num1 = 6 then
        num5 := 60+num
    elsif num1 = 7 then
        num5 := 70+num
    elsif num1 = 8 then
        num5 := 80+num
    elsif num1 = 9 then
        num5 := 90+num
    end if
end doubledigitset1
   
   

proc doubledigitset2
    if num2 = 0 then
        num6 := num4
    elsif num2 = 1 then
        num6 := 10+num4
    elsif num2 = 2 then
        num6 := 20+num4
    elsif num2 = 3 then
        num6 := 30+num4
    elsif num2 = 4 then
        num6 := 30+num4
    elsif num2 = 5 then
        num6 := 50+num4
    elsif num2 = 6 then
        num6 := 60+num4
    elsif num2 = 7 then
        num6 := 70+num4
    elsif num2 = 8 then
        num6 := 80+num4
    elsif num2 = 9 then
        num6 := 90+num4
    end if
end doubledigitset2

% I believe that this is good
proc math
    if symbol = "-" then
        ans:= num5 - num6
    elsif symbol = "+" then
        ans:= num5 + num6
    elsif symbol = "*" then
        ans:= num5 * num6
    elsif symbol = "/" then
        ans:= num5 div num6
    end if
end math

%and this is shortened
proc equal
    loop
        Mouse.Where (x,y,button)
        if button = 1 and x>130 and x < 180 and y>10 and y <60 then
            Draw.Text ("=",140,270,calc,red)
            delay (250)
            Draw.FillBox (71, 251, 179, 299, white)
            Draw.Text (intstr (ans),100,270,calc,blue)
        end if
    end loop
end equal

proc squareroot
    if symbol = "|" then
        sq := num5**0.5
        Draw.FillBox (71, 251, 179, 299, white)
        Draw.Text (realstr (sq,5),75,270,calc,blue)   
    end if
end squareroot


loop
    graphic
    digit1
    delay (250)
    digit2
    delay (250)
    signs
    delay (250)
    doubledigitset1
    squareroot
    digit3
    delay (100)
    digit4
    delay (250)
    doubledigitset2
    math
    equal

end loop


Insectoid




PostPosted: Thu May 07, 2015 8:58 pm   Post subject: RE:cant seem to get my clear button to work on my calculator

code:
proc doubledigitset2
    if num2 = 0 then
        num6 := num4
    elsif num2 = 1 then
        num6 := 10+num4
    elsif num2 = 2 then
        num6 := 20+num4
    elsif num2 = 3 then
        num6 := 30+num4
    elsif num2 = 4 then
        num6 := 30+num4
    elsif num2 = 5 then
        num6 := 50+num4
    elsif num2 = 6 then
        num6 := 60+num4
    elsif num2 = 7 then
        num6 := 70+num4
    elsif num2 = 8 then
        num6 := 80+num4
    elsif num2 = 9 then
        num6 := 90+num4
    end if
end doubledigitset2


If you take a look at this, you'll notice there's a bug- you do 30+num4 twice instead of having a 40+num4. You will never notice this bug unless you do a fair bit of testing, and it happened because this bit of code is so long and clumsy. Remember, if you're repeating yourself, you're doing something wrong. We can shorten this like so:
code:
num6 := (num6*10) + num4


Now we don't even have an if statement. We just used basic math to do all the work for us.

Your button code can't be shortened much without getting into more complicated stuff that you shouldn't worry about yet, but it can be shortened. Notice that some of the buttons share coordinates- all the top buttons have the same y coordinates, as do all the middle ones, and all the bottom ones. You can put if statements inside other if statements, like so:

code:
if <condition a> then
    if <condition b> then
        <do something>
    end if
end if


This will be useful for shortening your button code.


Your function names are a little better but can still use some work. Keep in mind that a function isn't something, it does something. What does your function do? Does it get the first digit? Then call it 'getDigitOne'. Does it draw the calculator? Then call it 'DrawCalculator'. You'll get it with practise. Variables, on the other hand, are something, and your variable names should describe what they are. I don't know what num5 or num6 are. Variable names don't have to be 3 or 4 letters long- they can be as long as they need to be. 'operand1Digit1' is totally acceptable and instantly tells you what it is- it's the first digit of the first operand (operands are the numbers that operators operate on, like in 5+3, 5 and 3 are operands).
ThatTuringNoob




PostPosted: Thu May 07, 2015 9:39 pm   Post subject: RE:cant seem to get my clear button to work on my calculator

Thanks for the great tips so far and how to shorten codes very much I will be sure to use all of them! However I still am a bit lost for the clear button, I can't seem to wrap my head around how to reset the variables (or if I even need too) and have the program restart at the click of that button
Insectoid




PostPosted: Thu May 07, 2015 9:44 pm   Post subject: RE:cant seem to get my clear button to work on my calculator

To 'reset' the program, you need to re-initialize every variable to its original value and go back to the top of the program. It looks like the last thing your program does is run the equals procedure. When the equals procedure finishes, the code will resume at the top of your main loop, with the graphic procedure. But does your equals procedure ever finish?
Sponsor
Sponsor
Sponsor
sponsor
ThatTuringNoob




PostPosted: Thu May 07, 2015 9:54 pm   Post subject: RE:cant seem to get my clear button to work on my calculator

Never would of caught that, thanks for the help very much appreciate it. If I run into any other problems ill just post again on the thread probably tomorrow if I need any more help
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: