in reply to New to Programming and I have chose PERL
By using strict, you will catch accidental misspellings and make you declare your variables. The way that your code is right now, you don't see the last line of it,until you do a control-C out of the program. Mine will do one of the calculations then exit. When checking the input with a regular expression, I used the "i" modifier which means that if the user has their caps lock key on, it will still accept it.#!/usr/bin/perl -w use strict; my($input,$hrate,$yrate,$x); while(!defined $x){ print "Do you wish to calculate yearly salary (y) or hourly rate (h) +:"; chomp($input=<STDIN>); if($input=~/y/i){ # I am just guessing that this is what you're # trying to do print "Enter your yearly salary: "; chomp ($yrate=<STDIN>); $hrate=($yrate/52)/40; printf("You make \$%2d per hour.",$hrate); $x=1; }elsif($input=~/h/i){ print "Enter your hourly rate: "; chomp ($hrate=<STDIN>); $yrate=($hrate * 40)*52; print"\nYour yearly salary is \$$yrate\n"; $x=1; }else{ print"Please enter either h or y.\n\n"; sleep 2; ) }
|
|---|