Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7834871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:22:02+00:00 2026-06-02T13:22:02+00:00

I am trying to make a program that simulates a line at a grocery

  • 0

I am trying to make a program that simulates a line at a grocery storye.
if a is entered, it allows for users to add names.
if c is entered, it simulates a person leaving the line.
if p is entered, it prints the list of names.
if q is entered, it quits.

my code just results in an infinite loop and I am not sure why. Every time I try and enter in values, it just reads invalid input and wont exit. I am not sure if the other stuff is working or not but that is not what I need help with.

    $choice="";
    $name;
    @line=();
    print "\n";
    print "Choose an option:\n";
    print "a: Add person to end of line\n";
    print "c: Call the next person in line\n";
    print "p: Print the list of people in line\n";
    print "q: Quit\n";
    print "\n";

    while ($choice ne "q") {

    print "Your choice:";
    $choice = <>;
    print "\n";

    if($choice eq "a") {
            print "Enter name:";
            $name = <>;
            push(@line,$name);
    }
    elsif ($choice eq "c") {
    shift(@line);
    }
    elsif ($choice eq "p") {
            for ($i=0;$i<=scalar(@line);$i++) {
                    print (@line[$i]);
            }
    }
    elsif ($choice eq "q") {
            exit;
    }
    else {
            print "Invalid option";
    }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:22:11+00:00Added an answer on June 2, 2026 at 1:22 pm

    As @stark has correctly pointed out, the main issue with your loop is that you’re not removing the newline after getting your input from STDIN. So, $choice will never match your options and you will never break out of the loop. Try changing:

    print "Your choice:";
    $choice = <>;
    

    to

    print "Your choice:"; 
    $choice = <STDIN>; 
    chomp $choice;
    

    Notice that you’ll need to chomp $choice in order to remove the newline before you do your string comparison.

    Also, try writing your scripts with “use warnings” and “use strict”. This will pick up a lot of little errors you may not otherwise have noticed. For example, your script might look something like this:

    #!/usr/bin/env perl
    use strict; 
    use warnings; 
    
    my $choice = ""; 
    my $name; 
    my @line = (); 
    print "\n"; 
    print "Choose an option:\n"; 
    print "a: Add person to end of line\n"; 
    print "c: Call the next person in line\n"; 
    print "p: Print the list of people in line\n"; 
    print "q: Quit\n"; 
    print "\n"; 
    
    while ( $choice ne "q" ) { 
    
        print "Your choice:"; 
        $choice = <STDIN>; 
        chomp $choice; 
        print "\n"; 
    
        if ( $choice eq "a" ) { 
            print "Enter name:"; 
            $name = <>; 
            push( @line, $name ); 
        } 
        elsif ( $choice eq "c" ) { 
            shift( @line ); 
        } 
        elsif ( $choice eq "p" ) { 
            for ( my $i = 0; $i <= scalar( @line ); $i++ ) { 
                print( $line[$i] ); 
            } 
        } 
        elsif ( $choice eq "q" ) { 
            exit; 
        } 
        else { 
            print "Invalid option"; 
        } 
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a program that read a file line by line and
I'm trying to make a basic program that allows a user to create a
I am trying to make a program that automatically corrects the perspective of a
I'm trying to make a program that reads the value at a certain address.
A am trying to make a program that takes a signal from one class
I am trying to make a program that will find similar images from a
Hey, I am trying to make a program that minimises any program to the
I'm trying to make a small program that could intercept the open process of
I am trying to make a simple program that could open a new activity
I am trying to make a program in Java that checks for three specific

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.