need help with $end vs kEND error

K

Kd Kushelduv

I have been trying to write a program for my class assignment but
everytime i do i get an error on the last line of the code that says
'unexpected $end, expecting kEND'
this happens on every program i try to run
any ideas?

i am using fedora core 5 if that makes a difference
 
D

Drew Olson

Kd said:
I have been trying to write a program for my class assignment but
everytime i do i get an error on the last line of the code that says
'unexpected $end, expecting kEND'
this happens on every program i try to run
any ideas?

i am using fedora core 5 if that makes a difference

Often times this error is a result of missing an end statement somewhere
within your program. For example, you may have forgotten to end a
method, if statement or class. Can you post some code so we can take a
look?
 
A

Andrew Libby

I'm just learning ruby myself, but typically when this
happens, I'm missing an end to a function, or some other
block:


#!/usr/bin/ruby

def joe
print "JOE MAMMA\n"


Produces:


[alibby@localhost ~]$ ./t.rb
/t.rb:6: syntax error, unexpected $end, expecting kEND


Perhaps this is it? If you're willing to post the code (if
it's not too long) we may be able to provide more guidance.

Andy
 
C

Carl Lerche

You have mismatched block openings / endings. Check your def / end
do / end if / end, etc...
 
K

Kd Kushelduv

as far as i can tell nothing is missing
here is the code (its only three methods):

def promptToQuit
print "\nYou did not enter any numbers \n are you sure you wish to
quit? (y/n): "
answer = getc
answer = answer.downcase
while (answer != 'y' && answer != 'n')
print "Are you sure you wish to quit? (y/n): "
answer = getc
answer = answer.downcase
end

return answer
end

def getNumbers (num)
$high = num
$low = num

while (num != 0)
print "Please enter a number (0 to quit): "
num = geti

if ( (num > $high) && (num != 0) ) then
$high = num
else if ( (num < $low) && (num != 0) ) then
$low = num
end
end
end

answer = 'n'

print "\nPlease enter a number (0 to quit): "
num = geti

if (num == 0) then
answer = promptToQuit
end

if (answer == 'y') then
print "\nGoodbye"
else
num = 1
getNumbers(num)
end

puts "Highest Number: " + $high.to_s
puts "Lowest Number: " + $low.to_s
puts "\n"
 
M

Mike Fletcher

Kd said:
if ( (num > $high) && (num != 0) ) then
$high = num
else if ( (num < $low) && (num != 0) ) then
$low = num
end

This should be

if ( (num > $high) && (num != 0) ) then
$high = num
elsif ( (num < $low) && (num != 0) ) then
$low = num
end

Your else is starting a new block context (not the Ruby term I'm sure;
that's kinda what I'd call it in Perl :) and Ruby thinks what you're
doing is:

if ...

else
if ...
end
end

But you've only got a single end so it's confuzzled.
 
K

Kd Kushelduv

huh that was the problem but my text book had everything as
elseif

i added the space because it wouldn't accept that
but it seems the author still mistyped the entire thing

o well
 
E

Eero Saynatkari

--T7mxYSe680VjQnyC
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

as far as i can tell nothing is missing
here is the code (its only three methods):
=20
def promptToQuit
print "\nYou did not enter any numbers \n are you sure you wish to=20
quit? (y/n): "
answer =3D getc
answer =3D answer.downcase
while (answer !=3D 'y' && answer !=3D 'n')
print "Are you sure you wish to quit? (y/n): "
answer =3D getc
answer =3D answer.downcase
end
=20
return answer
end
=20
def getNumbers (num)
$high =3D num
$low =3D num
=20
while (num !=3D 0)
print "Please enter a number (0 to quit): "
num =3D geti
=20
if ( (num > $high) && (num !=3D 0) ) then
$high =3D num
else if ( (num < $low) && (num !=3D 0) ) then
$low =3D num

Right here. You want elsif, otherwise you are missing
the closing end for the nested if. Also, drop the thens,
they are not needed :)
end
end
end
=20
answer =3D 'n'
=20
print "\nPlease enter a number (0 to quit): "
num =3D geti
=20
if (num =3D=3D 0) then
answer =3D promptToQuit
end
=20
if (answer =3D=3D 'y') then
print "\nGoodbye"
else
num =3D 1
getNumbers(num)
end
=20
puts "Highest Number: " + $high.to_s
puts "Lowest Number: " + $low.to_s
puts "\n"

The error message is a bit ambiguous--$end means EOF, which
Ruby encountered while it was still waiting for a literal
end (the token is called kEND).

--T7mxYSe680VjQnyC
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)

iD8DBQFFK/AR7Nh7RM4TrhIRArDCAKCtzU965WGEzz4grrYwLsQBha3bPACgkbtK
mGdhzWygOFqib907OboOlww=
=745Q
-----END PGP SIGNATURE-----

--T7mxYSe680VjQnyC--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top