Another problem :/
Author |
Message |
canada123
|
Posted: Sat Jun 03, 2006 11:54 pm Post subject: 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.
[/code]var amount : int
var func : string
var func2 : string
var c : string (1) := ""
loop
%-----------------GET INFO-------------------
put "The Max set of numbers allowed to be calculated is 3 and 2 Functions."
put "How many sets of numbers will you be inputting?" ..
get amount
if amount = 3 then
put "Whats the 1st function? i.e: +, -, x, / :" ..
get func
put "Whats 2nd function? i.e: +, -, x, / :" ..
get func2
end if
if amount = 2 then
put "Enter a function. i.e: +, -, x, / :" ..
get func
end if
%-----------------Variables------------------------
var num : array 1 .. amount of real
num (1) := 0
num (2) := 0
%------------1 FUNCTIONS---------------------
if amount = 2 and func = "+" then
put "Enter 1 number to be calculated: "
get num (1)
put "Enter another"
get num (2)
elsif amount = 2 and func = "-" then
put "Enter 1 number to be calculated: "
get num (1)
put "Enter another"
get num (2)
elsif amount = 2 and func = "/" then
put "Enter 1 number to be calculated: "
get num (1)
put "Enter another"
get num (2)
elsif amount = 2 and func = "x" then
put "Enter 1 number to be calculated: "
get num (1)
put "Enter another: "
get num (2)
end if
if amount = 2 and func = "+" then
put "Therefore:", num (1), func, num (2), "=", num (1) + num (2)
elsif amount = 2 and func = "-" then
put "Therefore:", num (1), func, num (2), "=", num (1) - num (2)
elsif amount = 2 and func = "/" then
put "Therefore:", num (1), func, num (2), "=", num (1) / num (2)
elsif amount = 2 and func = "x" then
put "Therefore:", num (1), func, num (2), "=", num (1) * num (2)
end if
%-----------------2 FUNCTIONS-----------------
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
if func = "+" and func2 = "+" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) + num (2) + num (3)
elsif func = "+" and func2 = "-" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) + num (2) - num (3)
elsif func = "+" and func2 = "/" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) + num (2) / num (3)
elsif func = "+" and func2 = "x" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) + num (2) * num (3)
elsif func = "-" and func2 = "-" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) - num (2) - num (3)
elsif func = "-" and func2 = "+" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) - num (2) + num (3)
elsif func = "-" and func2 = "/" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) - num (2) / num (3)
elsif func = "-" and func2 = "x" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) - num (2) * num (3)
elsif func = "/" and func2 = "/" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) / num (2) / num (3)
elsif func = "/" and func2 = "+" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) / num (2) + num (3)
elsif func = "/" and func2 = "-" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) / num (2) - num (3)
elsif func = "/" and func2 = "x" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) / num (2) * num (3)
elsif func = "x" and func2 = "x" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) * num (2) * num (3)
elsif func = "x" and func2 = "+" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) * num (2) + num (3)
elsif func = "x" and func2 = "-" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) * num (2) - num (3)
elsif func = "x" and func2 = "/" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) * num (2) / num (3)
end if
%-----------------RESTART---------------------
put "To Clear and resart, Press any key" ..
getch (c)
cls
end loop
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Mr. T
![](http://www.madpaintballer.com/images/purepwnage.gif)
|
Posted: Sun Jun 04, 2006 12:05 am Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
TheOneTrueGod
![](http://www.drmcninja.com/images/mcninjab3.jpg)
|
Posted: Sun Jun 04, 2006 8:04 am Post subject: (No subject) |
|
|
code: |
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
code: |
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:
code: |
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:
code: |
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
|
All of those do the exact same thing, so why do you need an if statement around em?
Anyways, good luck ![Very Happy Very Happy](images/smiles/icon_biggrin.gif) |
|
|
|
|
![](images/spacer.gif) |
canada123
|
Posted: Sun Jun 04, 2006 9:06 am Post subject: (No subject) |
|
|
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 :/ ):
[/code]var amount : int
var func : string
var func2 : string
var c : string (1) := ""
loop
%-----------------GET INFO-------------------
put "The Max set of numbers allowed to be calculated is 3 and 2 Functions."
put "How many sets of numbers will you be inputting?" ..
get amount
if amount = 3 then
put "Whats the 1st function? i.e: +, -, x, / :" ..
get func
put "Whats 2nd function? i.e: +, -, x, / :" ..
get func2
end if
if amount = 2 then
put "Enter a function. i.e: +, -, x, / :" ..
get func
end if
%-----------------Variables------------------------
var num : array 1 .. amount of real
num (1) := 0
num (2) := 0
%------------1 - 2 FUNCTIONS---------------------
if amount = 2 and func = "+" then
put "Enter 1 number to be calculated: "
get num (1)
put "Enter another"
get num (2)
elsif amount = 2 and func = "-" then
put "Enter 1 number to be calculated: "
get num (1)
put "Enter another"
get num (2)
elsif amount = 2 and func = "/" then
put "Enter 1 number to be calculated: "
get num (1)
put "Enter another"
get num (2)
elsif amount = 2 and func = "x" then
put "Enter 1 number to be calculated: "
get num (1)
put "Enter another: "
get num (2)
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 = "/" 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
%------------Calculations---------------------
if amount = 2 and func = "+" then
put "Therefore:", num (1), func, num (2), "=", num (1) + num (2)
elsif amount = 2 and func = "-" then
put "Therefore:", num (1), func, num (2), "=", num (1) - num (2)
elsif amount = 2 and func = "/" then
put "Therefore:", num (1), func, num (2), "=", num (1) / num (2)
elsif amount = 2 and func = "x" then
put "Therefore:", num (1), func, num (2), "=", num (1) * num (2)
elsif func = "+" and func2 = "+" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) + num (2) + num (3)
elsif func = "+" and func2 = "-" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) + num (2) - num (3)
elsif func = "+" and func2 = "/" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) + num (2) / num (3)
elsif func = "+" and func2 = "x" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) + num (2) * num (3)
elsif func = "-" and func2 = "-" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) - num (2) - num (3)
elsif func = "-" and func2 = "+" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) - num (2) + num (3)
elsif func = "-" and func2 = "/" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) - num (2) / num (3)
elsif func = "-" and func2 = "x" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) - num (2) * num (3)
elsif func = "/" and func2 = "/" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) / num (2) / num (3)
elsif func = "/" and func2 = "+" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) / num (2) + num (3)
elsif func = "/" and func2 = "-" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) / num (2) - num (3)
elsif func = "/" and func2 = "x" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) / num (2) * num (3)
elsif func = "x" and func2 = "x" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) * num (2) * num (3)
elsif func = "x" and func2 = "+" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) * num (2) + num (3)
elsif func = "x" and func2 = "-" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) * num (2) - num (3)
elsif func = "x" and func2 = "/" and amount = 3 then
put "Therefore:", num (1), func, num (2), func2, num (3), "=", num (1) * num (2) / num (3)
end if
%-----------------RESTART---------------------
put "To Clear and resart, Press any key" ..
getch (c)
cls
end loop
|
|
|
|
|
![](images/spacer.gif) |
TheOneTrueGod
![](http://www.drmcninja.com/images/mcninjab3.jpg)
|
Posted: Sun Jun 04, 2006 9:08 am Post subject: (No subject) |
|
|
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 |
|
|
|
|
![](images/spacer.gif) |
|
|