?
Solved

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

Posted on 2003-12-10
6
Medium Priority
?
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 49

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Accepted Solution

by:
griessh earned 300 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 101

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 101

Expert Comment

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

mlmcc
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
What's worse than having your data encrypted by ransomware? Getting attacked by a so-called "wiper," which simply destroys the data and offers you no hope of ever seeing it again.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 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