Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
pascal programming problem Posted by rrwe on 19 Dec 2012 at 7:20 AM
Please take a look: https://www.dropbox.com/sh/3r788fmlgxq14b3/n9TefWkzVD
I've tried many times but still it fails to show the statement 'Sorry! You are under 18 so cannot join this voting event.'

Also, the ID numbers that entered won't save into the text file. The voting record won't be updated if success entering the correct data.

Can anyone help me? urgent! Thanks a lot!

Report
Re: pascal programming problem Posted by Actor21 on 19 Dec 2012 at 6:34 PM
How about posting your code here instead of posting a link?
Report
Re: pascal programming problem Posted by quikcarlx on 21 Dec 2012 at 8:16 PM
You need to change your functions from the Result to the name of the function. I don't know how you got your
program to compile. I changed your first function and left the rest for you to do. And I also got a copy of the program to be put on here.
program voting;

Uses Crt;

const
   pw = '123456';
   currentyr = 2012;

type
   VoteType = record
                 Songtitle : string[30];
                 Singer : string[30];
                 voted:integer;
              end;

var
   choice1: string;
   ans: string;
   Votefile, idfile, pfile: Text;
   idno: array[1..50] of string;
   Vote: array[1..50] of VoteType;
   v, MAX_Vote, MAX_idno: Integer;

function UpperCase ( value : string ): string;

   var
     i : integer;
     temp : string;
	 
   begin
     
     for i := 1 to Length( value ) do
       temp[i] := UpCase( value[i] );
	   
     UpperCase := temp  
   end;

function TryStrToInt(Value: String; var Number: Integer): Boolean;
var code: Integer;
begin
  Val(Value,Number,Code);
  Result:=(Code=0);
end;

function FileExists(FileName: string): boolean;
var f: File;
begin
  {$I-}
  Assign(f,FileName);
  Reset(f);
  Close(f);
  Result:=(IOResult=0);
  {$I+}
end;

function OpenOrCreateDB(FileName: string): boolean;
begin
  Result:=FileExists(FileName);
  Assign(Votefile, FileName);
  if Result then
    Reset(Votefile)
  else
    ReWrite(Votefile);
end;

function OpenOrCreateDB2(FileName: string): boolean;
begin
  Result:=FileExists(FileName);
  Assign(idfile, FileName);
  if Result then
    Reset(idfile)
  else
    ReWrite(idfile);
end;

procedure InputID(var count2 : integer);
begin
  if OpenOrCreateDB2('C:\Dev-Pas\sba\id.txt') then
  begin
    count2:=0;
    while not eof(idfile) do
    begin
      count2:=count2+1;
      readln(idfile, idno[count2]);
    end;
  end;
  Close(idfile);
end;

procedure SaveID(count2: integer);
var
   i: integer;
begin
   rewrite(idfile);
   for i:=1 to count2 do
      writeln(idfile,idno[i]);
   close(idfile);
end;

procedure InsertID(var count2 : integer);
begin
  append(idfile);
  count2:=count2+1;
  writeln(idfile,idno[count2]);
  close(idfile);
  SaveID(Max_idno);
end;

procedure BubbleSortByID(var count2 : integer);
var temp: string;
    swaps: Boolean;
    s,e,n1,n2: integer;
begin
  e:=count2-1;
  swaps:=true;
  while (e>Low(idno)) and (swaps) do
  begin
    Swaps:=False;
    for s:=Low(idno) to e do
    begin
      TryStrToInt(copy(idno[s],2,7),n1);    {convert to number, or 0 if error}
      TryStrToInt(copy(idno[s+1],2,7),n2);  {convert to number, or 0 if error}
      if (ord(idno[s][1]) > ord(idno[s+1][1])) and (n1 > n2) then
	  begin
        Swaps:=True;
        temp := idno[s];
        idno[s] := idno[s+1];
        idno[s+1] := temp
      end;
    end;
    e:=e-1;
  end;
end;

function SearchID(target:string; count:integer):Boolean;
var
   top,bottom,middle:integer;
   found:Boolean;
begin
{binary search algorithm}
  BubbleSortByID(count);

  found:=false;
  count:=0;
  top:=1;
  bottom:=count;
  repeat
    middle:=(top+bottom) div 2;
    if UpperCase(target) > UpperCase(idno[middle]) then
      top := middle + 1
    else
      if UpperCase(target) < UpperCase(idno[middle]) then
        bottom := middle - 1
      else found:=true;
  until (found) or (top > bottom);
  Result := found;
end;

procedure InputRecord(var count : integer);
begin
{read all records from Votefile into Vote[] array}
  if OpenOrCreateDB('C:\Dev-Pas\sba\vote.txt') then
  begin
    count:=0;
    while not EOF(Votefile) do
    begin
      count:=count+1;
      with Vote[count] do
      begin
        readln(Votefile, Songtitle);
        readln(Votefile, Singer);
        readln(Votefile, voted);
      end;
    end;
  end;
  Close(Votefile);
end;

procedure SaveRecord(Filename: string);
var
   i: integer;
begin
   Assign(Votefile, FileName);
   ReWrite(Votefile);
   for i:=1 to MAX_Vote do
     if Vote[i].Songtitle <> '$$$' then
     begin
       writeln(Votefile,Vote[i].Songtitle);
       writeln(Votefile,Vote[i].Singer);
       writeln(Votefile,Vote[i].voted);
     end;
   Close(Votefile);
end;

procedure DisplayRecord(count:integer);
var
   index, align1: integer;
begin
   ClrScr;
   writeln( 'Displaying ',MAX_Vote,' Candidates Records');
   writeln( 'Song Title              Singer        Vote');
   for index:=1 to count do
      with Vote[index] do
      begin
         align1:= 25-length(Songtitle);
         writeln( Songtitle, '':align1, Singer,'':6,Voted)
      end;

   writeLn( 'Press enter key to continue...');
   readln;
end;

procedure InsertRecord(var count: integer);
var
   ans,tryagain:char;
   target:string;
   found:boolean;
   i,temp:integer;
begin
   writeln('Insert Candidate Records');
   append(Votefile);
   repeat
      repeat
         write( 'Enter song title: ');
         readln(target);
         found:=false;
         i:= 0;
         while (i<Max_vote) and (not found) do
            begin
               i:=i+1;
               with Vote[i] do
                  if UpperCase(Songtitle) = UpperCase(target)
                     then begin
                             temp := i;
                             found:=true;
                             write('This song is already in the list. Do you want to try again? (Y/N): ');
                             readln(tryagain)
                          end;
            end;
      until (tryagain in ['N','n']) or (not found);
   if (not found)
      then begin
              count := count +1;
              with Vote[count] do
                 begin
                    Songtitle:=target;
                    write('Enter singer''s name: ');
                    readln(Singer);
                    voted:=0;
                 end;
              write('Input another candidates (Y/N): ');
              readln(ans)
           end;
   until (ans in ['N','n']);
   close(Votefile);
end;

procedure DeleteRecord(var count : integer);
var
   ans: char;
   target:string[30];
   found:boolean;
   MAX_Vote,index:integer;
begin
   writeln('Delete Candidate Records');
   write('Enter song title to be deleted: ');
   readln(target);
   found:=false;
   MAX_Vote:=0;
   repeat
      MAX_Vote:=MAX_Vote+1;
      with Vote[MAX_Vote] do
         if Songtitle = target
            then begin
                    index :=MAX_Vote;
                    found:=true
                 end
   until found or (MAX_Vote = count);
   if found
      then begin
              write('Candidate record found! Are you sure to delete (Y/N)?');
              readln(ans);
              if ans = 'Y'
                 then begin
                         with Vote[index] do
                            Songtitle:='$$$';
                         count:=count-1;
                         writeln('Candidate record deleted!')
                      end
           end
      else writeln('Song title not found!')
end;



procedure AmendRecord(var count:integer);
var
   target:string[30];
   found:boolean;
   n,index:integer;
begin
   writeln('Amend Candidate Records');
   write('Enter song title to be amended: ');
   readln(target);
   found:=false;
   n:= 0;
   while (n<count) and (not found) do
   begin
      n:=n+1;
      with Vote[n] do
         if UpperCase(Songtitle) = UpperCase(target) then
         begin
            index := n;
            found:=true;
         end;
   end;
   if found then
   begin
      writeln('Candidate record found! Please enter the edited information below.');
      with Vote[index] do
      begin
         write('Enter song title: ');
         readln(Songtitle);
         write('Enter singer''s name: ');
         readln(Singer);
         write('Enter the starting number of the vote: ');
         readln(voted);
         writeln('Record updated!')
      end;
   end else
     writeln('Song Title "',target,'" not found!');
end;

function LocateVoteRecord(Target: string; var Index: integer): Boolean;
var i: integer;
begin
{locate a vote record by song title}
  Result:=False; Index:=-1;
  if (Target<>'') and (MAX_Vote>0) then
  begin
    i:=1;
    while (i<=MAX_Vote) and (not Result) do
      if UpperCase(Target)=UpperCase(Vote[i].Songtitle) then
      begin
        Result:=True;
        Index:=i;
      end else
        i:=i+1;
  end; {if}
end;

procedure VoteRecord(var count:integer);
var
   target: string;
   index: integer;
begin
   write('Enter the song title to be voted: ');
   readln(target);

   If LocateVoteRecord(target,index) then
   begin
     writeln('Candidate record found!');
     Vote[index].voted := Vote[index].voted + 1;
     writeln('Record updated! Thank you for your vote!')
  end else
    writeln('Candidate''s number not found!')
end;

procedure Printreport;
var
   index, align1,align2,s: integer;
   swaps: Boolean;
   temp:votetype;
   pass,i:integer;
begin
   assign(pfile,'C:\Dev-Pas\sba\print.txt');
   rewrite(pfile);
   writeln(pfile, 'Song Title             Singer        Vote');
   pass:=0;
   repeat
      pass:=pass +1;
      swaps:=false;
       for i:=1 to MAX_Vote - pass do
       if vote[i].Voted < vote[i+1].Voted
          then
             begin
             temp:=Vote[i];
             Vote[i]:= Vote[i+1];
             Vote[i+1]:=temp;
             Swaps:=True;
             end;
   until (pass= MAX_vote-1) or not swaps;
   for index := 1 to MAX_Vote do
   with Vote[index] do
      begin
         align1:= 25-length(Songtitle);
         align2:= 20-length(singer);
         writeln(pfile,Songtitle, '':align1, Singer,'':align2,Voted)
      end;
   close(pfile);
end;

function CalcCheckData(HKID: string): Integer;
var n: array[1..8] of Integer;
    i,j,v: Integer;
begin
{calculate the check data from VALID HKID}
  if Length(HKID)=8 then
  begin
{first character, a=1..z=26}
    n[1] := Ord(UpCase(HKID[1]))-64;
{next six must be numeric}
    for i:=2 to 7 do
      TryStrToInt(copy(HKID,i,1),n[i]);
{last can be a=10 or number}
    if not TryStrToInt(copy(HKID,8,1),n[8]) then n[8]:=10;
{do some math on the values}
    j:=8; v:=0;
    for i:=1 to 7 do
    begin
      v := v + (n[i]*j);
      j:=j-1;
    end;
{calc remainder}
    Result:=(v + n[8]) mod 11;
  end;
end;

function HKIDValid(Format,HKID: String): boolean;
var i,l,v: integer;
begin
  l:=Length(HKID);
{check length (must be same as length of format string)}
  Result:=(Length(Format)=l);
{if valid, check characters against format string}
  if Result then
  begin
    i:=1;
    while (i<l) and (Result) do
    begin
      case Format[i] of
        '0': Result:=TryStrToInt(HKID[i],v); {0 means character must be number only (0..9)}
    'A','a': Result:=(HKID[i] in ['A'..'Z','a'..'z']); {A means character must be letter only (a..z)}
        '?': Result:=(HKID[i] in ['A'..'Z','a'..'z','0'..'9']); {? means almost any character}
        '.': Result:=(HKID[i] in ['0'..'9','A','a']); {special case}
      end; {case}
      i:=i+1;
    end; {while}
  end; {if}
end;

procedure ChoiceIs2;
var idno: string;
    yr: integer;
    tryagain: char;
    finished: boolean;
begin
   writeln('Please enter your personal information as identification.');
   writeln;
   write('Enter the year of your birth: ');
   readln(yr);
   if ((currentyr-yr) < 18) then
     writeln('Sorry! You are under 18 so cannot join this voting event. Press enter key to quit...')
     else begin
     tryagain:=#0;
     finished:=false;
     repeat
       write('Enter your HKID card [sample: A123456(3)]: ');
       readln(idno);
     if SearchID(idno, MAX_idno)
        then writeln('Sorry! You are not allowed to vote twice.')
        else if (HKIDValid('A000000.',idno)) and (CalcCheckData(idno)=0)
                then begin
                        insertID(MAX_idno);
                        InputRecord(MAX_Vote);
                        DisplayRecord(MAX_Vote);
                        VoteRecord(MAX_Vote);
                        finished:=true;
                     end
                else begin
                        write( 'Invalid input. Do you want to try again? (Y/N): ' );
                        readln(tryagain);
                     end;
     until (tryagain in ['N','n']) or (SearchID(idno, MAX_idno)) or finished;
     if finished
     then writeln('Success! Press enter key to quit...');
          readln;
   end;
end;

procedure PasswordIsRight;
begin
  v:=0;
  repeat
    ClrScr;
    writeln('1. Insert Records');
    writeln('2. Delete Records');
    writeln('3. Amend Records');
    writeln('4. Display Records');
    writeln('5. Print Report');
    writeln('6. Quit');
    writeln;
    write('Enter your choice (1-6):');
    readln(choice1);
    writeln;
    TryStrToInt(choice1,v);
    case v of
      1: InsertRecord(MAX_Vote);
      2: DeleteRecord(MAX_Vote);
      3: AmendRecord(MAX_Vote);
      4: DisplayRecord(MAX_Vote);
      5: Printreport;
    end;
  until v = 6;
end;

procedure ChoiceIs1;
var attempts: integer;
begin
  attempts:=0;
  repeat
    write('Please enter the password: ');
    readln(ans);
    if ans<>pw then
    begin
      writeln('Sorry! Wrong password, you have ',3-attempts,' attempts left. Please try again!');
      attempts:=attempts+1;
    end;
  until (attempts>3) or (ans=pw);

  if ans=pw then
    PasswordIsRight;
end;

function MainMenu: integer;
begin
  ClrScr;
  TextColor(6);
  writeln( 'Music Voting System');
  TextColor(19);
  writeln( '[',MAX_Vote,' records found]');
  TextColor(14);
  writeln;
  TextColor(white);
  writeln( '1. Management Interface (password is needed)');
  writeln( '2. Vote ');
  writeln( '3. Quit');
  writeln;
  write('Enter your choice (1-3): ');
  readln(choice1);
  TryStrToInt(choice1,Result);
end;

begin
  InputRecord(MAX_Vote);
  InputID(MAX_idno);

  repeat
    v := MainMenu;

    case v of
      1: ChoiceIs1;
      2: ChoiceIs2;
      3:;
    else
      WriteLn('Invalid choice, please try again.');
    end;
  until v=3;

  SaveRecord('C:\Dev-Pas\sba\vote.txt');
end.





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.