The functions.

Hi everybody

Am a new student in c++ and am really interested to know a lot in this computer science programing language. In my class, we recently had a class that involved the use of ifstream to derive some data from a given text file and I don’t want the file in my main as it is very long and makes the program look untidy. https://www.theengineeringprojects.com/2021/10/loops-and-arrays-in-c.html Therefore, am thinking on how I can make it a function which I can be called if need be and reduce the clutter that comes with the file.

Any helpful insight is highly welcomed.

Thank you.


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
int main()
{
    //welcomeMSG();// Welcome message and what the program does 
        //declare the necessary constants
    //create the Users and Contacts array of size 1000
    const int CAPACITY = 1000;
    int size = 0;
    float  user_ids[CAPACITY];
    float contact_with[CAPACITY];
    float contact_start[CAPACITY];
    float contact_end[CAPACITY];
    float distance[CAPACITY];
    float duration[CAPACITY];
    
    const int cap = 1000;
    int size2 = 0;
    float users_ids[cap];
    string fname[cap];
    string lname[cap];
    char gender[cap];
    int age[cap];
    int phone[cap];
    string address[cap];
    
    //read the files using the functions
    
    //function to read the contacts.txt file
    ifstream infile1; //Contact.txt
    infile1.open("contacts.txt");

    if (infile1.is_open())
    {
        string temp;
        float  id, cw, cs, ce, dist; //place holders 
        
        

        getline(infile1, temp);

        while (infile1 >> id >> cw >> cs >> ce >> dist)
        {
            
            user_ids[size]= id;
            contact_with[size]=cw;
            contact_start[size]=cs;
            contact_end[size]=ce;
            distance[size]=dist;
            size++;
        
         //DEBUGGER 
        
        /*
            float dur = ce - cs;
            cout<< id << " " <<cw<< " " <<dur<< " " <<dist<<endl;
        */
        

    
        }
    }
    else
    {
        cout << "Could not open" << endl;

    }
    
    //function to read the users.txt file
    ifstream infile2; //User.txt
    infile2.open("users.txt");
    
        if (infile2.is_open())
    {
        string temp2;
        float  u_id,u_age,u_phone;  
        string Fn, Ln, add;
        char  Gend;
        

        getline(infile2, temp2);

        while (infile2 >> u_id >> Fn >> Ln >> Gend >> u_age>>u_phone>>add) // Get the data from user text 
        {
            
         users_ids[size2] = u_id;
         fname[size2] = Fn;
         lname[size2] = Ln;
         gender[size2] = Gend;
         age[size2] = u_age;
         phone[size2] = u_phone;
         address[size2] = add;
         size2++;
    
        }
    }
    else
    {
        cout << "Could not open" << endl;

    }
    
    
    cout<< "\n\nselect your choice: \n";
    cout<< "\n\n1. Exit program \n";
    cout<< "\n\n2. Print users \n";
    cout<< "\n\n3. Print all the contacts \n";
    cout<< "\n\n4. Print all the contacts who came into contact \n";
    cout<< "\n\n5. Search by user ID \n";
    cout<< "\n"<<endl;
    cout<< "\n\n   Enter your option"<<endl;
    
    int op;
    cin >> op;
    if (op == 1 )
        return 0;
    
    // print funcs
    if (op == 2 )
    {
        PrintUsers( users_ids,  fname, lname, gender, age,  phone, address,  size2);//print users
    }
    if (op == 3 )
    {
        PrintContacts(user_ids,contact_with,contact_start,contact_end,distance, size);//print contacts
    }
    if (op == 4 )
    {
        CameIntoCon(user_ids,contact_with,contact_start,contact_end,distance, size);//print Con who came into contact
    }


//user_ids,contact_with,contact_start,contact_end,distance tags



//rest of the program…


    system("PAUSE");
    return 0;
}
 
Last edited on
Normally you would put logical units into a function. A logical unit would be a section of lines that belongs together. In your case line 28 to 63 and 68 to 99. But you have that many arrays that you need to pass as a paramter that it would not make much sense.

Do you know about struct? With it you could have one array with several value members instead of many arrays for each value. Also consider std::vector.

With this you would significantly reduce the lines of code and the clutter.
As a refactored starter, consider (not tried as no file test data provided):

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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

const size_t CAPACITY {1000};

struct Contact {
	float user_id{};
	float contact_with{};
	float contact_start{};
	float contact_end{};
	float distance{};
	float duration{};
};

struct User {
	float users_id{};
	string fname;
	string lname;
	char gender{};
	int age{};
	int phone{};
	string address;
};

istream& operator>>(istream& is, Contact& con) {
	return (is >> con.user_id >> con.contact_with >> con.contact_start >> con.contact_end >> con.distance);
}

istream& operator>>(istream& is, User& user) {
	return (is >> user.users_id >> user.fname >> user.lname >> user.gender >> user.age >> user.phone >> user.address);
}

ostream& operator<<(ostream& os, const User& user) {
	return os << user.users_id << '\n' <<
		user.fname << '\n' <<
		user.lname << '\n' <<
		user.gender << '\n' <<
		user.phone << '\n' <<
		user.address << '\n';
}

ostream& operator<<(ostream& os, const Contact& con) {
	return os << con.user_id << '\n' <<
		con.contact_start << '\n' <<
		con.contact_end << '\n' <<
		con.contact_with << '\n' <<
		con.distance << '\n' <<
		con.duration << '\n';
}

void printUsers(User users[], size_t noUser) {
	for (size_t u{}; u < noUser; ++u)
		cout << users[u] << '\n';
}

void printContacts(Contact cons[], size_t noCon) {
	for (size_t c{}; c < noCon; ++c)
		cout << cons[c] << '\n';
}

int main() {
	ifstream infile1("contacts.txt");

	if (!infile1)
		return (cout << "Cannot open contacts.txt\n"), 1;

	ifstream infile2("users.txt");

	if (!infile2)
		return (cout << "Cannot open users.txt\n"), 1;

	Contact contacts[CAPACITY];
	User users[CAPACITY];
	size_t noCon{}, noUser{};
	string temp;

	getline(infile1, temp);
	getline(infile2, temp);

	while (noCon < CAPACITY && (infile1 >> contacts[noCon++]));
	while (noUser < CAPACITY && (infile2 >> users[noUser++]));

	for (int op{}; op != 1; ) {
		cout << "\n1. Exit program \n";
		cout << "2. Print users \n";
		cout << "3. Print all the contacts \n";
		cout << "4. Print all the contacts who came into contact \n";
		cout << "5. Search by user ID \n";
		cout << "\n   Enter your option: ";

		cin >> op;

		switch (op) {
		case 1:
			break;

		case 2:
			printUsers(users, noUser);
			break;

		case 3:
			printContacts(contacts, noCon);
			break;

		case 4:
			cout << "Not yet implemented\n";
			break;

		default:
			cout << "Invalid option\n";
			break;
		}
	}
}

Last edited on
Topic archived. No new replies allowed.