Click here to Skip to main content
15,917,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<iostream>
#include<fstream> 
#include <cstdlib> 

using namespace std;  

struct user 
{
	
	string f_name, l_name, address, gender, id, age, phone;
		
};
struct unode
{
	user data;
	unode *next;
	unode *prev;
};
struct ulist
{
	int size;
	unode *head;
	unode *tail;
}; 

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);
			getline(infile, node->data.id);
			getline(infile, node->data.age);
			getline(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;
		} 
	cout << "\tTotal users: "<< list.size <<endl;
	cout << "\t------------------------------------------------------------" << endl;

}
int main() 
{ 
	int opt;
	//Deaclare and initialize the list
	ulist list;
	list.head = NULL;
	list.tail = NULL;
	list.size = 0;
	
	userfile (list);
		//print startup message
		cout<<"\n\n\t\t*************************************************"<<endl; 
		cout<<"\n\t\tMinistry of health contact tracing analyser:"<<endl;
		cout<<"\n\t\t*************************************************"<<endl;
	
	system ("pause");
	system ("CLS"); 
	
	do{
		//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<< "\n\tEnter your option : ";
		
		//validating user input using a while loop
		while (!(cin>>opt) || opt > 2 || 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;
			}
			
			system ("pause");
			system ("CLS");
	}while( opt != 1 );			 				

	
return 0; 
}  


What I have tried:

i tried printing but there is no output. how can i fix this?
Posted
Updated 24-Nov-21 4:46am
Comments
Richard MacCutchan 24-Nov-21 11:06am    
It almost works for me, although see my solution below.

1 solution

C++
		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;
// add the following
            node = node->next; // select the next node*
		} 

Your loop only accesses the single node at list.head. So first you should use the debugger to check the contents of that node.

The fix for the loop is as annotated above>
 
Share this answer
 
v3
Comments
John Bob 2021 24-Nov-21 11:17am    
okay got it.. thank you.. :)
Richard MacCutchan 24-Nov-21 11:19am    
You're welcome.
John Bob 2021 24-Nov-21 11:46am    
one more thing, when i change the data type for id, age & phone to int, it gives me this message:[Error] no matching function for call to 'getline(std::ifstream&, int&)'
Richard MacCutchan 24-Nov-21 11:59am    
The getline method only reads into a string. For other data types you need to use the operator>> overloads. But telephone numbers are not really int types as they may contain spaces, or other characters.

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