Solved

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

Posted on 2003-12-10
6
221 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 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pascal equivalent of VB Select/case 3 346
How do you get the IP address of a network? 12 289
Virtuailstring tree compare node issue 14 137
code issue 8 185
Facebook has became the #1 social media platform. People share many funny videos there, yet you don't know how to download them? Now you can download Videos from Facebook in just 3 simple steps.
Read the original post on Monitis Blog. Believe it or not, the most important thing about the website of your business is not what’s on it but how fast it loads. Yes, that’s right!    As you can see on this infographic (an oldie but goodie!), …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 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