Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<iostream>
#include<fstream>  

using namespace std;  


//User struct
struct user 
{
	string f_name, l_name, address;
	string gender, phone;
	int id, age;
};

//Contact struct
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;
}; 
//function to read the contacts.txt file
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();
} 
//function to read the users.txt file
void userfile(ulist &list)
{ 

	//user_size = 0;
	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();
}

//function to print all users
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; // select the next node*
		} 
	cout << "\tTotal users: "<< list.size <<endl;
	cout << "\t------------------------------------------------------------" << endl;

}

//function to print all contacts
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; // select the next node*
		}
	cout << "\tTotal contacts: " << chain.size <<endl; 
	cout << "\t-------------------------------------------" << endl;

}

//function to filter contacts.txt and print contacts who came into contact
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;
	
}

//search user ID function
void searchbyid(ulist &list)
{
	int i, search;
	cout<<"\tenter the user ID to search: ";
	cin>> search;
	
		if (cin.fail()) //input validation
		{
			cout<<"\t---------------------"<<endl;
			cout<<"\t 0 User ID's found: "<<endl;
			cout<<"\t---------------------\n"<<endl;
			cin.clear(); //clears prvious input
			cin.ignore(20, '\n'); //discarding previous input
		}
	
	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;
	//Deaclare and initialize the list
	ulist list;
	list.head = NULL;
	list.tail = NULL;
	list.size = 0;
	
	clist chain;
	chain.head = NULL;
	chain.tail = NULL;
	chain.size = 0;
	
	userfile (list);
		//print startup message
		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;
		//print menu options
		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): ";
		//validating user input using a while loop
		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
Posted
Updated 24-Nov-21 23:05pm
Comments
phil.o 25-Nov-21 1:45am    
Please define what is exptected and how actual result differs from expectations.
John Bob 2021 25-Nov-21 2:15am    
the program is supposed to read the users and contacts txt files into two seperate linked list and print the informations extracted from both files using the print functions
John Bob 2021 25-Nov-21 2:18am    
the actual results from printusers function gave me only half of the data and some have gone missing whereas the print contacts functions outputed nothing.
Rick York 27-Nov-21 14:01pm    
One recommendation for you: look at a header file called WinNT.h in the windows SDK. There is a structure there called _LIST_ENTRY and it is nothing more than pointers to forward and backward links. There is then a series of macros used to manipulate the list and its contents. This is done in an entirely generic way and I suggest you take some suggestions from it. Primarily that you shouldn't build lists that are specific to their content. You should build a generic list class with methods to deal with the content. This way you have one set of code to manage a linked list and it can contain any kind of data you want to. This is easier with templates.

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
I can see one of your problems. The function to read the contacts file is never called.
 
Share this answer
 
This is basically the same issue as your question from yesterday at How do I print my data read into the linked list?[^]. And the suggestions I gave you there should work just as well for this issue. You just have two lists, rather than one.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900