mandalmanas5519 has asked for the wisdom of the Perl Monks concerning the following question:

My program let user to enter their value depends upon y/n like below First it should ask for 1.Do you want to enter value for Starting account balance. (y/n)?" if i enter y it should ask Please enter SAB= and it will go to next question if i enter n it should skip "Please enter SAB" and should go to next question 2.Do you want to enter the Date. (y/n)? Here if i enter n then program should exit
use feature ':5.10'; use warnings; print "1.Do you want to enter value for Starting account balance. (y/n +)?"; $answer= <STDIN>; chomp $answer; if ($answer eq "y") { print "Please enter SAB= "; $SAB=<STDIN>; print "$SAB"; print "2.Do you want to enter the Date. (y/n)?"; $answer=<STDIN>; } elsif ($answer eq "y") { print "Enter date = "; $Date=<STDIN>; print "$Date"; } else { print "Exit program"; }
Please help

Replies are listed 'Best First'.
Re: Don't get the output as expect.
by frozenwithjoy (Priest) on Jun 05, 2013 at 20:03 UTC
    The problem with your script is that you are using a single if-elsif-else conditional when you probably need to have a separate conditional for each question or nested ifs (as long as it doesn't get complicated and deep). Also, please use strict;.
      Thanks for suggestion I have modified the code
      use feature ':5.10'; use strict; use warnings; print "1.Do you want to enter value for Starting account balance. (y/n +)?"; $answer= <STDIN>; chomp $answer; if ($answer eq "y") { print "Please enter SAB= "; $SAB=<STDIN>; print "$SAB"; } print "2.Do you want to enter value date (y/n)?"; $answer= <STDIN>; chomp $answer; if ($answer eq "y") { print "Please enter date= "; $Date=<STDIN>; print "$Date"; } else{ print "Exit"; }
      Now i am getting the output what i am expecting

        Glad to hear. Be sure to declare your variables with my so that everything will run when you have use strict; in effect.

        Also, you may want to change this:

        if ($answer eq "y")

        to something like this:

        if ( $answer =~ /^y/i )

        That way, it will match Y y Ya yes yup Yar, etc. and let you remove two lines of code since you will no longer need to chomp $answer;.

Re: Don't get the output as expect.
by kcott (Archbishop) on Jun 05, 2013 at 20:29 UTC

    G'day mandalmanas5519,

    You've got your logic fairly messed up there with the same condition controlling both the if and the elsif.

    Perhaps drawing yourself a flow chart or writing some pseudocode might help. From your description, I think you want:

    Want SAB? If 'y' Get SAB Want Date? If 'y' Get Date else Exit

    Once you've got the logic sorted out, there's a few other issues you'll need to address. For instance, use strict; will probably tell you about most of them (see strict) and you may want to chomp $date.

    -- Ken

Re: Don't get the output as expect.
by choroba (Cardinal) on Jun 05, 2013 at 20:06 UTC
    This works as specified. No need to nest the blocks.
    print "1. Do you want to enter value for Starting account balance. (y/ +n)?\n"; chomp(my $answer = <STDIN>); my $SAB; if ('y' eq $answer) { print 'Please enter SAB='; $SAB = <STDIN>; } print "2. Do you want to enter the Date (y/n)?\n"; chomp($answer = <STDIN>); exit if 'n' eq $answer;

    However, you have not specified what to do if the user entered something else.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ