Solved

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

Posted on 2003-12-10
6
202 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 47

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this step by step tutorial with screenshots, we will show you HOW TO: Enable SSH Remote Access on a VMware vSphere Hypervisor 6.5 (ESXi 6.5). This is important if you need to enable SSH remote access for additional troubleshooting of the ESXi hos…
We have come a long way with backup and data protection — from backing up to floppies, external drives, CDs, Blu-ray, flash drives, SSD drives, and now to the cloud.
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

14 Experts available now in Live!

Get 1:1 Help Now