#include<iostream>
#include<fstream>
using namespace std;
struct user
{
string f_name, l_name, address;
string gender, phone;
int id, age;
};
struct contact
{
int id;
int contact_with;
int contact_start;
int contact_end;
int distance ;
};
struct unode
{
user data;
unode *next;
unode *prev;
};
struct ulist
{
int size;
unode *head;
unode *tail;
};
struct cnode
{
contact info;
cnode *next;
cnode *prev;
};
struct clist
{
int size;
cnode *head;
cnode *tail;
};
void contactfile(clist &chain)
{
ifstream inptfile;
inptfile.open("contacts.txt");
while(inptfile.good())
{
cnode *node = new cnode;
if (chain.head == NULL)
{
node->prev = NULL;
node->next = NULL;
chain.head = node;
chain.tail = node;
}
else
{
chain.tail->next = node;
node->prev = chain.head;
node->next = NULL;
chain.tail = node;
}
inptfile>> node->info.id
>> node->info.contact_with
>> node->info.contact_start
>> node->info.contact_end
>> node->info.distance;
chain.size++;
string blankline;
if(!getline(inptfile, blankline))
break;
}
inptfile.close();
}
void userfile(ulist &list)
{
ifstream infile;
infile.open("users.txt");
while(infile.good())
{
unode *node = new unode;
if (list.head == NULL)
{
node->prev = NULL;
node->next = NULL;
list.head = node;
list.tail = node;
}
else
{
list.tail->next = node;
node->prev = list.head;
node->next = NULL;
list.tail = node;
}
getline(infile, node->data.f_name);
getline(infile, node->data.l_name);
getline(infile, node->data.address);
getline(infile, node->data.gender);
infile>> node->data.id;
infile>> node->data.age;
infile>> node->data.phone;
list.size++;
string blankline;
if(!getline(infile, blankline))
break;
}
infile.close();
}
void printusers(ulist &list)
{
cout << "\t------------------------------------------------------------" << endl;
cout << "\tUserID\tFname\tLname\tGender\tAge\tPhone\tAddress " << endl;
cout << "\t------------------------------------------------------------" << endl;
unode *node = list.head;
for(int i = 0; i < list.size ;i++)
{
cout << node->data.f_name << endl;
cout << node->data.l_name << endl;
cout << node->data.address << endl;
cout << node->data.gender << endl;
cout << node->data.id << endl;
cout << node->data.age << endl;
cout << node->data.phone << endl;
node = node->next;
}
cout << "\tTotal users: "<< list.size <<endl;
cout << "\t------------------------------------------------------------" << endl;
}
void printcontacts(clist &chain)
{
int duration;
cout << "\t-------------------------------------------" << endl;
cout << "\tUserID\tCon/With Duration(s) Distance(cm) " << endl;
cout << "\t-------------------------------------------" << endl;
cnode *node = chain.head;
for(int i = 0; i < chain.size ;i++)
{
cout << node->info.id;
cout<< node->info.contact_with;
cout<< node->info.contact_start;
cout<< node->info.contact_end;
cout<< node->info.distance;
node = node->next;
}
cout << "\tTotal contacts: " << chain.size <<endl;
cout << "\t-------------------------------------------" << endl;
}
void printcameintocontact(clist &chain)
{
int current_size;
cout << "\t-------------------------------------------" << endl;
cout << "\tUserID Con/With Duration(s) Distance(cm) " << endl;
cout << "\t-------------------------------------------" << endl;
cnode *node = chain.head;
for(int i = 0; i < chain.size ;i++)
{
if ((node->info.contact_end - node->info.contact_start) >= 15 || node->info.distance < 100 )
{
cout <<"\t"<< node->info.id << "\t " << node->info.contact_with << "\t " <<
node->info.contact_end - node->info.contact_start << "\t\t" <<
node->info.distance << endl;
current_size++;
}
}
cout << "\tTotal contacts: " << current_size <<"/"<< chain.size <<endl;
cout << "\t-------------------------------------------" << endl;
}
void searchbyid(ulist &list)
{
int i, search;
cout<<"\tenter the user ID to search: ";
cin>> search;
if (cin.fail())
{
cout<<"\t---------------------"<<endl;
cout<<"\t 0 User ID's found: "<<endl;
cout<<"\t---------------------\n"<<endl;
cin.clear();
cin.ignore(20, '\n');
}
unode *node = list.head;
for (i=0; i < list.size; i++)
{
if (search == node->data.id)
{
cout<<"\n\t-----------------------------"<<endl;
cout<<"\tUserID:\t "<< node->data.id <<endl;
cout<<"\tName:\t "<< node->data.f_name <<" "<< node->data.l_name << endl;
cout<<"\tGender:\t "<< node->data.gender << endl;
cout<<"\tAge:\t "<<node->data.age<<endl;
cout<<"\tPhone:\t "<<node->data.phone <<endl;
cout<<"\tAddress: "<<node->data.address<<endl;
cout<<"\t-----------------------------\n"<<endl;
break;
}
}
}
int main()
{
int opt;
ulist list;
list.head = NULL;
list.tail = NULL;
list.size = 0;
clist chain;
chain.head = NULL;
chain.tail = NULL;
chain.size = 0;
userfile (list);
cout<<"\n\n\t\t*************************************************"<<endl;
cout<<"\n\t\tFIJI Ministry of health contact tracing analyser:"<<endl;
cout<<"\n\t\t*************************************************"<<endl;
cout<<"\n\t\tThis program analyses the data gathered by the "<<endl;
cout<<"\n\t\tFIJI ministry of health to help trace the possible "<<endl;
cout<<"\n\t\tcontacts of the recent Covid-19 outbreak.\n"<<endl;
system ("pause");
system ("CLS");
do{
int opt;
cout<< "\n\tWhat would you like to do?"<< endl;
cout<< "\n\t1. Exit the program "<< endl;
cout<< "\t2. Print the users "<< endl;
cout<< "\t3. Print all the contacts "<< endl;
cout<< "\t4. Print the contacts who 'came into contact' "<< endl;
cout<< "\t5. Search by user ID"<< endl;
cout<< "\n\tEnter your option (1-5): ";
while (!(cin>>opt) || opt > 5 || opt < 1)
{
cout<<"\n\tINVALID OPTION\n"<<endl;
cin.clear();
cin.ignore(20, '\n');
break;
}
switch (opt){
case 1:
exit(1);
break;
case 2:
printusers(list);
break;
case 3:
printcontacts(chain);
break;
case 4:
printcameintocontact(chain);
break;
case 5:
searchbyid(list);
break;
}
system ("pause");
system ("CLS");
}while(opt != 1 );
return 0;
}
What I have tried:
the two files are as follow without the ***headings***
*****contacts.txt file************
user_id contact_with contact_start contact_end distance(cm)
1007 1010 1630731125 1630731129 202
1003 1010 1630718920 1630719418 309
1020 1012 1630713119 1630713527 119
1010 1018 1630705321 1630705464 217
1007 1013 1630730361 1630730363 335
1006 1015 1630707356 1630707360 258
1014 1001 1630717612 1630717615 306
1020 1011 1630700732 1630701123 42
1005 1011 1630717455 1630717459 68
1015 1006 1630706680 1630706686 217
*******users.txt file************
user_id fname lname gender age phone address
1001 Ray Dixon M 46 9364652 Lokia
1002 Bryan Green M 18 9579302 Drekena
1003 Justin Dixon M 33 9353533 Lokia
1004 Lester Byrd M 45 9534695 Nasilai
1005 Santos Larson M 53 9093177 Vunuku
1006 Bryan Cobb M 42 9905139 Narocivo
1007 Eddie Watson M 20 9610408 Nabua
1008 Wesley Barton M 27 9801864 Nasigatoka
1009 Victor Mason M 50 9855386 Nukutubu
1010 Ellis Cobb M 24 9389406 Narocivo
1011 Diana Ross F 27 9940148 Vunuku
1012 Amanda Carter F 43 9506743 Nasilai
1013 Maria Edwards F 53 9798534 Narocivo
1014 Maria Jenkins F 34 9352516 Lomanikoro
1015 Louise Davis F 55 9812126 Nasilai
1016 Sandra Sanders F 29 9369570 Tavuya
1017 Bonnie Roberts F 40 9689234 Nukui
1018 Melissa Harris F 29 9321235 Drekena
1019 Marilyn Parker F 56 9409221 Nukui
1020 Bonnie Lopez F 43 9342939 Nasigatoka