Computer Science Canada cant seem to get my clear button to work on my calculator |
Author: | ThatTuringNoob [ 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 |
Author: | Insectoid [ 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:
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.
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:
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. |
Author: | ThatTuringNoob [ 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 |
Author: | Insectoid [ 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. |
Author: | ThatTuringNoob [ 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
|
Author: | Insectoid [ Thu May 07, 2015 8:58 pm ] | ||||||
Post subject: | RE:cant seem to get my clear button to work on my calculator | ||||||
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:
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:
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). |
Author: | ThatTuringNoob [ 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 |
Author: | Insectoid [ 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? |
Author: | ThatTuringNoob [ 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 |