Passwording a pascal program

I need to simply integrate a password system into a database type pascal program. It is very simple though. I just having trouble.

The password is entered at start of program. And every menu option except 1. it asks for the password. However, every menu option after the first choice leads the the result of inputting the correct password.

Anybody help?

[code]program Assignment_1;
uses crt;
label 1;

type
date = record
day: string;
month: string;
year: string;
end;

type
personfile = record
firstname: string;
surname: string;
DOB: date;
form: string;
yeargroup: string;
college: string;
end;

var
password, p1: string;
input: array[1..10] of personfile;
menuchoice, choice2, confirm: char;
nop, i, j: integer; {nop = number of people}


begin

writeln('Please enter a password to protect your database.');
readln(password);


{Menu}
1:
p1:='jh34jh';
clrscr;
writeln (' Menu');
writeln();
writeln('1. Add a Student');
writeln('2. View Students information');
writeln('3. Amend a Students Details');
writeln('4. Delete a Student');
writeln();
writeln('5. Exit');

menuchoice:= Readkey;
{End of Menu}


{Choice 1 - Add student}
If menuchoice = '1' then

begin
clrscr;
writeln('Do you want to add a student? (Y/N)'); {This confirms if you want to add a student, otherwise they will have to go through the long adding process}
confirm:= readkey;
if confirm = 'n' then
goto 1;

if confirm = 'y' then
clrscr;
writeln('How many students are you adding? (1-10)');
readln(nop);
for i:=1 to nop do
begin {entering values - i have used an array for this}
clrscr; {i is the main integer my program uses for array numbers}
writeln('Enter student ',i,'`s First Name');
readln(input[i].firstname);
writeln('Enter student ',i,'`s Surname');
readln(input[i].surname);
writeln('Enter student ',i,'`s Day of birth');
readln(input[i].DOB.day);
writeln('Enter student ',i,'`s Month of birth'); {DOB needs to be entered separatley}
readln(input[i].DOB.month);
writeln('Enter student ',i,'`s Year of birth');
readln(input[i].DOB.year);
writeln('Enter student ',i,'`s Form');
readln(input[i].form);
writeln('Enter student ',i,'`s Year Group');
readln(input[i].yeargroup);
writeln('Enter student ',i,'`s College');
readln(input[i].college);
end;

goto 1;
end;
{Choice 2 - View Info}
If menuchoice = '2' then

repeat
clrscr;
writeln('Please enter the password');
readln(p1);
until p1 = password;

begin
clrscr;
for i:=1 to nop do
begin
writeln('Student ',i);
writeln();
writeln(input[i].firstname, ' ', input[i].surname);
writeln(input[i].DOB.day, '/', input[i].DOB.month, '/', input[i].DOB.year);
writeln('Form: ' ,input[i].form);
writeln('Year: ' ,input[i].yeargroup);
writeln('College: ', input[i].college);
writeln();
writeln('-------------------------------------------------------');
end;
readln();
goto 1;
end;
{Choice 3 - Amend}
If menuchoice = '3' then

repeat
clrscr;
writeln('Please enter the password');
readln(p1);
until p1 = password;

begin
clrscr;
for i:=1 to nop do
begin
writeln(i, '. ', input[i].firstname, ' ', input[i].surname);
end;
writeln();
writeln('Which student would you like to amend?');
readln(i);
writeln('Which part would you like to edit?');
writeln('1. Name');
writeln('2. DOB');
writeln('3. Form');
writeln('4. Year');
writeln('5. College');
choice2:=readkey;

if choice2 = '1' then
begin
writeln('Enter student ',i,'`s First Name');
readln(input[i].firstname);
writeln('Enter student ',i,'`s Surname');
readln(input[i].surname);
end;
if choice2 = '2' then
begin
writeln('Enter student ',i,'`s Day of birth');
readln(input[i].DOB.day);
writeln('Enter student ',i,'`s Month of birth');
readln(input[i].DOB.month);
writeln('Enter student ',i,'`s Year of birth');
readln(input[i].DOB.year); {This part is rather untidy but it is simply just a menu choice with 5 options.}
end;
if choice2 = '3' then
begin
writeln('Enter student ',i,'`s Form');
readln(input[i].form);
end;

if choice2 = '4' then
begin
writeln('Enter student ',i,'`s Year Group');
readln(input[i].yeargroup);
end;

if choice2 = '5' then
begin
writeln('Enter student ',i,'`s College');
readln(input[i].college);
end;
goto 1;
end;

{Choice 4 - Delete}
If menuchoice = '4' then

repeat
clrscr;
writeln('Please enter the password');
readln(p1);
until p1 = password;
begin
clrscr;
for i:=1 to nop do
begin
writeln(i, '. ', input[i].firstname, ' ', input[i].surname);
end;
writeln();
writeln('Please enter the number of the student you would like to delete');
readln(j);
for i:=j to nop do
begin
input[j].firstname:=input[j+1].firstname;
input[j].surname:=input[j+1].surname;
input[j].DOB.day:=input[j+1].DOB.day; {Here the selection to delete is j, this coding makes j+1 move to space j}
input[j].DOB.month:=input[j+1].DOB.month;
input[j].DOB.year:=input[j+1].DOB.year;
input[j].form:=input[j+1].form;
input[j].yeargroup:=input[j+1].yeargroup;
input[j].college:=input[j+1].college;
j:=j+1 {It then makes j= j+1 ,so each box moves down a place.}

end;

goto 1;
end;
{Exit Choice}
If menuchoice = '5' then
begin
clrscr;
writeln('Do you really want to exit? (Y/N)');
confirm:= Readkey; {Basic exit command, asks for a confirm then halts if user select}
if confirm = 'n' then
goto 1;

if confirm = 'y' then
halt;
end


else
begin
goto 1; {Stops user selecting anything other than 1-5}
end;


end.[/code]

Comments

  • : I need to simply integrate a password system into a database type
    : pascal program. It is very simple though. I just having trouble.
    :
    : The password is entered at start of program. And every menu option
    : except 1. it asks for the password. However, every menu option after
    : the first choice leads the the result of inputting the correct
    : password.
    :
    [blue]
    Your main problem is that your code is not modular, having around 250 lines of code in the main procedure, making the entire program much more complex than it needs to be. This is my suggested approach. Make the password a constant.
    [/blue]
    [code]
    program Assignment_1 ;

    uses
    crt ;

    const
    P1 = 'jh34jh' ;

    type
    date = record
    day : string ;
    month : string ;
    year : string ;
    end ;

    type
    personfile = record
    firstname : string ;
    surname : string ;
    DOB : date ;
    form : string ;
    yeargroup : string ;
    college : string ;
    end ;

    var
    password : string ;

    procedure AddStudent ; { stub }
    begin
    end ;

    procedure ViewStudent ; { stub }
    begin
    end ;

    procedure AmendStudent ; { stub }
    begin
    end ;

    procedure DeleteStudent ; { stub }
    begin
    end ;

    var
    menuchoice,
    confirm : char ;

    begin
    writeln('Please enter a password to protect your database.') ;
    readln(password) ;

    if password = P1 then
    repeat
    {
    Menu
    }
    repeat
    clrscr ;
    writeln (' Menu') ;
    writeln ;
    writeln('1. Add a Student') ;
    writeln('2. View Students information') ;
    writeln('3. Amend a Students Details') ;
    writeln('4. Delete a Student') ;
    writeln ;
    writeln('5. Exit') ;

    repeat
    menuchoice:= Readkey
    until menuchoice IN ['1' .. '5'] ;
    { End of Menu }

    case menuchoice of
    '1' : AddStudent ;
    '2' : ViewStudent ;
    '3' : AmendStudent ;
    '4' : DeleteStudent ;
    end ; { case }
    until menuchoice = '5' ;
    {
    Exit Choice
    -- action for '5' is here instead of in case construct
    }
    clrscr ;
    writeln('Do you really want to exit? (Y/N)') ;
    repeat
    confirm:= Readkey ; { Basic exit command, asks
    for a confirm then halts if
    user selects 'N' }
    until upcase (confirm) IN ['N' .. 'Y']
    until confirm = 'Y'
    end.
    [/code]
  • : [code]: program Assignment_1;
    : uses crt;
    : label 1; [red]{ here's the beginning of your problems.
    try to avoid label and goto }[/red]
    :
    : type
    : date = record
    : day: string;
    : month: string;
    : year: string;
    : end;
    :
    : type
    : personfile = record
    : firstname: string;
    : surname: string;
    : DOB: date;
    : form: string;
    : yeargroup: string;
    : college: string;
    : end;
    :
    : var
    : password, p1: string;
    : input: array[1..10] of personfile;
    : menuchoice, choice2, confirm: char;
    : nop, i, j: integer; {nop = number of people}
    :
    :
    : begin
    :
    : writeln('Please enter a password to protect your database.');
    : readln(password);
    :
    :
    : {Menu}
    : 1:
    : p1:='jh34jh';
    : clrscr;
    : writeln (' Menu');
    : writeln();
    : writeln('1. Add a Student');
    : writeln('2. View Students information');
    : writeln('3. Amend a Students Details');
    : writeln('4. Delete a Student');
    : writeln();
    : writeln('5. Exit');
    :
    : menuchoice:= Readkey;
    : {End of Menu}
    :
    :
    : {Choice 1 - Add student}
    : If menuchoice = '1' then
    :
    : begin
    : clrscr;
    : writeln('Do you want to add a student? (Y/N)'); {This confirms if you want to add a student, otherwise they will have to go through the long adding process}
    : confirm:= readkey;
    : if confirm = 'n' then
    : goto 1;
    :
    : if confirm = 'y' then
    : clrscr;
    [red]{ continued indentation at this point
    suggests that you expect this code to be
    controlled by the above 'if' but it is not.
    the above 'if' controls only the 'clrscr' }[/red]
    : writeln('How many students are you adding? (1-10)');
    : readln(nop); [red]{ as far as I can tell this is
    the only place in the entire program
    where 'nop' is assigned a value,
    yet it is possible to reach other
    parts of the program where 'nop' is
    referenced without ever having gone
    through this assignment, meaning that
    'nop' can be used when it contains
    garbage. I think this is part of your
    problem. }[/red]



    [red]{ why increase indentation
    at this point? there's no control
    statement above. }[/red]
    : for i:=1 to nop do
    : begin {entering values - i have used an array for this}
    : clrscr; {i is the main integer my program uses for array numbers}
    : writeln('Enter student ',i,'`s First Name');
    : readln(input[i].firstname);
    : writeln('Enter student ',i,'`s Surname');
    : readln(input[i].surname);
    : writeln('Enter student ',i,'`s Day of birth');
    : readln(input[i].DOB.day);
    : writeln('Enter student ',i,'`s Month of birth'); {DOB needs to be entered separatley}
    : readln(input[i].DOB.month);
    : writeln('Enter student ',i,'`s Year of birth');
    : readln(input[i].DOB.year);
    : writeln('Enter student ',i,'`s Form');
    : readln(input[i].form);
    : writeln('Enter student ',i,'`s Year Group');
    : readln(input[i].yeargroup);
    : writeln('Enter student ',i,'`s College');
    : readln(input[i].college);
    : end;
    :
    : goto 1;
    : end;
    : {Choice 2 - View Info}
    : If menuchoice = '2' then [red]{ use 'else if', not just 'if' }[/red]
    :
    : repeat
    : clrscr;
    : writeln('Please enter the password');
    : readln(p1);
    : until p1 = password;
    :
    : [b]begin[/b] [red]{ here's the heart of the problem. I think
    you want this block to be controlled by
    the 'if' statement above, but it is not.
    the above 'if' controls only the repeat .. until
    loop. this block will be executed regardless. }[/red]
    : clrscr;
    : for i:=1 to nop do
    : begin
    : writeln('Student ',i);
    : writeln();
    : writeln(input[i].firstname, ' ', input[i].surname);
    : writeln(input[i].DOB.day, '/', input[i].DOB.month, '/', input[i].DOB.year);
    : writeln('Form: ' ,input[i].form);
    : writeln('Year: ' ,input[i].yeargroup);
    : writeln('College: ', input[i].college);
    : writeln();
    : writeln('-------------------------------------------------------');
    : end;
    : readln();
    : goto 1;
    : end;
    : {Choice 3 - Amend}
    : If menuchoice = '3' then [red]{ use 'else if' }[/red]
    :
    : repeat
    : clrscr;
    : writeln('Please enter the password');
    : readln(p1);
    : until p1 = password;

    [red]{ 'if' controls only the repeat .. until loop. }[/red]
    :
    : begin
    : clrscr;
    : for i:=1 to nop do
    : begin
    : writeln(i, '. ', input[i].firstname, ' ', input[i].surname);
    : end;
    : writeln();
    : writeln('Which student would you like to amend?');
    : readln(i);
    : writeln('Which part would you like to edit?');
    : writeln('1. Name');
    : writeln('2. DOB');
    : writeln('3. Form');
    : writeln('4. Year');
    : writeln('5. College');
    : choice2:=readkey;
    :
    : if choice2 = '1' then
    : begin
    : writeln('Enter student ',i,'`s First Name');
    : readln(input[i].firstname);
    : writeln('Enter student ',i,'`s Surname');
    : readln(input[i].surname);
    : end;
    : if choice2 = '2' then
    : begin
    : writeln('Enter student ',i,'`s Day of birth');
    : readln(input[i].DOB.day);
    : writeln('Enter student ',i,'`s Month of birth');
    : readln(input[i].DOB.month);
    : writeln('Enter student ',i,'`s Year of birth');
    : readln(input[i].DOB.year); {This part is rather untidy but it is simply just a menu choice with 5 options.}
    : end;
    : if choice2 = '3' then
    : begin
    : writeln('Enter student ',i,'`s Form');
    : readln(input[i].form);
    : end;
    :
    : if choice2 = '4' then
    : begin
    : writeln('Enter student ',i,'`s Year Group');
    : readln(input[i].yeargroup);
    : end;
    :
    : if choice2 = '5' then
    : begin
    : writeln('Enter student ',i,'`s College');
    : readln(input[i].college);
    : end;
    : goto 1;
    : end;
    :
    : {Choice 4 - Delete}
    : If menuchoice = '4' then [red]{ use 'else if' }[/red]
    :
    : repeat
    : clrscr;
    : writeln('Please enter the password');
    : readln(p1);
    : until p1 = password;
    [red]{ 'if' controls only the repeat .. until loop. }[/red]
    : begin
    : clrscr;
    : for i:=1 to nop do
    : begin
    : writeln(i, '. ', input[i].firstname, ' ', input[i].surname);
    : end;
    : writeln();
    : writeln('Please enter the number of the student you would like to delete');
    : readln(j);
    : for i:=j to nop do
    : begin
    : input[j].firstname:=input[j+1].firstname;
    : input[j].surname:=input[j+1].surname;
    : input[j].DOB.day:=input[j+1].DOB.day; {Here the selection to delete is j, this coding makes j+1 move to space j}
    : input[j].DOB.month:=input[j+1].DOB.month;
    : input[j].DOB.year:=input[j+1].DOB.year;
    : input[j].form:=input[j+1].form;
    : input[j].yeargroup:=input[j+1].yeargroup;
    : input[j].college:=input[j+1].college;
    : j:=j+1 {It then makes j= j+1 ,so each box moves down a place.}
    :
    : end;
    :
    : goto 1;
    : end;
    : {Exit Choice}
    : If menuchoice = '5' then [red]{ use 'else if' }[/red]
    : begin
    : clrscr;
    : writeln('Do you really want to exit? (Y/N)');
    : confirm:= Readkey; {Basic exit command, asks for a confirm then halts if user select}
    : if confirm = 'n' then
    : goto 1;
    :
    : if confirm = 'y' then
    : halt;
    : end
    :
    :
    : else
    : begin
    : goto 1; {Stops user selecting anything other than 1-5}
    : end;
    :
    :
    : end.[/code]:
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion