12-02-2017 01:15 PM
Hello,
In the attached VI of simple calculator, I want to retain the first-operand data even when I press the operator button (=,-,*,/) multiple times or when i want to change the operator. How can I do it?
Any help is appreciated.
Thanks.
Solved! Go to Solution.
12-02-2017 08:21 PM
Which data are you talking about?
One thing I see is an unnamed string indicator. (Show the label and give it a name!) That gets its value from the tunnel of the event structure. But it only gets data in one case. Because the tunnel is set to Use Default if Unwired, it gets default data, an empty string, when any other event case runs.
12-02-2017 10:53 PM
Hey Ravens,
Thanks for the reply.
Please ignore the empty string. I should have removed it. My question is, if I perform this operation say 21-3 I get the desired result. But, if I do this, 21--3 (If I press the operator twice, the first operand '21' is lost and I understand why it will be lost.) I would like to retain the first operand even when i press the operator (--) twice. I hope you got my question.
Thanks again.
12-03-2017 12:09 AM
Remember what the last button was by storing it in a shift register. If you detect a new button is an operator that matches the one in shift register, you know you duplicated the button and ignore it.
12-04-2017 03:15 AM
What if you want to calculate 21-(-3)?
/Y
12-04-2017 03:26 AM
Hello Yamaeda,
Nope. Not 21-(-3). I still want to calculate 21-3 but I press the - button twice/thrice or whatever. In our phyical calculator or windows calculator if you do like 21-----3 it is still 21-3 but in this code if I press the - button second time, my first operand (21) is lost and I understand why it will be lost but I'm thinking of a way to retain it even though I press - button multiple times.
Thanks.
12-04-2017 03:37 AM
Hi Ganny,
I understand why it will be lost but I'm thinking of a way to retain it even though I press - button multiple times.
Think of a state machine:
1. state: input of first numeric operand (as long as the user inputs numeric chars)
2. state: input of operation (as long as the user inputs an "operation" char like +-*/)
3. state: input of 2nd numeric operand (as long as the user inputs numeric chars) (might even be the same as 1st state)
4. state: iput of "=" or "enter" (or similar): solve operation from stored inputs
Each state from 1 to 3 will store its input in a separate shift register…
Sometimes it really helps to develop an algorithm on a sheet of paper - BEFORE starting to code…
12-04-2017 03:42 AM
Hello GerdW,
Thanks for the valuable suggestion - to write the algorithm before starting the code. I should start practicing it.
By the way, I will try it with State Machines. But, is there any way to get it done from this code?
12-04-2017 03:47 AM - edited 12-04-2017 03:49 AM
Hi Ganny,
But, is there any way to get it done from this code?
When you mean your VI from your first message with "this": sure!
(Btw. what's the purpose of a wait function in parallel to your loop?)
All you need to do is to implement all those valuable suggestions you got so far…
12-04-2017 04:08 AM
Hello GerdW,
That Wait Function don't have any purpose. I just didn't see it's there. I removed it.
By the way, I still don't understand how I can modify the code to retain the first operand in case of multiple - sign presses.
Can you throw some light on it for me?
Thanks