
-----------------------------------
Wajih
Thu Jan 13, 2011 6:10 pm

Calculator Questions
-----------------------------------
What is it you are trying to achieve?
I am trying to create a basic calculator.

What is the problem you are having?
Hey guys, thanks to your help, i have created a basic calculator. it still has a few bugs that i would like to fix.

1) Right now, the calculator can only do single digit operations. like 6 /8 = 0.75. What should i do so that calculator can recognize i entered the number 123 instead of 1, then clearing the number, 2 then clearing the number, and then 3, then clearing the number.

2) I need help with a part of the program that clears a certain parameter of data when a certain button is pressed. The creating the button is easy, but how do you make it so when the button is clicked it only clears a certain part of the screen. Is that even possible?

3) For my calculator, you have to enter the two numbers then the operator to do the  calculations. How do you make it so that you have to enter the first number, then the operator, and then the second number to do the calculations.

For 1) my teacher told me that i have to do some sort of storage technique but it was a bit confusing. can you guys elaborate?
 

Describe what you have tried to solve this problem
i tried creating a new variable to store the answer, but it did not work.

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
available upon request

Please specify what version of Turing you are using
4.1.1

-----------------------------------
Tony
Thu Jan 13, 2011 6:35 pm

RE:Calculator Questions
-----------------------------------
1 and 3 are the same thing, and your teacher is right -- you need to keep track of what state you are in right now. For example, "entering the first number"; while in this state, every digit pressed gets added to the first number being entered.

2 -- clearing means drawing something new on top. You can draw something new in just one part of the screen.

-----------------------------------
Wajih
Thu Jan 13, 2011 6:42 pm

RE:Calculator Questions
-----------------------------------
1 and 3-but my calculator has a button type interface, so if i enter a number, it will only show that number. and by state, you mean creating a new variable that keeps track if it is in this state or not?

2- but i want to clear only a certain paramters of the code x and y, coordinates.  oh i get it, do i have to do something like if this button is clicked, cls this part of the screen?

-----------------------------------
Tony
Thu Jan 13, 2011 6:46 pm

RE:Calculator Questions
-----------------------------------
yes and yes.

-----------------------------------
Wajih
Thu Jan 13, 2011 7:04 pm

RE:Calculator Questions
-----------------------------------
1 and 3- kk i get it. so it will be like 

if state = 1 then 
so and so 
else do the single digit operation.

20 its clearer to me but do i have to be like 

if button = 1 and so and so then 
cls ( x1, y1, x2, y2)
end if. 

that doesnt seem right because i think the cls will clear the whole screen instead of the certain parameters

-----------------------------------
Wajih
Thu Jan 13, 2011 7:06 pm

RE:Calculator Questions
-----------------------------------
2-*

-----------------------------------
Tony
Thu Jan 13, 2011 7:15 pm

RE:Calculator Questions
-----------------------------------
you can read the build in documentation or our copy of it here -- [tdoc]cls[/tdoc]

-----------------------------------
Wajih
Thu Jan 13, 2011 7:19 pm

RE:Calculator Questions
-----------------------------------
alright i read it, but how do i change the row and column?

-----------------------------------
Tony
Thu Jan 13, 2011 7:25 pm

RE:Calculator Questions
-----------------------------------
of the cursor? locate(r,c)

Did you read this part?

The entire output window is set to the current text background color


Hint: cls is probably not what you are looking for.

-----------------------------------
Wajih
Thu Jan 13, 2011 7:29 pm

RE:Calculator Questions
-----------------------------------
oh so the if i do cls, it clears the entire thing since the background is white. do you know where to find what i am looking for? erase, delete?

-----------------------------------
Tony
Thu Jan 13, 2011 7:38 pm

Re: RE:Calculator Questions
-----------------------------------

2 -- clearing means drawing something new on top. You can draw something new in just one part of the screen.

-----------------------------------
Wajih
Thu Jan 13, 2011 7:39 pm

RE:Calculator Questions
-----------------------------------
never mind man i just solved the 2nd one :D. ok for the first question, i just created a state variable,   
and if it 0 then it will do single digit operations. if state is 1, it will do triple or digit operations. but how? do i have to put num, num? or num, num2?

-----------------------------------
Tony
Thu Jan 13, 2011 7:43 pm

RE:Calculator Questions
-----------------------------------
You have clicked the first number. You have a copy of that single digit in num1, and you are in a state that describes this situation.

You click on another digit. What needs to happen to num1 and what needs to happen to the state?

How about if you click on an operator, such as "+"?

-----------------------------------
Wajih
Thu Jan 13, 2011 7:52 pm

RE:Calculator Questions
-----------------------------------
so i am in state = 1. 
num1 becomes the first number after the second digit i click.  if state = 1 then i should add to the num1?

-----------------------------------
Tony
Thu Jan 13, 2011 8:06 pm

RE:Calculator Questions
-----------------------------------
if state = 1 describes a situation where you are "still entering num1", then yes, the second digit needs to be included.

The tricky parts are the transitions. How do you get to state = 2 and what does it mean? ;)

-----------------------------------
Wajih
Thu Jan 13, 2011 8:22 pm

RE:Calculator Questions
-----------------------------------
why would you get to state = 2?

doesnt state = 1 include all the operations for double, triple digit operations, etc? or for each additional digit you have to go to another state? if stage = 1, then how would output lets say 23?

-----------------------------------
Tony
Thu Jan 13, 2011 8:31 pm

RE:Calculator Questions
-----------------------------------
As you are entering the first number, do you need different states for having already entered 1, 2, or 3 digits? If so, how are they different?

-----------------------------------
Wajih
Thu Jan 13, 2011 8:40 pm

RE:Calculator Questions
-----------------------------------
im thinking you can add as many numbers as you want until you hit the second set of numbers

-----------------------------------
Tony
Thu Jan 13, 2011 9:18 pm

RE:Calculator Questions
-----------------------------------
Indeed. As such, you need just a few general states.

-----------------------------------
Wajih
Thu Jan 13, 2011 9:49 pm

RE:Calculator Questions
-----------------------------------
im sorry, i dont understand what you are trying to say. so if its a 6 digit number it should be in state 5? but first, if its in state = 1, how would you output a number?

-----------------------------------
Tony
Thu Jan 13, 2011 9:59 pm

Re: RE:Calculator Questions
-----------------------------------
so if its a 6 digit number it should be in state 5?
im thinking you can add as many numbers as you want until you hit the second set of numbers

-----------------------------------
Wajih
Thu Jan 13, 2011 10:20 pm

RE:Calculator Questions
-----------------------------------
oh i get what you are saying now. but for displaying the number that you want, how do you do it? do you add num and num to get num?

-----------------------------------
Tony
Thu Jan 13, 2011 10:26 pm

RE:Calculator Questions
-----------------------------------
Yes, almost. But if the first digit is 1 and second is 2, you want to get 1&2=12, not 1+2=3.

-----------------------------------
Wajih
Thu Jan 13, 2011 10:43 pm

RE:Calculator Questions
-----------------------------------
then how would you do something like that? would you add to num, multiply it? i will send you a part of my code : 

 if (button = 1) and (x >= 137) and (x = 142) and (y = 194) and (x = 142) and (y = 196) and (x = 110) and (y = 137) and (x = 142) and (y = 194) and (x = 142) and (y = 251) and (x = 142) and (y = 137) and (x = 190) and (y = 194) and (x = 190) and (y = 251) and (x = 190) and (y = 137) and (x = 241) and (y = 196) and (x < 210) and (y > 237) and (y < 259) then
            locate (5, 19)
            num := 8
            put num ..
        end if
 

        if (button = 1) and (x >= 251) and (x = 241) and (y = 440) and (x = 187) and (y = 305) and (x = 140) and (y = 345) and (x = 140) and (y = 386) and (x = 140) and (y = 305) and (x = 190) and (y = 345) and (x = 190) and (y = 386) and (x = 190) and (y = 305) and (x = 238) and (y = 345) and (x = 238) and (y = 386) and (x = 238) and (y = 172) and (x = 273) and (y = 220) and (x = 273) and (y = 323) and (x = 271) and (y = 370) and (x = 271) and (y = 426) and (x = 137) and (y  Lets fix this yes yes?

-----------------------------------
Wajih
Fri Jan 14, 2011 11:22 pm

RE:Calculator Questions
-----------------------------------
im not sure if you're being serious or not lol, no offense, but would arrays work in this case?

-----------------------------------
Tony
Fri Jan 14, 2011 11:33 pm

RE:Calculator Questions
-----------------------------------
I am assuming the the coordinates of any particular button can be figured out based on its index, so maybe just a for-loop instead?

-----------------------------------
Wajih
Sat Jan 15, 2011 12:02 am

RE:Calculator Questions
-----------------------------------
wouldnt you have to make up a nex variable then?

cant just do like this:

for i : 0 ...1000000
end for

or something like that

-----------------------------------
Wajih
Sat Jan 15, 2011 4:41 pm

RE:Calculator Questions
-----------------------------------
hold up. my two sets of numbers are in the same loop. do i have to make num in one loop and num2 in another, then loop them ?

right now it is like this:

loop
num 
num2 
end loop

but should it be like this? :

loop 
loop
num 
end loop
loop 
num2
end loop
end loop

-----------------------------------
Tony
Sat Jan 15, 2011 7:23 pm

RE:Calculator Questions
-----------------------------------
In the second version, you'd be looping as you are entering the first number, end when you are done, then loop while entering the second number.

That sounds like a right thing to do.

-----------------------------------
Wajih
Sat Jan 15, 2011 8:42 pm

RE:Calculator Questions
-----------------------------------
the second state? or the second version of the loop that i posted?

-----------------------------------
Tony
Sat Jan 15, 2011 8:45 pm

RE:Calculator Questions
-----------------------------------
The second version of the loop. You'd need to keep track of your states to figure out when to exit one loop and enter into another.

-----------------------------------
Wajih
Sat Jan 15, 2011 9:26 pm

RE:Calculator Questions
-----------------------------------
alright but how many view updates would you have and where wouldyou put them in the loops?

-----------------------------------
Tony
Sat Jan 15, 2011 9:30 pm

RE:Calculator Questions
-----------------------------------
Think about what View.Update does, that should lead towards the answer (or to the question that should lead towards the answer ;)). There are also examples in documentation -- [tdoc]View.Update[/tdoc]

-----------------------------------
Wajih
Sat Jan 15, 2011 9:46 pm

RE:Calculator Questions
-----------------------------------
so there should be 3?

because there are 3 loops and each loop is drawing the calculator?

-----------------------------------
Tony
Sat Jan 15, 2011 9:50 pm

RE:Calculator Questions
-----------------------------------
That sounds correct.

-----------------------------------
Wajih
Sat Jan 15, 2011 10:05 pm

RE:Calculator Questions
-----------------------------------
i did that but when i tried to press any second set of buttons, they would not work, any reasons why?

-----------------------------------
Tony
Sat Jan 15, 2011 10:14 pm

RE:Calculator Questions
-----------------------------------
Perhaps the code flow is not where you expect it to be, or the values of mouse variables don't line up with the expected if conditionals. Trace through your program, and try to figure out exactly what's going on at every step of it.

-----------------------------------
Wajih
Sat Jan 15, 2011 10:27 pm

RE:Calculator Questions
-----------------------------------
but when i put it all in one loop, both sets of numbers work, why is that?

and do you know how to shorten string catenation generated by a string

-----------------------------------
Tony
Sat Jan 15, 2011 10:56 pm

Re: RE:Calculator Questions
-----------------------------------
but when i put it all in one loop, both sets of numbers work, why is that?

When you randomly put things in and out of code, there's a discrepancy between what you are telling the computer to do and what you understand about the things that you tell the computer to do. The difference between the two is the cause for a majority of problems with running programs.


and do you know how to shorten string catenation generated by a string
I don't understand this question.

-----------------------------------
Wajih
Sat Jan 15, 2011 11:09 pm

RE:Calculator Questions
-----------------------------------
but i am doing the same thing that you told me. 

i put 

loop
view update
draw stuff
loop
viewupdate
drawstuff
num
end loop
loop
viewupdate
drawstuff
num2
end loop
end loop

i did that. it recognized the first number and the operator, but it did not recognize the second number. 

also, is there a command similar to locate?

-----------------------------------
Tony
Sat Jan 15, 2011 11:49 pm

RE:Calculator Questions
-----------------------------------
I also said to read the documentation on View.Update (and the example provided there). Things you draw will not show up on screen until View.Update is called (think about the order of drawing stuff, and telling it to show up).

Also, unless there's a lot of "flashing" happening, you don't really need offscreenonly mode.

Documentation for [tdoc]locate[/tdoc] might list related functions. Is there something in particular you are looking for?

-----------------------------------
Wajih
Sun Jan 16, 2011 12:07 am

RE:Calculator Questions
-----------------------------------
exactly bro, i read it. i out the view update at the bottom of the loops AFTER  it did all the drawing stuff, but then the whole screen went blank and i couldnt do anything with it.   

and there is a lot of flashing going on which is why i am using offscreenonly

yes, there is. 

i made up a button and a variable. 

once something is multiplied, or divided, or added, or subtracted

i store it in a variable. then i "locate"
that variable onto a certain row and column. 

there is a button that when it is pressed, it is supposed to clear what i have typed and set the answer back to 0. 

but if try pressing that button , only the numbers that i did the operations with get deleted, not the answer. is there anything related that will cls of the answer and set the answer back to 0?

-----------------------------------
Tony
Sun Jan 16, 2011 12:17 am

RE:Calculator Questions
-----------------------------------
if you use something like
[code]
locate(row, column)
put ""
[/code]
That will clear (draw on top of) the entire row.

-----------------------------------
Wajih
Sun Jan 16, 2011 1:16 pm

RE:Calculator Questions
-----------------------------------
i got that too work but i still do not know how to do double digit operations.

-----------------------------------
Tony
Sun Jan 16, 2011 1:37 pm

RE:Calculator Questions
-----------------------------------
Maybe ask huskiesgoaler34? http://compsci.ca/v3/viewtopic.php?t=27249
