Randy Curtis
Randy Curtis

Reputation: 11

Simple menu with array homework problem wont execute

My final lab for my programming and logic intro course will not execute, it has a quick CMD flash on the screen then nothing happens. I'm not sure if it is a syntax error or what. Thank you very much for any help.

#Part 2 Insert 1.0
@username=(“first”,“last”);
print "Please enter your first name: ";
$username[0] = <STDIN>;
chomp $username[0];

print "Please enter your last name: ";
$username[1] = <STDIN>;
chomp $username[1];

print "Your name is:\n";
print "$username[0]\n$username[1]\n";

print "Please enter a number\n";
$number1 = <STDIN>;
chomp $number1;
#Part 2 Insert 1.0 - END


print "Please enter a second number\n";
$number2 = <STDIN>;
chomp $number2;

while ( $number2 == 0 )
{
  print "You entered $number2\nPlease enter a non-zero number\n";
  $number2 = <STDIN>;
  chomp $number2;
}

print "Please enter your first name\n";
$name=<STDIN>;
chomp $name;

print "$name, how many times do you want to run your program?\n";
$total_loops=<STDIN>;
chomp $total_loops;

print "\n$name, you entered $number1 and $number2\n";

for($ctr=1; $ctr <= $total_loops; $ctr++)
{
  print("\nDisplaying $ctr of $total_loops loops \n\n");
  print("Please make a selection using the menu below\n");
  print "1 - Subtract \n";
  print "2 - Divide\n";
  print "3 - Modulus\n";
  print "4 - Exit\n";
  $answer = <STDIN>;
  chomp $answer;

#next line edited to use lab4 part 2 array
  print "$username[0] $username[1] You selected $answer as your menu choice\n\n";

if($answer == 1)
{
  $sub = $number1 - $number2;
  print("$number1 - $number2 is $sub");
 }
elsif($answer == 2)
{
  $quot = $number1/$number2;
  print "$number1 / $number2 is $quot";
}
elsif($answer == 3)
{
   $mod=$number1 % $number2;
   print "$number1 % $number2 is $mod";
 }
elsif($answer == 4)
{
    print "Exiting program";
    exit 0;
 }
else
 {
    print "$answer is not a valid menu choice! Please retry.";
  }
  print ("\n"); #blank line
}

Expected - prompt for name and assign to array prompt for numbers menu for operation selection print results with assigned name from array

Actual- Quick CMD window flash then nothing

Upvotes: 1

Views: 132

Answers (1)

Dave Cross
Dave Cross

Reputation: 69314

It sounds like you're double-clicking the program in some kind of directory list which starts the program in a command window and then immediately closes - not giving you time to see what message it displays. The solution is, surely, to open your own command window and then type perl your_program_name.pl at the prompt. An already-open command window will not close when the program exits and you will be able to see the error.

(But if I had to guess, I'd suggest that the problem was the "smart quotes" in the line where you define @username.)

Upvotes: 3

Related Questions