How to use members of class

So I have a program written using structs now I want to which it over to a class
contacts[i].duration = contacts[i].end - contacts[i].start
1
2
3
4
5
6
7
8
9
10
11
12
  	if(infile.is_open())
	{
		int i = 0;
		string temp;
		getline(infile, temp);
		while(infile >> contacts[i].id >> contacts[i].contact >> contacts[i].start >> contacts[i].end >> contacts[i].distance)
		{
			contacts[i].duration = contacts[i].end - contacts[i].start;
			i++;
		}
		num_contacts = i;
	}

contacts[i].duration = contacts[i].end - contacts[i].start
bcuz duration is based of 2 members of my class how do i do it the way I did in structs? btw this wat i have so far though its just for reading in the data

1
2
3
4
5
6
7
8
9
	while (!infile.eof())
		{
			infile >> id >> cw >> cs >> ce >> dist;
			conlist[Num_Contacts].setid(id);
			conlist[Num_Contacts].setcw(cw);
			conlist[Num_Contacts].setcs(cs);
			conlist[Num_Contacts].setce(ce);
			conlist[Num_Contacts].setdist(dist);
			Num_Contacts++;
Hi,

I can't really tell much from the code you provided, but it looks like you are writing a feature for some type of contact book program. If you want to write a class, you will have to build something such as a Contact class with the appropriate attributes. In that class you can add methods for your 'id', 'cw', 'cs', and 'ce' attributes that I see in your file reading loop. You can add a struct in your main file file where you can have a struct of Contact types and append new ones as necessary.

Also make sure to close files after you open them.

-C
Last edited on
Second snippet, line 1: Do not loop on (! stream.eof()) or (stream.good()). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> (or getline) operation as the condition in the while statement.
1
2
3
  while (stream >> var) // or while (getline(stream,var))
  {  //  Good operation
  }
The irony is that the "struct version" did it right:
while(infile >> contacts[i].id >> contacts[i].contact >> contacts[i].start >> contacts[i].end >> contacts[i].distance)
but for some reason the "class version" was different:
1
2
3
while ( !infile.eof() )
{
    infile >> id >> cw >> cs >> ce >> dist;



Back to the main question: contacts[i].duration = contacts[i].end - contacts[i].start;
The duration changes whenever end changes or start changes.
Therefore, both setcs() and setce() should update the duration.

However, does the contact need to store the duration? Would it be enough to have
getduration() const { return end - start; }
Then contacts no longer need memory for duration and other members do not need to maintain its value.
You need to go one step further :
1
2
3
while (infile >> contacts[Num_Contacts]) {
    ++Num_Contacts;
}


As Keskiverto suggests, don't store duration in the class. Instead add this method:
double getDuration() const { return end - start; }\

Of start, end, and duration, you only need to store 2 of the values. Your goal is to hide from the user which 2 you're storing and which one is computed.
ok kesikiverto for while(infile >> contacts[i].id >> contacts[i].contact >> contacts[i].start >> contacts[i].end >> contacts[i].distance) a mate of mine told me that structs and classes amend to the same principle so I tried to do it the same way I did the struct but that resulted in errors so then I changed it.

I dont know how to implement that function double getDuration() const { return end - start; }.Do I just it have outside the class ?? as a function on its own?

currently my class looks like this
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
class Contacts
{
public:
	//construct and decon 
	//functions 
	Contacts();//Constructor
	~Contacts(); //Destructor
	//setter
	void setid(float id);
	void setcw(float cw);//contact_with
	void setcs(float cs);//call start
	void setce(float ce);//call end
	void setdist(float dist);//distance
	void setdur(float dur);//duration
	//getter
	float getid();
	float getcw();
	float getcs();
	float getce();
	float getdist();
	float getdur();

private:
	//varaibles
	
	float id, cw, cs, ce, dur, dist;
	//dur = ce - cs;

};


I have no problem reading in the txt and displaying the contents which is id,contact_with, call_start, call_end and distance of the file but i am required to only display the id,contact_with distance and replace call_start, call_end with call_duration

and for those curious this is what the text file looks like
user_id contact_with contact_start contact_end distance(cm)
1011 1010 1630731125 1630731129 202
1009 1010 1630718920 1630719418 309

I have to find the duration for each record.
Last edited on
> I dont know how to implement that function double getDuration() const { return end - start; }
it's a member function, just like setdist() or getdur()
i get that its a member function but I dont have any experience on anything like
getDuration() const { return end - start; } particularyly the part const { return end - start; } is something I have never seen b4.
Last edited on
You have this:
1
2
3
4
5
6
7
8
9
10
11
class Contacts
{
public:
	// other member functions

	void setcs(float cs);
	void setce(float ce);
	float getdur();
private:
	float id, cw, cs, ce, dist;
};

And probably these:
1
2
3
4
5
6
7
void Contacts::setcs(float cs) {
  this->cs = cs;
}

void Contacts::setce(float ce) {
  this->ce = ce;
}

And you need:
1
2
3
float Contacts::getdur() {
  // ????
}

I do suggest that it could be:
1
2
3
float Contacts::getdur() {
  return ce - cs;
}


The "const" is for getting around code like this:
1
2
const Contacts xyz = conlist[3]; // create copy of element that is constant
std::cout << xyz.getdur(); // ERROR cannot call non-const member on const object 

A member function can be marked const and then it can be called on const object:
1
2
3
4
5
6
7
8
9
10
11
class Contacts
{
public:
	float getdur() const;

	// other members
};

float Contacts::getdur() const {
  return ce - cs;
}

Additionally, compiler will show error if you try to modify the object within the body of const member function.
I have done
1
2
3
4
5
6
7
8
9
public:
	float getdur() const;

	// other members
};

float Contacts::getdur() const {
  return ce - cs;
}
before without the const before and got the wrong output it is still wrong even with const added. thank you for the help :)
got the wrong output

It shouldn't make a difference but you could use parentheses in your return statement:
return (ce - cs);
More likely your data member variables are not what you expect them to be.

What would you expect the return value to be, and what are you actually getting returned?
taking the sample text file from a few threads up we have 1630731125 and 1630731129
so I wanted it to be like this but doing it to all the records
1
2
3
4
5
6
 getdur(ce,cs) 
{
    dur=ce-cs // 1630731129 - 1630731125
     return dur; // should be 4 but when I run it I get 0
                     // the next record is 384 which is still wrong 
}


I am still bamboozled as to why I am getting these strange figures
You are getting the wrong output because you are using float.

You are reading
cs = 1630731125
ce = 1630731129

Both of those are floats, and hence stored to an accuracy of about 6 sig. figs. The difference in those numbers is in the 10th significant figure.

Since they appear to be discrete and have no decimal point you could actually store them in variables of type unsigned long long. Otherwise use double.


I think that storing an "id" in a float is not a good idea either. Use an unsigned long long, or even a string.
Last edited on
holy ***** this dude/duddete just saved me I had no idea that float could do that I always assumed it to be a safe bet between double and it

Now I can finally close this one :)
Topic archived. No new replies allowed.