class

how to make class and object methods but still output the same.



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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//include the neccessary headers
#include <iostream>
#include <fstream>
#include<string>

using namespace std;
//declare the necessary constants
const int arrsize = 1000;
const int arrsize2 = 1000;
bool going = true;
//Users arrays
string user_id[arrsize], fname[arrsize], lname[arrsize], address[arrsize];
char Gender[arrsize];
int Age[arrsize], Phone[arrsize];
int x;

string user_id2[arrsize2], fname2[arrsize2], lname2[arrsize2], address2[arrsize2];
char Gender2[arrsize2];
int Age2[arrsize2], Phone2[arrsize2];
int x2;

//Contacts arrays
string user_ids[arrsize], contact_with[arrsize];
int contact_start[arrsize], contact_duration[arrsize], contact_end[arrsize];
int distances[arrsize];
int y;

string user_ids2[arrsize], contact_with2[arrsize];
int contact_start2[arrsize], contact_duration2[arrsize], contact_end2[arrsize];
int distances2[arrsize];
int y2;

void mySearchUser();
void myUserDistance();
void myReadFromUser();
void myReadFromUser2();
void myReadFromContact();
void myReadFromContact2();
void requirements(string respond);
//main function
int main()
{
	//Display message and instructions about the program
	cout << "\n\n\t\tWelcome to Covid-19 Record Manager" << endl;
	cout << "\t\t**********************************" << endl;
	cout << "1. Exit the Program" << endl;
	cout << "2. Print the users" << endl;
	cout << "3. Print all the contacts" << endl;
	cout << "4. Print the contacts who came into contact" << endl;
	cout << "5. Searching by UerID" << endl;

	string repond;
	cout << "Enter your option (1-5):";
	cin >> repond;
	//read the files using the functions
	requirements(repond);
	//rest of the program

	return 0;
}
//declare the Contact struct
void mySearchUser() {
	myReadFromUser2();
	string search;

	cout << "Please enter a User ID to search: ";
	cin >> search;


	for (int i = 0; i < x2; i++) {
		if (user_id2[i] == search) {
			cout << "-------------------------" << endl;
			cout << "UserID:" << user_id2[i] << endl;
			cout << "Name:" << fname2[i] << " " << lname2[i] << endl;
			cout << "Gender:" << Gender2[i] << endl;
			cout << "Age:" << Age2[i] << endl;
			cout << "Phone:" << Phone2[i] << endl;
			cout << "Address:" << address2[i] << endl;
			cout << "-------------------------" << endl;
		}

	}
	main();
}
//declare the User struct
void myUserDistance() {
	myReadFromContact2();
	int xy = 0;

	cout << "-------------------------------------------------------------" << endl;
	cout << "\nUserID\t Con/With\t Duration(s)\t Distance(cm) \n";
	cout << "-------------------------------------------------------------" << endl;
	for (int i = 0; i < y2 - 1; i++)
	{
		if (distances2[i] < 100 || contact_duration2[i] >= 15) {
			cout << user_ids2[i] << "\t\t" << contact_with2[i] << "\t\t" << contact_duration2[i] << "\t\t" << distances2[i] << endl;
			xy++;
		}

	}
	cout << "Total Contacts: " << xy << "/" << y2 - 1 << endl;
	cout << "-------------------------------------------------------------" << endl;

	main();
}
//function to read the users.txt file
void myReadFromUser() {

	string line;

	fstream myRead;

	myRead.open("users.txt", ios::in);//read

	if (myRead.is_open()) {
		while (getline(myRead, line))
		{


			myRead >> user_id[x] >> fname[x] >> lname[x] >> Gender[x] >> Age[x] >> Phone[x] >> address[x];

			x++;
		}

		cout << "-------------------------------------------------------------" << endl;
		cout << "\nUserID\t Fname\t Lname\t Gender\t Age \t Phone\t Address \n";
		cout << "-------------------------------------------------------------" << endl;
		for (int i = 0; i < x - 1; i++) {
			cout << user_id[i] << "\t" << fname[i] << "\t" << lname[i] << "\t" << Gender[i] << "\t" << Age[i] << "\t" << Phone[i] << "\t" << address[i] << endl;
		}
		cout << "Total Contacts: " << x - 1 << endl;
		cout << "-------------------------------------------------------------" << endl;
	}
	else
	{
		cerr << "Error Opening";
	}
	myRead.close();
	main();
}
void myReadFromUser2() {

	string line;

	fstream myRead;

	myRead.open("users.txt", ios::in);//read

	if (myRead.is_open()) {
		while (getline(myRead, line))
		{


			myRead >> user_id2[x2] >> fname2[x2] >> lname2[x2] >> Gender2[x2] >> Age2[x2] >> Phone2[x2] >> address2[x2];

			x2++;
		}

		for (int i = 0; i < x2 - 1; i++) {

		}

	}

	myRead.close();
}

void myReadFromContact()
{
	string line;
	y = 0;
	fstream myRead;
	myRead.open("contacts.txt", ios::in);//open in read-mode
	if (myRead.is_open()) //open file
	{
		while (getline(myRead, line))
		{
			//Read from contact.txt into each field array
			myRead >> user_ids[y] >> contact_with[y] >> contact_start[y] >> contact_end[y] >> distances[y];
			contact_duration[y] = contact_end[y] - contact_start[y];
			y++;
		}
		cout << "-------------------------------------------------------------" << endl;
		cout << "\nUserID\t Con/With\t Duration(s)\t Distance(cm) \n";
		cout << "-------------------------------------------------------------" << endl;
		for (int i = 0; i < y - 1; i++) {
			cout << user_ids[i] << "\t\t" << contact_with[i] << "\t\t" << contact_duration[i] << "\t\t" << distances[i] << endl;
		}
		cout << "Total Contacts: " << y - 1 << endl;
		cout << "-------------------------------------------------------------" << endl;
	}
	else
	{
		cerr << "Error Opening";
	}
	myRead.close();
	main();
}
void myReadFromContact2() {

	string line;
	y2 = 0;
	fstream myRead;
	myRead.open("contacts.txt", ios::in);//open in read-mode
	if (myRead.is_open()) //open file
	{
		while (getline(myRead, line))
		{
			//Read from contact.txt into each field array
			myRead >> user_ids2[y2] >> contact_with2[y2] >> contact_start2[y2] >> contact_end2[y2] >> distances2[y2];
			contact_duration2[y2] = contact_end2[y2] - contact_start2[y2];
			y2++;
		}
		for (int i = 0; i < y2 - 1; i++) {
		}
	}
	else
	{
		cerr << "Error Opening";
	}
	myRead.close();
}
//function to implement all the requirements (1-5)
void requirements(string respond)
{
	if (respond == "1")
	{
		exit(1);
	}
	else if (respond == "2")
	{
		myReadFromUser();
	}
	else if (respond == "3")
	{
		myReadFromContact();
	}
	else if (respond == "4")
	{
		myUserDistance();
	}
	else if (respond == "5")
	{
		mySearchUser();
	}
	else
	{
		cout << "Invalid Input" << endl;
	}
	main();
Last edited on
how to change the struct to class


I can't see a struct anywhere. (Or any compileable code.)

I presume that you really want to rewrite parallel arrays as an array of structs or other objects.

Please don't call main() from within your functions.
struct and class word change, then put a public: on the stuff you want public access on in the class. the difference is that struct are public by default, class is private by default, and you can override this for either one as you see fit.

because they are virtually identical, if you have something working, there is zero reason to change it.

I only skimmed your code but I don't see any objects at all. ???

a struct:

1
2
3
4
5
6
struct  structname
{
   int i{};  
   double d{};
   void foo(int f){cout << f*i << endl;}
};
Last edited on
oh no i mean to create a class. i'll edit it
ah.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Users arrays
class user
{
string user_id, fname, lname, address; //I very highly recommend address be a new class.  It is a complex and aggravating thing. 
char Gender; //I am not sure a char is sufficient to handle the 10000000000 genders out there today.  this may be useful as a enum type. 
int Age, Phone[5]; //example, maybe a person has 5 phone numbers.

void ReadFromContact(); //header.  this class part would go in the .h file if you split it up

};

void user::ReadFromContact() //body, this would go in .cpp file if you split it up
{
  code;
}
...

user userarray1[arrsize]; //this replaces the arrays you had earlier.  


I can't make much sense of what you had before, you have user1 who has 1000 id numbers, followed by user2 who has 1000 id numbers and addresses... ???

it seems like you want to have one user item per user (the class above does this) for N users (the array after the class).

if you need a list of the people a person was in contact with, you can add an array into the user class for that. consider
user* contact_with[arrsize]; //put a pointer to the person's user record, and you have all that info without copying it a billionty times. So you read all the users in first, then do their contacts after. That means you will need a way to search your user's array to find each contact as it comes in, but you will save a lot of space this way ... and you can trace chains ... joe.contact[someone].contact[someoneelse].contact[deeperitgoes]....
Last edited on
Your main should look like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
    do {
	//Display message and instructions about the program
	cout << "\n\n\t\tWelcome to Covid-19 Record Manager" << endl;
	cout << "\t\t**********************************" << endl;
	cout << "1. Exit the Program" << endl;
	cout << "2. Print the users" << endl;
	cout << "3. Print all the contacts" << endl;
	cout << "4. Print the contacts who came into contact" << endl;
	cout << "5. Searching by UerID" << endl;

	string repond;
	cout << "Enter your option (1-5):";
	cin >> repond;
	//read the files using the functions
	requirements(repond);
	//rest of the program
    } while ( true );
    return 0;
}

Then you can remove all those recursive calls to main, and your program just loops properly through the normal call / return mechanism.

main() is allowed to do things like say call default constructors for global objects, which would be a very bad idea.
main cannot be recursive in standard c++, while on that topic. It is allowed by most compilers, but in strict mode or for a less forgiving compiler, it will not work. Language extensions can be fun and handy, but you are better off avoiding them unless absolutely necessary or until you know C++ very well and understand that you did something beyond the language.
Last edited on
oh no i mean to create a class

The brief basics of creating a struct:
https://www.learncpp.com/cpp-tutorial/structs/

The basics of creating a class:
https://www.learncpp.com/cpp-tutorial/welcome-to-object-oriented-programming/
Topic archived. No new replies allowed.