MikeyG has asked for the wisdom of the Perl Monks concerning the following question:
Below is my entire script, the problem I have is with Choice number 4.
#! /usr/bin/perl -W use strict; use 5.010; my %accounts = (tom => "BigApple"); my %types = (tom => "admin"); my $choice; my ($userID, $passwd, $type); my @userID = $userID; my $menu = <<MENU; System Administration Tasks 1- Create a new account 2- Delete an account 3- Change an account password 4- List users in alphabetical order 5- Exit the system MENU while (1) { print $menu, "\n"; print "Make your choice: "; chomp($choice = <STDIN>); if ($choice == 1){ print "Enter your userID: "; chomp ($userID = <STDIN>); print "Enter your password: "; chomp ($passwd = <STDIN>); if (exists $accounts{$userID}) { print "Duplicate user id, try again later" }else { print "Enter the account type, admin (or regular): "; chomp ($type = <STDIN>); $accounts{$userID} = $passwd; $types{$userID} = $type; print "Account was successfully created \n"; } } elsif ($choice == 4){ # sort the users in descending order foreach $userID (reverse sort keys %accounts) { # print out login names 3 per line @keys = keys %accounts; for (my $counter = 1; $counter <= @keys; $counter++) { print "$userID\t"; print "\n" if $counter % 3 == 0; } } } else { print "Invalid choice, TRY AGAIN!\n"; } }
Original code restored below by GrandFather
#! /usr/bin/perl -W use strict; use 5.010; sub heading; # A script that allows a user to perform system administration tasks # Programmer name: Michael A. Gregor # Course: COSC 146 # Lab#: 7 # Due Date: 3/10/16 # lab7.pl my %accounts = (tom => "BigApple"); my %types = (tom => "admin"); my $choice; my ($userID, $passwd, $type); my @userID = $userID; # call the function named 'heading' to display the output heading heading; my $menu = <<MENU; System Administration Tasks 1- Create a new account 2- Delete an account 3- Change an account password 4- List users in alphabetical order 5- Exit the system MENU while (1) { # display the menu print $menu, "\n"; # get the user choice and fullfil the request print "Make your choice: "; chomp($choice = <STDIN>); if ($choice == 1){ #print "a\n"; # replace the print-statements with your own code !! +!!!!!!!!!!!!!!! # get user id and password print "Enter your userID: "; chomp ($userID = <STDIN>); print "Enter your password: "; chomp ($passwd = <STDIN>); # is there an account by that name already? if so, display a messa +ge and then start the next loop iteration if (exists $accounts{$userID}) { print "Duplicate user id, try again later" } # no account by that name, ask the user for the account type else { print "Enter the account type, admin (or regular): "; chomp ($type = <STDIN>); # create the account and then print a message $accounts{$userID} = $passwd; $types{$userID} = $type; print "Account was successfully created \n"; } } elsif ($choice == 2){ # get user id and password print "Enter your userID: "; chomp ($userID = <STDIN>); print "Enter your password: "; chomp ($passwd = <STDIN>); # authenticate the user account. If it is a valid account with + admin privileges, go ahead and delete the # account. If the account is invalid or has no admin privieges +, display an appropriate message in each case if (authenticate($userID, $passwd, %accounts) == 1) { if ($type eq 'admin') { delete $accounts{$userID}; print "Account was deleted successfully"; } else { print "Can't delete, Contact the system administrator" +; } } } elsif ($choice == 3){ # get user id and password print "Enter your userID: "; chomp ($userID = <STDIN>); print "Enter your password: "; chomp ($passwd = <STDIN>); # authenticate the user account. If it is a valid account, ask + the user for the new password. # have the user re-enter the new password again and confirm th +at they are the same. If so, change # the password. In any case, print a appropriate message if (authenticate($userID, $passwd, %accounts) == 1) { print "Enter a new password: "; chomp (my $passwd = <STDIN>); print "re-enter the password again: "; chomp (my $npass1 = <STDIN>); if ($passwd eq $npass1) { $passwd = $npass1; $accounts{$userID} = $passwd; print "password was changed successfully"; } else { print "password was not changed!"; } } } elsif ($choice == 4){ # sort the users in descending order foreach $userID (reverse sort keys %accounts) { # print out login names 3 per line @keys = keys %accounts; for (my $counter = 1; $counter <= @keys; $counter++) { print "$userID\t"; print "\n" if $counter % 3 == 0; } } } elsif ($choice == 5){ # print a message and terminate the script print "Good bye! \n\n"; exit; } else { print "Invalid choice, TRY AGAIN!\n"; } } # write a function definition for the function named 'heading'. This f +unction prints out the output heading sub heading { print "\n"; print "Programmer name: Michael A. Gregor \n"; print "Course: COSC 146 \n"; print "Lab#: 7 \n"; print "Due Date: 3/10/16 \n"; } # authenticate a user. return 1 if the user id and password combinatio +n is good. Otherwise return 0; sub authenticate { my $id = shift; my $passwd = shift; my %accounts = @_; if (exists $accounts{$id} && $accounts{$id} eq $passwd){ return 1; } else { return 0; } }
|
|---|