473,378 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Program flow

13
I'm having a bit of trouble with program flow.

I'd love to explain more, but it seems the comments in my code do a better job.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $left = 'left';
  7. my $right = 'right';
  8.  
  9. print "You\'re walking along, and you see a fork in the road. What do you do\?.\n";
  10. print "To Turn Left(type left)\?\n";
  11. print "To Turn Right(type right)\?\n";
  12.  
  13. my $choice = <STDIN>;
  14. chomp $choice;
  15.  
  16. if ($choice eq $left) { ###### If $choice equals $left then it needs to print LOLOL and then jump down to "If you turned left" section.
  17.     print "LOLOL\n";
  18.  
  19. } elsif ($choice eq $right) { ####### if $choice quals $right then print HAHA and jump down to "If you turned right"
  20.     print "HAHA\n";
  21.  
  22. } else {
  23.     print "PRINT A VALID OPTION MAAAAAN\n"; ######## Jump back my $choice so the user can print a valid option.
  24. }
  25.  
  26. ###############- If you turned left -
  27.  
  28. print "You just turned left, and you come to a cliff. What do you do\?.\n";
  29. print "To jump off (type off) \n";
  30. print "To do nothing (tpe nothing) \n";
  31.  
  32. ####################### -If you turned right -
  33.  
  34. print "You just turned right, and you come to a village. What do you\?.\n";
  35. print "To burn and sack the village (type sack) \n"; 
  36. print "To run through the village to the church (type church) \n";
  37.  
Sep 6 '07 #1
3 1281
KevinADC
4,059 Expert 2GB
If this is anymore than an excesize to learn some basic coding you should not continue to proceed like you have so far. Trying to code a game like this is not as easy as you might hope. But if it is just an excersize, you need to start using subroutines and passing arguments to them and back from them when the time comes:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. start();
  6.  
  7. sub start {
  8.    print "You\'re walking along, and you see a fork in the road. What do you do\?.\n";
  9.    print "To Turn Left(type left)\?\n";
  10.    print "To Turn Right(type right)\?\n";
  11.    my $choice = <STDIN>;
  12.    chomp $choice;
  13.    fork_in_road($choice);
  14. }
  15.  
  16. sub fork_in_road {
  17.    my $choice = shift;
  18.    if ($choice eq 'left') { ###### If $choice equals $left then it needs to print LOLOL and then jump down to "If you turned left" section.
  19.       print "LOLOL\n";
  20.       fork_left();
  21.  
  22.    }
  23.    elsif ($choice eq 'right') { ####### if $choice quals $right then print HAHA and jump down to "If you turned right"
  24.       print "HAHA\n";
  25.       fork_right();
  26.  
  27.    }
  28.    else {
  29.       print "PRINT A VALID OPTION MAAAAAN\n"; ######## Jump back my $choice so the user can print a valid option.
  30.       start();
  31.    }
  32. }
  33.  
  34. ###############- If you turned left -
  35. sub fork_left {
  36.    print "You just turned left, and you come to a cliff. What do you do\?.\n";
  37.    print "To jump off (type off) \n";
  38.    print "To do nothing (type nothing) \n";
  39. }
  40. ####################### -If you turned right -
  41.  
  42. sub fork_right {
  43.    print "You just turned right, and you come to a village. What do you\?.\n";
  44.    print "To burn and sack the village (type sack) \n"; 
  45.    print "To run through the village to the church (type church) \n";
  46. }
  47.  
Sep 7 '07 #2
gloomer
13
WOW. Thank you so very much!

I know how to use subs now.
Sep 7 '07 #3
KevinADC
4,059 Expert 2GB
That was only a gentle and basic introduction to subs.
Sep 7 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Albert Ahtenberg | last post by:
Hello, I had some bad experience with code organization and script functionality in writing my php based applications. And as the applications get bigger in scale it gets even worse. Therefore,...
4
by: Bruce W...1 | last post by:
A scripting newbie question... I'm trying to understand some code I found. This script conducts a poll and writes the results to a text file. The following statement is part of the source file. ...
3
by: Kelvin Klein | last post by:
Is there a visualization tool for C program out there? Is there a visualization tool for the language of one of the crossposted newsgroup out there? I am a refuge from comp.lang.c, kicked out...
1
by: Will Stuyvesant | last post by:
I never used the popen or popen2 libraries but it is my understanding that they can capture the output of console based programs. Is it also possible to send keystrokes to console base programs? ...
7
by: jacob navia | last post by:
Suppose that you want to know where your program has passed, i.e. the exact flow of the program. A brute force solution is to make an array of n lines, n being the number of lines in the...
1
by: Brett | last post by:
I'd like to have all of my documentation in one place. I use the following for documenting code: - attributes for certain types of documentation - use of the C# generated inline XML...
7
by: Buck Rogers | last post by:
Hi all! Newbie here. Below is an example from Teach Yourself C in 21 Days. My apologies if it is a bit long. What I don't understand is how the "get_data" function can call the...
8
by: lovecreatesbea... | last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, ... . "Non-automatic variables are...
6
by: Crooter | last post by:
Hello colleagues, Could anybody tell me if there are existing open-source solutions to extract the program tree using a program source code? I'm aware that GCC has program flow information and...
3
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.