Im taking a perl class not exactly sure if what Im doing is right.
I was to take a written program that allowed the user to enter a number guessing what the target was. The original used elseif statements to let the user know if the guess was to high, too low, or correct. I need to make it so that it loops until the correct answer is given. Here is what I have so far. Any suggestions would be great.

#!/usr/bin/perl -w
# guessnum2.pl

use strict;

my $target = 12;
print "Guess my number!\n";
print "Enter your guess: ";
my $guess = <STDIN>;

if ($target == $guess)  {
	print "That's it!! You guessed correctly:\n";
while (<STDIN> > $target) {
	print "Your guess is too high!\n";
}
while (<STDIN> < $target) {
	print "Your guess is too low!\n";
}

A students best cheat tool is a search engine or the search feature of a website or forum. You would have found a recent thread discussing the exact same assignment had you done just a little searching.

http://www.daniweb.com/forums/thread169597.html

commented: I like it :) +27

Thanks, i went back and looked at the other thread. This is what I got out of it to make it work for what I needed. Does this make sense?

#!/usr/bin/perl -w
# guessnum2.pl

use strict;
use warnings;

my $target = 12;
while  {
   print "Pick a number\n";
   my $guess = <STDIN>;
   chomp $guess;
   if ($guess > $target){
      print "You guessed too high\n";
      next;
   }
   elsif ($guess < $target) {
      print "You guessed too low\n";
      next;
   }
   print "YOU GUESSED RIGHT!! It was $guess\n";
   last;
}

If you tried to run your code it will return a syntax error so I am suspicious you did not try it. In the other code the "while" loop has a condition:

while (1) {

why did you remove the condition in parenthesis?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.