
-----------------------------------
canada123
Sat Jun 03, 2006 11:54 pm

Another problem :/
-----------------------------------
I made a calculator but when I input two numbers during the first time through the loop, it always says "variable has no value", but when i input 3 numbers the first time and then go back to input 2 numbers, the error doesn't appear. Anyone know a solution? Thanks.



-----------------------------------
Mr. T
Sun Jun 04, 2006 12:05 am

Alex's Opinion
-----------------------------------
I could be way off here, but I think the problem is that your trying to change the upper bound of your array (amount), but your array is not flexible. Since there is no third element in your array, amount = 3 returns no value. Go through the Turing Walkthrough to find out how to use flexible arrays.

-----------------------------------
TheOneTrueGod
Sun Jun 04, 2006 8:04 am


-----------------------------------
    
if func = "+" and func2 = "+" and amount = 3 then


This is the line thats giving you the error.  I havn't poured through your code (becuase its long, and I just woke up), but I would assume that func2 is the one that hasn't been declared.  You can test this by using a "put func2" just above that line.  There are several ways you can fix this.

#1) Reorganize your code into two separate, major if statements.  The first one should contain what happens if there were 2 "amount"s, and the second should contain what happens if there were 3.

#2)  C has a handy feature where it ignores everything else if the first statement doesn't work, and all the other things are "ands".  I'm pretty sure Turing does this as well (I havn't extensively tested this, but eh), so if you just reorganize the if statement, a la

    if amount = 3 and func = "+" and func2 = "+" then

the compiler will hit the amount = 3 and realise "hey, this isn't true, and theres only ands in this statement.  Whats the point in continuing checking here?  I'll save the user some run-time and skip the rest of this statement.


Now, a bit more pressing concern of mine is, your program constantly talks about functions, but it doesn't have any.  This would make your program quite a bit easier to manage, as you wouldn't have that huge drawn out if-clause.  In fact, with clever function management, you could whittle it down quite a bit.  Your program also doesn't do order of operations.

An example of what i'm talking about is:

function mathOp (num1:int,op1:char,num2:int) : int
   if op1 = "+" then
      result num1 + num2
   end if
   %you can do the same with the other operations.

   %if theres an error, then...
   result num1
end mathOp
put mathOp(1,"+",3)
%Now, in order to output the answer of three things, you just do:
put mathOp(mathOp(1,"+","5),"-",4)%assuming you add the - part into the function.


Because of the result num1 line, you could just initialize the 'func2' value to something like " " and then allways use the second calling of mathOp that I gave you, and it will only return if func2 is recieved by the user!



Also, what is this:

if amount = 3 and func2 = "+" then
        put "Enter 1 number to be calculated: "
        get num (1)
        put "Enter another: "
        get num (2)
        put "Enter one more: "
        get num (3)
    elsif amount = 3 and func2 = "-" then
        put "Enter 1 number to be calculated: "
        get num (1)
        put "Enter another: "
        get num (2)
        put "Enter one more: "
        get num (3)
    elsif amount = 3 and func2 = "/" then
        put "Enter 1 number to be calculated: "
        get num (1)
        put "Enter another: "
        get num (2)
        put "Enter one more: "
        get num (3)
    elsif amount = 3 and func2 = "x" then
        put "Enter 1 number to be calculated:"
        get num (1)
        put "Enter another: "
        get num (2)
        put "Enter one more: "
        get num (3)
    end if

 :shock: 
All of those do the exact same thing, so why do you need an if statement around em?

Anyways, good luck  :D

-----------------------------------
canada123
Sun Jun 04, 2006 9:06 am


-----------------------------------
Well thanks for the help guys, i finally got it to start working. I would to procedures but i just learned them in class so i am unfamiliar with it. 
Heres the fix version(Still Sucks :/ ):




-----------------------------------
TheOneTrueGod
Sun Jun 04, 2006 9:08 am


-----------------------------------
For future reference, just highlite the section that is code, and press the code button once.  It'll auto-convert the entire thing into a code section for ya
