structs and functions

Hi I am trying to use 2 members of my struct to form the other member but I dont know how to do so. Any help is more then welcomed thanks

what the txt looks like
user_id contact_with contact_start contact_end distance(cm)
1007 1010 1630731125 1630731129 202

what errors I have at the moment are all in the "ContactDuration" function
line 27 [Error] expected ',' or ';' before '{' token
line 26 cs and ce not delcared

line 26 [Error] expression list treated as compound expression in initializer [-fpermissive]

contact.duration = contact.contact_end - contact.contact_start



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


//declare the Contact struct
struct Contact
{
	float user_id;
	float contact_with;
	float contact_start;
	float contact_end;
	float distance;
	float duration;

};



//functions to implement all the requirements (1-5)
float ContactDuration(cs,ce) // Calculate duration 
{

	for (int  i = 0; i < size; i++)
	{
		contacts[i].duration = contacts[i].contact_end - contacts[i].contact_start;
	}
	
	
	return contacts[i]->duration;

}
void PrintCameIntoCon(Contact contacts[], int size) // Print list of people who came into contact
{
	cout << "--------------------------------------------------------------------------------------------" << endl;
	cout << "UserID	Con/with	Duration	distance		" << endl;

	for (int i = 0; i < size; i++)
	{
		//float dur = ContactDuration(contacts[i].duration);
		//float dist = distance[i];

		while (contacts[i].duration>= 15 || contacts[i].distance <= 100)
		{
			cout << contacts[i].user_id << "	 " << contacts[i].contact_with << "		 " << contacts[i].duration << "		  " << contacts[i].distance << endl;
			break;

		}
	}
	cout << "--------------------------------------------------------------------------------------------" << endl;

}





int main()
{
	

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

	

	//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, dur; //place holders 

		getline(infile1, temp);

		while (infile1 >> id >> cw >> cs >> ce >> dist)
		{

			contacts[size].user_id = id;
			contacts[size].contact_with = cw;
			contacts[size].contact_start = cs;
			contacts[size].contact_end = ce;
			contacts[size].distance = dist;
			contacts[size].duration = ContactDuration(cs,ce,size);
			size++;
			
		}
	}
	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 << " Enter your option" << endl;

	int op;
	cin >> op;

	switch (op) {

	case 1:
		break;

	case 2: {
		PrintUsers(users_ids, fname, lname, gender, age, phone, address, size2);//print users
		break;
	}



	case 3: {
		PrintContacts(contacts, size);//print contacts
		break;
	}



	case 4: {
		PrintCameIntoCon(contacts, size);//Print contacts with the conditions
		break;
	}



	case 5:
		//SearchByuser();
		break;

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

	//rest of the program…


	system("PAUSE");
	return 0;
}
Last edited on
Shouldn't the duration function be:

1
2
3
float contactDuration(float s, float e) {
    return e - s;
}



then L 91 becomes:

 
contacts[size].duration = ContactDuration(cs, ce);

To me it looks like you already had a thread and discussion (which did give you code) here: http://www.cplusplus.com/forum/beginner/280188/
"Double-posting" (on separate subforum, nonetheless) is usually not efficient.


Lets look at your function anyway:
1
2
3
4
5
6
7
8
float ContactDuration( cs, ce )
{
  for (int  i = 0; i < size; i++)
  {
    contacts[i].duration = contacts[i].contact_end - contacts[i].contact_start;
  }
  return contacts[i]->duration;
}

A function gets data via parameters. Each parameter is essentially a declaration of variable.
Does cs look like a variable? Is that a type? If yes, what is the name?
Does ce look like a variable? Is that a type? If yes, what is the name?

Where in this function the cs and ce are used?

What is size? You did not declare it inside the function, nor as parameter of function. Is it a global variable?

What is contacts? You did not declare it inside the function, nor as parameter of function. Is it a global variable?

You do declare variable in the for statement: int i Its lifetime is the loop. It does not exists when you do return.
If it would exists, its value would be equal to size. The loop never assigns anything to contacts[size].

Why do you return one unknown value anyway, when you have computed size values in the function?
Topic archived. No new replies allowed.