perl assignment question

Thread Solved

Join Date: Dec 2008
Posts: 11
Reputation: mtramnes is an unknown quantity at this point 
Solved Threads: 0
mtramnes mtramnes is offline Offline
Newbie Poster

perl assignment question

 
0
  #1
Jan 26th, 2009
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.

  1. #!/usr/bin/perl -w
  2. # guessnum2.pl
  3.  
  4. use strict;
  5.  
  6. my $target = 12;
  7. print "Guess my number!\n";
  8. print "Enter your guess: ";
  9. my $guess = <STDIN>;
  10.  
  11. if ($target == $guess) {
  12. print "That's it!! You guessed correctly:\n";
  13. while (<STDIN> > $target) {
  14. print "Your guess is too high!\n";
  15. }
  16. while (<STDIN> < $target) {
  17. print "Your guess is too low!\n";
  18. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: perl assignment question

 
0
  #2
Jan 26th, 2009
One while loop to input a guess, then 3 tests.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: perl assignment question

 
1
  #3
Jan 26th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 11
Reputation: mtramnes is an unknown quantity at this point 
Solved Threads: 0
mtramnes mtramnes is offline Offline
Newbie Poster

Re: perl assignment question

 
0
  #4
Jan 27th, 2009
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?

  1.  
  2. #!/usr/bin/perl -w
  3. # guessnum2.pl
  4.  
  5. use strict;
  6. use warnings;
  7.  
  8. my $target = 12;
  9. while {
  10. print "Pick a number\n";
  11. my $guess = <STDIN>;
  12. chomp $guess;
  13. if ($guess > $target){
  14. print "You guessed too high\n";
  15. next;
  16. }
  17. elsif ($guess < $target) {
  18. print "You guessed too low\n";
  19. next;
  20. }
  21. print "YOU GUESSED RIGHT!! It was $guess\n";
  22. last;
  23. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: perl assignment question

 
0
  #5
Jan 27th, 2009
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?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Perl Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC