Solved

Comparing lines in a text file to see if they are the same

Posted on 2003-12-10
6
207 Views
Last Modified: 2010-04-16
I am using a textfile and reading them into an array of records, i want to see if the last element of the record, which is in the text file is the same as other parts of the textfile, heres my code so far:-

program Assignment_3;

{$APPTYPE CONSOLE}

uses
  SysUtils;
const
  maxCustomers = 50;
type
  customer=record
  gender:char;
  surname:string;
  forename:string;
  likes:string;
  hates:string;
end;

var
  customers:text;
  maleList:array [1..25] of customer;
  femaleList:array [1..25] of customer;
{------------------------------------------------------------------------------}

{------------------------------------------------------------------------------}
Procedure writeMales;
var i:integer;
    thisCustomer:customer;
Begin
  writeln;
  writeln('List of Males: Press enter to view more');
  writeln;
  for i:=1 to maxCustomers do begin
      thisCustomer:=maleList[i];
        if (thisCustomer.gender in ['M', 'm']) then begin
            Writeln(thisCustomer.forename);
            Writeln(thiscustomer.surname);
            writeln('-------------------');
    readln;
   end;
end;
readln;
end;
{------------------------------------------------------------------------------}
Procedure writeFemales;
var
  i:integer;
  thisCustomer:customer;
begin
  writeln;
  writeln('List of Females: Press enter to view more');
  writeln;
  for i:=1 to maxCustomers do begin
    thisCustomer:=femaleList[i];
      if (thisCustomer.gender in ['F', 'f']) then begin
        writeln(thisCustomer.forename);
        writeln(thisCustomer.surname);
        writeln('-------------------');
      readln;
      end;
  end;
readln;
end;
{------------------------------------------------------------------------------}
procedure readMales;
var
  i:integer;
  thisCustomer:customer;
begin
  assignfile(customers, 'customers.txt');
    reset(customers);
  for i:=1 to maxCustomers do begin
    thisCustomer:=maleList[i];
    readln(customers, thisCustomer.gender);
    readln(customers, thisCustomer.surname);
    readln(customers, thisCustomer.forename);
    readln(customers, thisCustomer.likes);
    readln(customers, thisCustomer.hates);
   maleList[i]:=thisCustomer;


  end;
  closefile(customers);
end;
{------------------------------------------------------------------------------}
 procedure readFemales;
var
  i:integer;
  thisCustomer:customer;
begin
  assignfile(customers, 'customers.txt');
    reset(customers);
  for i:=1 to maxCustomers do begin
    thisCustomer:=femaleList[i];
    readln(customers, thisCustomer.gender);
    readln(customers, thisCustomer.surname);
    readln(customers, thisCustomer.forename);
    readln(customers, thisCustomer.likes);
    readln(customers, thisCustomer.hates);
   femaleList[i]:=thisCustomer;
  end;
  closefile(customers);
end;
{------------------------------------------------------------------------------}
Procedure matchMale;
var
  thisCustomer:customer;
  i:integer;
begin
  assignfile(customers, 'customers.txt');
  reset(customers);
  for i:=1 to maxCustomers do begin
    thisCustomer:=maleList[i];
    readln(customers, thisCustomer.likes);
  closefile(customers);
  end;
end;
{------------------------------------------------------------------------------}
Procedure matchFemale;
var
  thatCustomer:customer;
  i:integer;
begin
  assignfile(customers, 'customers.txt');
  reset(customers);
  for i:=1 to maxCustomers do begin
    thatCustomer:=femaleList[i];
    readln(customers, thatCustomer.likes);
  closefile(customers);
  end;
end;
{------------------------------------------------------------------------------}
Procedure menu;
var
  choice:char;
begin
  repeat
    writeln('.::::::: Ian''s Dating Agency ::::::::.');
    writeln;
    writeln('>Print List of Male Customers.........1');
    writeln('>Print List of Female Customers.......2');
    writeln('>Find a match for a Male..............3');
    writeln('>Find a match for a Female............4');
    writeln('>Exit.................................5');
    writeln;
    readln(choice);
    case choice of
    '1' : writeMales;
    '2' : writeFemales;
    '3' : matchMale;
    //'4' : matchFemales;
    end;
  until choice = '5';
end;
{------------------------------------------------------------------------------}
begin
  readMales;
  readFemales;
  menu;
  matchMale;
  matchFemale;
  readln;
end.

So what I need help with is the match part, what I am trying to do is just get the malelist likes to match the female list likes.  Any help would  be greatly appreciated, please don't solve the whole program for me, and please try and avoid just pasting the code, I need just some help to show how to implement it.  many thanks
0
Comment
Question by:Jenko_2003
6 Comments
 
LVL 48

Expert Comment

by:dbrunton
ID: 9918540
Roughly something like - this is for males

if (person.likes = thiscustomer.likes) and (person.hates = thiscustomer.hates) and (thiscustomer.gender = 'F') then
writeln ('We have a match');

This is rough coding.
0
 

Author Comment

by:Jenko_2003
ID: 9919860
Ok this is what I now have, and it compiles and runs, but doesn't print the matches to the screen it just prints blank lines, any ideas?
0
 

Author Comment

by:Jenko_2003
ID: 9919863
My bad heres the procedure: -

Procedure matchMale;
var
  thisCustomer, person:customer;
  i:integer;
begin
  assignfile(customers, 'customers.txt');
  reset(customers);
  writeln ('List of Perfect matches...');
  for i:=1 to maxCustomers do begin
    thisCustomer:=maleList[i];
   if thisCustomer.gender in ['M', 'm'] then
    readln(customers, thisCustomer.gender);
    readln(customers, thisCustomer.surname);
    readln(customers, thisCustomer.forename);
    readln(customers, thisCustomer.likes);
    readln(customers, thisCustomer.hates);
    maleList[i]:=thisCustomer;

    person:=femaleList[i];
   if person.gender in ['F', 'f'] then
    readln(customers, person.gender);
    readln(customers, person.surname);
    readln(customers, person.forename);
    readln(customers, person.likes);
    readln(customers, person.hates);
    femaleList[i]:=person;
    if (thisCustomer.likes = person.likes)
      and (thisCustomer.hates = person.hates) then  begin
        writeln(thisCustomer.forename);
        writeln(thisCustomer.surname);
        writeln('is matched with...');
       writeln(person.forename);
        writeln(person.surname)
    {end else begin
               writeln('No matches at current');}
             end;
    readln;
  end;
  closefile(customers);
end;
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Accepted Solution

by:
griessh earned 100 total points
ID: 9931386
1) In your readMales/readFemales you have to check for the gender before you add the record to your list. Keep a count of how many men/women you have!
2) matchxxx doesn't have to read the file anymore since you have already 2 lists, one with all women, one with all men.
3) matchxxx in its simplest solution has to compare all men with all women:
  for i:=1 to maxMen do begin
      for j:=1 to maxWomen do begin
        {do your comarison maleList[i].attribute = femaleList[j].attribute}
      end;
  end;
4) For debugging it is always good to have some writeln for example in the readXXX function to see haw the lists fill up.

Good luck!

=====
Werner    
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 10189629
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

recommend - split dbrunton/griessh

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

mlmcc      
Pascal Page Editor
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 10190958
Thanks for closing this question.

mlmcc
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Knowledge base software has turned out to be a quite reliable method for storing information, promoting collaborative work and for sharing valuable input and solutions.However, some organizations are trying to develop a knowledge base that works wit…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now