Click to See Complete Forum and Search --> : wondering about while


crokett
03-22-2001, 11:55 PM
i am learning perl. this is a script i wrote that uses a while loop, but the while is not working. the program just keeps running and does not exit properly. it does not matter what is entered by the user. any help is appreciated.
thanks

$ANS="y";

while ($ANS=="y" or $ANS=="Y"){
print "Enter the height:";
$HEIGHT=<STDIN>;

$WIDTH=$HEIGHT*2;
$LEDGE=sqrt(($HEIGHT**2)+($HEIGHT**2));
$LSPAR=$HEIGHT*1.09;
$SPREADR=$HEIGHT*.85;
$KLENGTH=$HEIGHT*.79;
$KSSIDE=$HEIGHT*.33;
$KLSIDE=$HEIGHT*.69;
$SPREADRLOC=$HEIGHT*.81;

print "The height of the kite is $HEIGHT feet.\n";
print "The width of the kite is $WIDTH feet.\n";
printf ("The leading edge is %.2f feet long. \n", $LEDGE);
printf ("The leading edge spar is %.2f feet long.\n", $LSPAR);
print "The spreader is $SPREADR feet long.\n";
print "The keel is $KLENGTH feet long.\n";
print "The keel short side is $KSSIDE feet long.\n";
print "The keel long side is $KLSIDE feet long.\n";
print "The spreader is located $SPREADRLOC feet up the leading edge.\n";
print "\n";
print "Size another kite? Enter y or n: ";
$ANS=<STDIN>;
chop($ANS);
}

Energon
03-23-2001, 01:33 AM
you have to compare strings like this:

if $answer eq "whatever"

not

if $answer == "whatever"

that's one of the major differences between Perl and C...

crokett
03-23-2001, 02:39 PM
oh. thanks. i figured it was something simple.

micxz
03-25-2001, 12:35 AM
here try:
$ANS="y";

while ($ANS=="y" or $ANS=="Y"){
print "Enter the height:";
$HEIGHT=<STDIN>;

$WIDTH=$HEIGHT*2;
$LEDGE=sqrt(($HEIGHT**2)+($HEIGHT**2));
$LSPAR=$HEIGHT*1.09;
$SPREADR=$HEIGHT*.85;
$KLENGTH=$HEIGHT*.79;
$KSSIDE=$HEIGHT*.33;
$KLSIDE=$HEIGHT*.69;
$SPREADRLOC=$HEIGHT*.81;

print "The height of the kite is $HEIGHT feet.\n";
print "The width of the kite is $WIDTH feet.\n";
printf ("The leading edge is %.2f feet long. \n", $LEDGE);
printf ("The leading edge spar is %.2f feet long.\n", $LSPAR);
print "The spreader is $SPREADR feet long.\n";
print "The keel is $KLENGTH feet long.\n";
print "The keel short side is $KSSIDE feet long.\n";
print "The keel long side is $KLSIDE feet long.\n";
print "The spreader is located $SPREADRLOC feet up the leading edge.\n";
print "\n";
print "Size another kite? Enter y or n: ";
$ANS=<STDIN>;
chop($ANS);
exit; #<THIS HELPS
}

Energon
03-25-2001, 01:34 AM
that still won't work, for the exact reason I posted above...

micxz
03-26-2001, 02:46 AM
try:

last;

at the end instead of exit;

maybe?

Energon
03-26-2001, 11:26 AM
maybe you should read my post... an exit/last isn't what is needed... just read my post up there and be content that it's the right solution...