how to make a program do something instead of something else

  • Hiei
  • Beginner
  • Beginner
  • Hiei
  • Posts: 51

Post 3+ Months Ago

^ right there, i've been working on a code that makes you log in and allows you to choose wether to add,divide or play some cheesy game i made,

however when i run it it goes in order and wont let me choose what to do. my code

Code: [ Select ]
sub login { print "put in the name please";
$login = <stdin>;
chomp ($login);


while ($login ne brian) { 
print "put in the name please";
$login = <stdin>;
chomp ($login); };
if($login eq brian) { print "what would you like to do";}
};

&login;

$choice = <stdin>;

if($choice eq login) { &login; }
if($choice eq leave) { print "bye" } exit;
  1. sub login { print "put in the name please";
  2. $login = <stdin>;
  3. chomp ($login);
  4. while ($login ne brian) { 
  5. print "put in the name please";
  6. $login = <stdin>;
  7. chomp ($login); };
  8. if($login eq brian) { print "what would you like to do";}
  9. };
  10. &login;
  11. $choice = <stdin>;
  12. if($choice eq login) { &login; }
  13. if($choice eq leave) { print "bye" } exit;

any ideas on how i can fix this? thanks
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

OK, so there are probably better way to go about this, but this works:
Code: [ Select ]
$count=0;
&login;
sub checklogin {
    if ($login ne 'brian') {
        if($login =~ (/leave|exit|bye|quit/) || $count == 2) {
            exit;
        } else {
            $count++;
            &login;
        }
    } else {
        &runprogram;
    }
}

sub runprogram {
    print "what would you like to do: ";
    $choice = <stdin>;
    if($choice eq login) { &login; }
    if($choice =~ (/leave|exit|bye|quit/)) {
        print "bye"
    } else {
        print "I don't have any more functions $login, bye\n\n";
        exit;
    }
}

sub login {
    print "put in the name please: ";
    $login = <stdin>;
    chomp ($login);
    &checklogin;
}
  1. $count=0;
  2. &login;
  3. sub checklogin {
  4.     if ($login ne 'brian') {
  5.         if($login =~ (/leave|exit|bye|quit/) || $count == 2) {
  6.             exit;
  7.         } else {
  8.             $count++;
  9.             &login;
  10.         }
  11.     } else {
  12.         &runprogram;
  13.     }
  14. }
  15. sub runprogram {
  16.     print "what would you like to do: ";
  17.     $choice = <stdin>;
  18.     if($choice eq login) { &login; }
  19.     if($choice =~ (/leave|exit|bye|quit/)) {
  20.         print "bye"
  21.     } else {
  22.         print "I don't have any more functions $login, bye\n\n";
  23.         exit;
  24.     }
  25. }
  26. sub login {
  27.     print "put in the name please: ";
  28.     $login = <stdin>;
  29.     chomp ($login);
  30.     &checklogin;
  31. }


Cheers
This
  • Hiei
  • Beginner
  • Beginner
  • Hiei
  • Posts: 51

Post 3+ Months Ago

thanks for the code, however i made some modifications so it keeps asking you for the pass. instead of just 3 times, also when i enter the pass, it doesn't allow me to type any more, is this a result of me making a modafication to the code? heres the part i put in

Code: [ Select ]
print "put in the name please";
$login = <stdin>;
chomp ($login);


while ($login ne brian) { 
print "put in the name please";
$login = <stdin>;
chomp ($login); };
  1. print "put in the name please";
  2. $login = <stdin>;
  3. chomp ($login);
  4. while ($login ne brian) { 
  5. print "put in the name please";
  6. $login = <stdin>;
  7. chomp ($login); };


thanks for the help though
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

Really stuck on using a while loop, huh?
Are you still calling the &checklogin routine? With the while loop, you shouldn't be. also, remove the last ; from the line that reads
chomp ($login); };
also, you need to call the &runprogram routine once the while loop evaluates false and exits. So:
Code: [ Select ]
print "put in the name please";
$login = <stdin>;
chomp ($login);


while ($login ne brian) {
print "put in the name please";
$login = <stdin>;
chomp ($login); }
&runprogram;
  1. print "put in the name please";
  2. $login = <stdin>;
  3. chomp ($login);
  4. while ($login ne brian) {
  5. print "put in the name please";
  6. $login = <stdin>;
  7. chomp ($login); }
  8. &runprogram;

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 69 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.