This post was published long ago, when I was a student and an amateur blogger. The links might be outdated and content may not be useful anymore. Please read this content keeping its age in mind.
Program 7 : Write a C++ program to read two lists of names and then match the names in the two lists using Cosequential Match based on a single loop. Output the names common to both the lists.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<fstream.h>
#include<iostream.h>
void writeLists()
{
fstream out1,out2;
int i,m,n;
char name[20];
out1.open(“file1.txt”,ios::out);
out2.open(“file2.txt”,ios::out);
if( (!out1) || (!out2) )
{
cout<<“Unable to open one of the list files”;
getch();
exit(0);
}
cout<<“Enter no. of names you want to enter in file1”; cin>>m;
cout<<“nEnter the names in ascending ordern”;
for(i-0;i<m;i++)
{
cin>>name;
out1<<name;
out1<<‘n’;
}
cout<<“Enter no. of names you want to enter in file2”; cin>>n;
cout<<“nEnter the names in ascending order n”;
for(i=0;i<n;i++)
{
cin>>name;
out2<<name;
out2<<‘n’;
}
out1.close();
out2.close();
}
void main()
{
char list1[100][20], list2[100][20];
int i,j,m,n;
clrscr();
fstream out1,out2,out3;
writeLists();
out1.open(“file1.txt”,ios::in);
out2.open(“file2.txt”,ios::in);
out3.open(“file3.txt”,ios::out);
if( (!out3) || (!out1) || (!out2) )
{
printf(“Unable to open one of the file”);
getch();
exit(0);
}
clrscr();
m=0;
n=0;
while(!out1.eof())
{
out1.getline(list1[m],20,’n’);
cout<<list1[m];
m++;
}
while(!out2.eof())
{
out2.getline(list2[n],20,’n’);
cout<<list2[n];
n++;
}
m–;
n–;
i=0;
j=0;
cout<<“nElements common to both files are: n “;
while(i<m && j<n)
{
if( strcmp(list1[i],list2[j]) == 0 )
{
out3<<list1[i];
cout<<list1[i]<<“n”;
out3<<‘n’;
i++;
j++;
}
else if( strcmp(list1[i],list2[j]) < 0 )
i++;
else
j++;
}
getch();
}
Leave a ReplyCancel reply