help college student! 
Author Message
 help college student!

I have some code that will eventually run a menu driven program that I
want to be able to recall the menu and rerun the program after each
invidual session by the next read in menu choice. ... however I cannot
get the code to line up correctly ( it will find one function and then
not another because it is under that function in my listing of
functions/procedures...) .What I would like some help on, is to help me
figure out this recursive function in Pascal, I know that it is easily
possible in c by the switch function, however I'm getting frustrated
with this one!
Thanks!
rob

code:

Program Assignment1;

uses Wincrt;

var
   menuchoice : char;
   a,b,c,d,e,f,g,h,q : char;

Function initmenu (menuchoice:char):char;
         begin
              writeln('*********************');
              writeln('Welcome to my program');
              writeln('*********************');
              writeln('.By: Robert Lockhart.');
              writeln('.....May 24, 1999....');
              writeln('...Data Structures ..');
              writeln('**...Project 1....***');
              writeln('*****Instructor******');
              writeln('******Bob David******');
              writeln('******************');
              writeln('a)  Enter choice 1');
              writeln('b)  Enter choice 2');
              writeln('c)  Enter choice 3');
              writeln('d)  Enter choice 4');
              writeln('e)  Enter choice 5');
              writeln('f)  Enter choice 6');
              writeln('g)  Enter choice 7');
              writeln('h)  Enter choice 8');
              writeln('q)  to quit.  ');
              writeln('******************');
              writeln('Choice?...');

         END;

Function option1 (choice:char):integer;
var
   ans, y, n : char;

         Begin
              writeln(choice, ')  Are you sure you want this? (y,n)');
              read(ans);
              if ans = n then
              option1 := 1
                      else
                          writeln('ok, here you go for option1');
                          option1 := 0;
              printmenu;
          End;
Function option2 (choice : char) : char;
          begin
               writeln('You chose letter b!');
               printmenu;

          end;
Function option3(choice : char) : char;
         Begin
              writeln('You chose option3');
              option3 := choice;
              printmenu(c);

         End;
Function option4(choice : char) : char;
         Begin
              writeln('You chose :', choice);
              option4 := choice;
         End;
Function option5(choice : char) : char;
         Begin
              writeln('You chose :', choice);
              option5 := choice;
         End;
Function option6(choice : char) : char;
         Begin
              writeln('You chose :', choice);
              option6 := choice;
         End;
Function option7(choice : char) : char;
         Begin
              writeln('You chose :', choice);
              option7 := choice;
         End;
Function option8(choice : char) : char;
         Begin
              writeln('You chose :', choice);
              option8 := choice;
         End;
Function menu (menuchoice:char):char;
         begin
              CASE menuchoice of
               'a' : option1(a);
               'b' : option2(b);
               'c' : option3(c);
               'd' : option4(d);
               'e' : option5(e);
               'f' : option6(f);
               'g' : option7(g);
               'h' : option8(h);
               ELSE
              writeln(menuchoice, 'is not valid input.');
            END

            END;

Procedure printmenu (var mchoice:char);

         begin
              writeln('a)  Enter choice 1');
              writeln('b)  Enter choice 2');
              writeln('c)  Enter choice 3');
              writeln('d)  Enter choice 4');
              writeln('e)  Enter choice 5');
              writeln('f)  Enter choice 6');
              writeln('g)  Enter choice 7');
              writeln('h)  Enter choice 8');
              writeln('q)  to quit.  ');
              writeln('******************');
              writeln('Choice?...');
              read(mchoice);
              menu(mchoice);
         END;

 (* end functions*)

Begin
          initmenu(a);
          read(menuchoice);
          while menuchoice <> q do

          menu(menuchoice);

END.



Wed, 18 Jun 1902 08:00:00 GMT  
 help college student!

Quote:

>I have some code that will eventually run a menu driven program that I
>want to be able to recall the menu and rerun the program after each
>invidual session by the next read in menu choice. ... however I cannot
>get the code to line up correctly ( it will find one function and then
>not another because it is under that function in my listing of
>functions/procedures...) .What I would like some help on, is to help me
>figure out this recursive function in pascal, I know that it is easily
>possible in c by the switch function, however I'm getting frustrated
>with this one!
>Thanks!
>rob

I don't think any recursion is needed. Just a simple WHILE loop.
At least part of the problem seems to be that you don't read any
input inside the loop. For a fix - see below! My comments are marked
with an asterisk '*'.

BTW! Are you related to a math professor named Robert Lockhart. He
was at Notre Dame in the late 80s, when I was there in grad school.
I guess he later moved to Naval Academy.

Quote:

>code:

>Program Assignment1;

>uses Wincrt;

>var
>   menuchoice : char;
>   a,b,c,d,e,f,g,h,q : char;

* That's a lot of variables with non-descriptive names. Are you sure
* you need them all?

Quote:

>Function initmenu (menuchoice:char):char;

* Why is this a function? A procedure feels more natural!
* This function does not need the variable 'menuchoice'.
* I guess a later version may have a use for it. Presently it seems
* unnecessary.

Quote:
>         begin
>              writeln('*********************');
>              writeln('Welcome to my program');
>              writeln('*********************');
>              writeln('.By: Robert Lockhart.');
>              writeln('.....May 24, 1999....');
>              writeln('...Data Structures ..');
>              writeln('**...Project 1....***');
>              writeln('*****Instructor******');
>              writeln('******Bob David******');
>              writeln('******************');
>              writeln('a)  Enter choice 1');
>              writeln('b)  Enter choice 2');
>              writeln('c)  Enter choice 3');
>              writeln('d)  Enter choice 4');
>              writeln('e)  Enter choice 5');
>              writeln('f)  Enter choice 6');
>              writeln('g)  Enter choice 7');
>              writeln('h)  Enter choice 8');
>              writeln('q)  to quit.  ');
>              writeln('******************');
>              writeln('Choice?...');

>         END;

>Function option1 (choice:char):integer;
>var
>   ans, y, n : char;

>         Begin
>              writeln(choice, ')  Are you sure you want this? (y,n)');
>              read(ans);
>              if ans = n then
>              option1 := 1
>                      else
>                          writeln('ok, here you go for option1');
>                          option1 := 0;
>              printmenu;
>          End;
>Function option2 (choice : char) : char;
>          begin
>               writeln('You chose letter b!');
>               printmenu;

>          end;
>Function option3(choice : char) : char;
>         Begin
>              writeln('You chose option3');
>              option3 := choice;
>              printmenu(c);

>         End;
>Function option4(choice : char) : char;
>         Begin
>              writeln('You chose :', choice);
>              option4 := choice;
>         End;
>Function option5(choice : char) : char;
>         Begin
>              writeln('You chose :', choice);
>              option5 := choice;
>         End;
>Function option6(choice : char) : char;
>         Begin
>              writeln('You chose :', choice);
>              option6 := choice;
>         End;
>Function option7(choice : char) : char;
>         Begin
>              writeln('You chose :', choice);
>              option7 := choice;
>         End;
>Function option8(choice : char) : char;
>         Begin
>              writeln('You chose :', choice);
>              option8 := choice;
>         End;
>Function menu (menuchoice:char):char;
>         begin
>              CASE menuchoice of
>               'a' : option1(a);
>               'b' : option2(b);
>               'c' : option3(c);
>               'd' : option4(d);
>               'e' : option5(e);
>               'f' : option6(f);
>               'g' : option7(g);
>               'h' : option8(h);
>               ELSE
>              writeln(menuchoice, 'is not valid input.');
>            END

>            END;

>Procedure printmenu (var mchoice:char);

>         begin
>              writeln('a)  Enter choice 1');
>              writeln('b)  Enter choice 2');
>              writeln('c)  Enter choice 3');
>              writeln('d)  Enter choice 4');
>              writeln('e)  Enter choice 5');
>              writeln('f)  Enter choice 6');
>              writeln('g)  Enter choice 7');
>              writeln('h)  Enter choice 8');
>              writeln('q)  to quit.  ');
>              writeln('******************');
>              writeln('Choice?...');

Remove->>              read(mchoice);
Remove->>              menu(mchoice);

* I would leave out these two last lines from this procedure.
* I find it confusing to do anything else but print the menu
* in a procedure called 'printmenu'.

- Show quoted text -

Quote:
>         END;

> (* end functions*)

>Begin
>          initmenu(a);
>          read(menuchoice);
>          while menuchoice <> q do

>          menu(menuchoice);

>END.

I would rephrase this to read:

        initmenu(a);  {* get rid of that a, please *}
        menuchoice:=' '; {* a dummy initialization *}
        while menuchoice <> 'q' do {* you probably meant 'q' instead of q *}
        begin
           read(menuchoice);
           menu(menuchoice);
        end;

It seems to me that at some points you were trying to read the next
menuchoice from within one of the option functions. I think that is
bad programming and will obscure your code unnecessarily. Do all the
menu related stuff inside the menu loop of the main program! It may
well be that a previous menu choice will affect/restrict the future
choices as well, but there are better ways of dealing with this

There may be other problems with your code, as I didn't study it too
carefully. Learn to use the F7 and F8 -keys. Then you see the actual
flow of your program and can pinpoint the exact line of code that
causes the program to malfunction.

Jyrki Lahtonen, Ph.D.
Department of Mathematics,
University of Turku,
FIN-20014 Turku, Finland
Note to human e-mailers! To obtain my real e-mail address form the
string consisting of my first name, a period, my family name (names in
lower case) and an at-sign followed by utu.fi



Wed, 18 Jun 1902 08:00:00 GMT  
 help college student!
Robert III Lockhart used chalk and slate for

Quote:
>I have some code that will eventually run a menu driven program that I
>want to be able to recall the menu and rerun the program after each
>invidual session by the next read in menu choice. ... however I cannot
>get the code to line up correctly ( it will find one function and then
>not another because it is under that function in my listing of
>functions/procedures...)

Move PrintMenu up in your code so that it is above the
procedures/functions that call it or pre-declare Printmenu with
        Procedure PrintMenu; forward;

--
Pedt

Never curse the Crocodile's mother before crossing the river



Wed, 18 Jun 1902 08:00:00 GMT  
 help college student!

Quote:
> I have some code that will eventually run a menu driven program that I
> want to be able to recall the menu and rerun the program after each
> invidual session by the next read in menu choice. ... however I cannot
> get the code to line up correctly ( it will find one function and then
> not another because it is under that function in my listing of
> functions/procedures...) .What I would like some help on, is to help me
> figure out this recursive function in pascal, I know that it is easily
> possible in c by the switch function, however I'm getting frustrated
> with this one!

   Since you did a lot of work on this before you posted a plea for help
(something we see little of but which I respect and will gladly offer
aid), I have taken the liberty of reorganizing, simplifying and
correcting your program.  Note the following:
  1. I used CRT instead of WINCRT.
  2. You're confused about variables and constants: you used variables
(a,b,c, etc.) as though they were the values of their names.  Yet you
don't assign "a' to a, "b" to b, etc., so those uninitialized variables
won't have the contents you're expecting.  Thus, I removed all those
global variables and changed references to constants.
  3. Any user program should account for bad (or uninformed) user input.  
You expect 'a, 'b', etc as input, but the user may type 'A', 'B', etc.  
Your program should force a common case (UPPER is best) and operate
accordingly.  
  4. I added GetChar and Pause to handle common input processing.  Any
such routine must assure it handles Special/Extended keystrokes...by
processing #00 and the scan codes.
  5. As you can see, the whole issue of subprogram scope which you were
fighting can be handled simply, and with less code.  Study the code below
carefully to see how it's commonly done...
   The program below works as much as your logic contained, and you can
use this scaffold to expend as needed.

Program Assignment1;
uses crt;
var MenuChoice : char;

procedure Pause;
var C1,C2 : char;
begin
  gotoXY (1,24); write('Press any key to continue...'); C1 := ReadKey;
  if C1 = #00 then C2 := ReadKey                   { flush scan code }
end;  { Pause }

function GetChar : char;
var C1,C2 : char;
begin
  C1 := UpCase(ReadKey);
  if C1 = #00 then C2 := ReadKey;                 { flush scan code }
  GetChar := C1
end;  { GetChar }

Procedure InitMenu;
begin
  ClrScr;
  writeln('*********************');
  writeln('Welcome to my program');
  writeln('*********************');
  writeln('.By: Robert Lockhart.');
  writeln('.....May 24, 1999....');
  writeln('...Data Structures ..');
  writeln('**...Project 1....***');
  writeln('*****Instructor******');
  writeln('******Bob David******');
  writeln('******************'); Pause
end;

Function Option1 : integer;
var ans : char;
Begin
  writeln(MenuChoice, ')  Are you sure you want this? (Y,n) ');
  ans := GetChar;
  if ans = 'N' then Option1 := 1
  else
    begin
      writeln('ok, here you go for Option1'); Option1 := 0
    end
end;  { Option1 }

Function Option2 : char;
begin
  writeln('You chose letter b!'); Pause
end;

Function Option3(choice : char) : char;
Begin
  writeln('You chose Option3'); Option3 := choice; Pause
End;

Function Option4(choice : char) : char;
Begin
  writeln('You chose :', choice); Option4 := choice; Pause
End;

Function Option5(choice : char) : char;
Begin
  writeln('You chose :', choice); Option5 := choice; Pause
End;

Function Option6(choice : char) : char;
Begin
  writeln('You chose :', choice); Option6 := choice; Pause
End;

Function Option7(choice : char) : char;
Begin
  writeln('You chose :', choice); Option7 := choice; Pause
End;

Function Option8(choice : char) : char;
Begin
  writeln('You chose :', choice); Option8 := choice; Pause
End;

Procedure PrintMenu;
begin
  ClrScr;
  writeln('a)  choice 1');
  writeln('b)  choice 2');
  writeln('c)  choice 3');
  writeln('d)  choice 4');
  writeln('e)  choice 5');
  writeln('f)  choice 6');
  writeln('g)  choice 7');
  writeln('h)  choice 8');
  writeln('q)  to quit.  ');
  writeln('******************'); writeln('Choice?...')
END;  { PrintMenu }

Begin
  initmenu;
  repeat
    PrintMenu; MenuChoice := GetChar;
    CASE MenuChoice of
     'A' : Option1;
     'B' : Option2;
     'C' : Option3(MenuChoice);
     'D' : Option4(MenuChoice);
     'E' : Option5(MenuChoice);
     'F' : Option6(MenuChoice);
     'G' : Option7(MenuChoice);
     'H' : Option8(MenuChoice);
     'Q' : ;
     ELSE
       begin writeln(MenuChoice, 'is not valid input.'); Pause end
    END
  until MenuChoice = 'Q';
  writeln ('End of program...')
END.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. HELP A college student!

2. To all you college students out there!

3. College needs help tpb7 where to put

4. College Help PLease?

5. College Help

6. Not a student but would like help with Pascal (Need Help)

7. a student to help???

8. please help a student w/ Pics and sound!

9. Student help--records, linked lists, sorting

10. Help (aics student)

11. Help for student

12. Not a student but would like help with Pascal

 

 
Powered by phpBB® Forum Software