Computer Science Canada

Trouble compiling program.

Author:  darknoob [ Wed Mar 29, 2006 8:25 pm ]
Post subject:  Trouble compiling program.

code:

var number : string
var located : int := 1
var intnumber : int :=0
var realnumber : real :=0
var rea11 : int := 0
var decimal : int := 0
var add : int := 0

get number

for i : 1 .. length (number)
if ord (number (i)) = 46 then
decimal := i
rea11 := 1
end if
end for

if rea11 = 1 then
if length (number) = 3 then
if decimal = 1 then
located :=2
realnumber := realnumber + ((ord (number ( located )) -48 / 10)

located :=3
realnumber := realnumber + ((ord (number ( located )) -48) / 100)
elsif decimal =2 then
located :=1
realnumber :=realnumber + ((ord (number ( located )) -48
located :=3
realnumber := realnumber + ((ord (number ( located )) -48 / 10))

elsif decimal =3 then
located :=1
realnumber := realnumber + ((ord (number ( located )) -48) * 10)
located :=2
realnumber := realnumber + ((ord (number ( located )) -48))
end if
end if

put realnumber
else
if length (number) = 1 then
located := 1
intnumber := intnumber + ord (number (located)) -48
elsif length (number) =2 then
located :=1
intnumber := intnumber + ord (number (located)) -48
elsif length (number) = 2 then
located :=1
intnumber := intnumber + ((ord (number (located)) -48) * 10)
located := 2
intnumber := intnumber + ((ord (number (located)) -48))
elsif length (number) = 3 then
located :=1
intnumber := intnumber + ((ord (number (located)) -48) * 100)
located :=2
intnumber := intnumber + ((ord (number (located)) - 48) * 10)
located :=3
intnumber := intnumber + ((ord(number (located)) -48))
elsif length (number) = 4 then
located :=1
intnumber :=intnumber + ((ord(number (located)) -48) *1000)
located :=2
intnumber :=intnumber + ((ord(number (located)) -48) *100)
located :=3
intnumber :=intnumber + ((ord(number (located)) -48) *10)
located :=4
intnumber :=intnumber + ((ord(number (located)) -48))
end if
put intnumber
end if

Author:  darknoob [ Wed Mar 29, 2006 8:26 pm ]
Post subject: 

i donno what is wrong, every time i try to run it it always say error on 'located'

Author:  HellblazerX [ Wed Mar 29, 2006 8:31 pm ]
Post subject: 

the problem is the variable located, but the line before that

for the line
code:
realnumber := realnumber + ((ord (number (located)) - 48 / 10)

you missed a bracket at the end. change it to:
code:
realnumber := realnumber + ((ord (number (located)) - 48 / 10))

and for the line
code:
realnumber := realnumber + ((ord (number (located)) - 48

you're missing 2 brackets, so change it to:
code:
realnumber := realnumber + ((ord (number (located)) - 48))

it should work after that.

Author:  darknoob [ Wed Mar 29, 2006 8:36 pm ]
Post subject: 

hmm...but what should i do?
when i run the program, everything (for example 45+44) it always turn out to be 0
what should i do??

Author:  person [ Wed Mar 29, 2006 8:39 pm ]
Post subject: 

a) indent ur code
b) this is almost the exact same code as: http://www.compsci.ca/v2/viewtopic.php?t=11677
c) y dont u read the error message turing gives u?
d) tell us wat ur program is supposed to do

Author:  HellblazerX [ Wed Mar 29, 2006 8:40 pm ]
Post subject: 

wut is your program suppose to do?

Author:  darknoob [ Wed Mar 29, 2006 8:43 pm ]
Post subject: 

hmm...i know that guy!!!0_o
message says unnamed #1 finished execution, nothing about what is wrong
the run window says this when i try to run it
code:

35+45
0

Author:  darknoob [ Wed Mar 29, 2006 8:45 pm ]
Post subject: 

the program is suppose to read and decode a simple mathematical expression given as a string

Author:  darknoob [ Wed Mar 29, 2006 8:54 pm ]
Post subject: 

i still get 0 after i changed all the stuff

Author:  HellblazerX [ Wed Mar 29, 2006 8:54 pm ]
Post subject: 

in that case, why don't you just use the strint function to convert the String into an int, and perform the operations on the int, instead of getting the ASCII value of the number, and working with those.

Author:  darknoob [ Wed Mar 29, 2006 8:56 pm ]
Post subject: 

hmm...i'm really new at programing >_< just started this year

Author:  person [ Wed Mar 29, 2006 9:06 pm ]
Post subject: 

Try using the debugger menu to find whats wrong

Author:  darknoob [ Wed Mar 29, 2006 9:09 pm ]
Post subject: 

i told u i'm really new to this turing thing and i dont know anything about it, just want to pass computer science and move on @_@
how u use the debug menu...T-T

Author:  darknoob [ Wed Mar 29, 2006 9:12 pm ]
Post subject: 

it says i got error on the second last line...at least that is what i think it says -_-

Author:  HellblazerX [ Wed Mar 29, 2006 9:19 pm ]
Post subject: 

try this code instead:

code:
var num1 : real
var num2 : real
var operator : string
var number : string

get number

for i : 1 .. length (number)
    if strintok (number (i)) = false and ord (number (i)) not= 46 then
        num1 := strreal (number (1 .. (i - 1)))
        operator := number (i)
        num2 := strreal (number ((i + 1) .. *))
    end if
end for

if ord (operator) = 43 then
    put num1 + num2
elsif ord (operator) = 45 then
    put num1 - num2
elsif ord (operator) = 42 then
    put num1 * num2
elsif ord (operator) = 47 then
    put num1 / num2
end if

basically, this code takes in the String input, and divides the String up based on where the operator is, the substring that cannot be converted to int. The first half is the first number, and the second half is the second number, and the operator is recorded. Then, the appropriate operation is performed based on the what operator is used. This code doesnt have any error checking, so its easy to crash if you add in spaces and other stuff

Author:  darknoob [ Wed Mar 29, 2006 9:20 pm ]
Post subject: 

nice
thx

Author:  chrispminis [ Thu Mar 30, 2006 12:03 am ]
Post subject: 

Err darknoob dont use it. I'm probly in your class and the assigment said specifically NOT to use predefined Turing functions such as strint or strintok. That's why we spent so many classes emulating the strreal function. There have been a billion topics on this subject, all by people you know, because we are the only school in the world with this assignment right now.

Author:  MysticVegeta [ Thu Mar 30, 2006 8:54 pm ]
Post subject: 

I find this to be pretty dumb. Why would someone block from using predefined fcns? to increase logic/skills? Heck, there are other ways, make them do J4 on CCC lol. Anyways, if you find the topic by chrispminis, you will find how to convert it without using those fcns.

Author:  person [ Thu Mar 30, 2006 9:19 pm ]
Post subject: 

MysticVegeta wrote:
Why would someone block from using predefined fcns? to increase logic/skills?


Yes. I don't understand why this would be a bad way of teaching students about concepts.


: