Solved

program problem

Posted on 2000-04-18
2
181 Views
Last Modified: 2010-03-05
1 Write a program in perl that prompts and restricts the user to any year between 1900
  and 2099, and for that year prints the sign of the Chinese Zodiac. Your
  solution can only use "if-elsif-else" type statements to implement the
  selections.  There are twelve signs in the Chinese Zodiac and they are,
  in order:

  Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Goat, Monkey, Rooster, Dog, Pig

  In addition, the Cat is used instead of the Rabbit in Vietnamese culture. If
  the year maps to Rabbit, print both.

  This sequence, starting with Rat, begins in the year 1900.  As a test, this
  year, 2000 is Year of the Dragon.
0
Comment
Question by:myoreorex
2 Comments
 
LVL 16

Accepted Solution

by:
maneshr earned 50 total points
ID: 2727869
try this...........

#!/usr/local/bin/perl

@yrs=("Rat","Ox","Tiger","Rabbit","Dragon","Snake","Horse","Goat","Monkey","Roo
ster","Dog","Pig");

$sign_change=11;

print "Enter a year between 1900 & 2099 ";
$year=<STDIN>;
chomp($year); ## Remove the \n from the users input
$year=~ s/\s+//g; ## Remove any white spaces from the user's input.
if ($year<1900 || $year >2099){
  print "Year can ONLY be between 1900 and 2099\n";
  exit;
}

$sign=$year-1900;

while($sign>$sign_change){
  $sign-=scalar @yrs;
}

print "$year is the year of $yrs[$sign] ";

if ($yrs[$sign]=~ /^Rabbit$/){
  print "Cat";
}
0
 

Author Comment

by:myoreorex
ID: 2727905
Ok...I left out the chomp...thats where I went wrong.  Thanks!


Michelle
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now