C++ Coding that create a function that open the contact file using struct, pointers, array, and classes

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void printContacts(string user_id[], float contact_with[], float contact_start[],float contact_end[],double distance[], int size)
{
cout << "user_id\tcontact_with\tcontact_start\tcontact_end]tdistance" << endl;
for (int i = 0; i < size; i++)
{

cout << "\n" << user_id[i] << contact_with[i] << contact_start[i] << contact_end[i] << distance[i] << endl;
}
}
struct Contacts
{
string user_id;
float contact_with;
float contact_start;
float contact_end;
double distance;
};

int main()
{
//Welcoming messages
cout << "WELCOME TO THE CONTACT TRACE PROGRAM DURING COVID-19 OUTBREAK IN FIJI" << endl;
cout << "-----------------------------------------------------------------------" << endl;

const int CAPACITY = 1000;
string user_id[CAPACITY];
float contact_with[CAPACITY];
float contact_start[CAPACITY];
float contact_end[CAPACITY];
double distance[CAPACITY];

int size = 0;

ifstream infile;
infile.open("contacts.txt");
if (infile.is_open())
{
string temp;
string id;
float cont_with;
float con_start;
float con_end;
double dist;
getline(infile, temp);
while (infile >> id >> cont_with >> con_start >> con_end >> dist)
{
user_id[size] = id;
contact_with[size] = cont_with;
contact_start[size] = con_start;
contact_end[size] = con_end;
distance[size] = dist;
size++;

}
}
else
{

cout << "Could not found file" << endl;

}


/*while (!infile.eof())
{
infile >> cons[size].user_id >> cons[size].contact_with >> cons[size].contact_start >> cons[size].contact_end >> cons[size].distance;

cout << cons[size].user_id << cons[size].contact_with << cons[size].contact_start << cons[size].contact_end << cons[size].distance;
size++;
}
infile.close();*/
printContacts(user_id, contact_with, contact_start, contact_end, distance);
return 0;

}
Last edited on
What's your question? You did not ask one.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
avoid the .eof() loop ... it does not always work as expected. It works ok until it does not, but when it does not, its difficult to diagnose and fix.
prefer a while( file read) style, eg
while( file>>data)
Last edited on
my question is to correct my code, which is a function that read the text from a file but a uses of struct, pointers, arrays and classes
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void printContacts(string user_id[], float contact_with[], float contact_start[],float contact_end[],double distance[], int size)
{
cout << "user_id\tcontact_with\tcontact_start\tcontact_end]tdistance" << endl;
for (int i = 0; i < size; i++)
{

cout << "\n" << user_id[i] << contact_with[i] << contact_start[i] << contact_end[i] << distance[i] << endl;
}
}
struct Contacts
{
string user_id;
float contact_with;
float contact_start;
float contact_end;
double distance;
};

int main()
{
//Welcoming messages
cout << "WELCOME TO THE CONTACT TRACE PROGRAM DURING COVID-19 OUTBREAK IN FIJI" << endl;
cout << "-----------------------------------------------------------------------" << endl;

const int CAPACITY = 1000;
string user_id[CAPACITY];
float contact_with[CAPACITY];
float contact_start[CAPACITY];
float contact_end[CAPACITY];
double distance[CAPACITY];

int size = 0;

ifstream infile;
infile.open("contacts.txt");
if (infile.is_open())
{
string temp;
string id;
float cont_with;
float con_start;
float con_end;
double dist;
getline(infile, temp);
while (infile >> id >> cont_with >> con_start >> con_end >> dist)
{
user_id[size] = id;
contact_with[size] = cont_with;
contact_start[size] = con_start;
contact_end[size] = con_end;
distance[size] = dist;
size++;

}
}
else
{

cout << "Could not found file" << endl;

}


/*while (!infile.eof())
{
infile >> cons[size].user_id >> cons[size].contact_with >> cons[size].contact_start >> cons[size].contact_end >> cons[size].distance;

cout << cons[size].user_id << cons[size].contact_with << cons[size].contact_start << cons[size].contact_end << cons[size].distance;
size++;
}
infile.close();*/
printContacts(user_id, contact_with, contact_start, contact_end, distance);
return 0;

}


make the function that the texts from file correct on using struct, pointers, arrays,and classes
Last edited on
This should get you started.
Not tested because I don't have the input file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct Contacts
{
	string user_id;
	float contact_with;
	float contact_start;
	float contact_end;
	double distance;
};

void printContacts(Contacts contacts[], int size)
{
	cout << "user_id\tcontact_with\tcontact_start\tcontact_end]tdistance" << endl;
	for (int i = 0; i < size; i++)
	{
		cout << contacts[i].user_id << "\t" << contacts[i].contact_with << "\t" << contacts[i].contact_start
			<< "\t" << contacts[i].contact_end << "\t" << contacts[i].distance << endl;
	}
}

int main()
{
	//Welcoming messages
	cout << "WELCOME TO THE CONTACT TRACE PROGRAM DURING COVID-19 OUTBREAK IN FIJI" << endl;
	cout << "-----------------------------------------------------------------------" << endl;

	const int CAPACITY = 1000;
	Contacts contacts[CAPACITY];
	int size = 0;

	ifstream infile;
	infile.open("contacts.txt");
	if (!infile.is_open())
	{
		cout << "Could not find file" << endl;
		return 1;	// exit program with error 
	}
	string id;
	float cont_with;
	float con_start;
	float con_end;
	double dist;
		
	while (infile >> id >> cont_with >> con_start >> con_end >> dist)
	{
		contacts[size].user_id = id;
		contacts[size].contact_with = cont_with;
		contacts[size].contact_start = con_start;
		contacts[size].contact_end = con_end;
		contacts[size].distance = dist;
		size++;
	}

	printContacts(contacts, size); 
	return 0;
}
Very similar to this post here http://www.cplusplus.com/forum/beginner/280188/

Topic archived. No new replies allowed.