Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2003-12-10
6
Medium Priority
?
239 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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Sometimes Administrators rights are not enough. These cases call for the SYSTEM account. The process in this article outlines the steps required to execute commands using the SYSTEM account.
Tech spooks aren't just for those who are tech savvy, it also happens to those of us running a business. Check out the top tech spooks for business owners.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

604 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