constant timestamp

closed account (17Mo1hU5)
I have a class named message and I want to be able to tag it with a timestamp, so then I can write the tag on a .txt file. Yet, I have no clue how to make the timestamp constant, so it doesen't change every single time I try to access it from another class. Basicly the whole idea is to have users like messages, open up a txt file (ex: user_name.txt) and in there have all the timestamps of the messages that the user has liked. Then I ll be able to tackle the problem, that a user presses like twice at the same message. Thank you in advance <3 !

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
  class Message{
	private:
		string mnm;
		int likes=0;
		time_t now = time(0);
		string timestamp = ctime(&now);
		const string mtd = timestamp;

		
		string to_string(int k){
			stringstream ss; 
			string s;
			ss<<k;    
			ss>>s;  
			return s;
		}
		
		string set_message(User send){	
			cout<<"Type your message here:\t";
			getline(cin,mnm);
			
			mnm = send.getName() + ": " + mnm + "\t\t"  + "Likes: " + to_string(likes) +"\t||" + timestamp;
			return mnm;
		}
		
		bool can_post(User send, User rec){
			string friendList = send.getName() + "_friends.txt";
			string data;
			bool flag=0;
			fstream strm;
			strm.open(friendList.c_str(), ios::in | ios::out | ios::app);
			while(strm.is_open() && !strm.eof()){
				getline(strm,data);
				if(data== send.getName() || data== rec.getName()){
					strm.close();
					return 1;
				}
			}
			strm.close();
			return 0;
		}
		
		string get_message(User send, bool reply){
			set_message(send);
			if(!reply){
				return mnm;
			}else{
				return "\t"+mnm;
			}
		}
		
		
	public:
		Message(){
		}
		void post(User send, User rec, bool reply){
			if(can_post(send,rec)){
				string userWall = rec.getName() + "_wall.txt";
				fstream wall;
				wall.open(userWall.c_str(), ios::in | ios::out | ios::app);
				wall<<get_message(send,reply);
				wall.close();
			}else{
				cout<<"You can not post to that wall!"<<endl;
			}
		}
		
		string get_timestamp(){
			return mtd;
		}
		
		void set_likes(){
			likes++;
		}
};

class Like{
	private:
		vector<string> fileToVector(string fileName){
			string data;
			vector<string> vec;
			fstream strm;
			strm.open(fileName.c_str(), ios::in | ios::out | ios::app);
			
			while(strm.is_open() && !strm.eof()){
				strm>>data;
				if(data!="\n"){
					vec.push_back(data);
				}
			}
			strm.close();
			return vec;
		}
		
		bool can_like(User send, User rec, Message m1){
			int i;
			string userStamps = send.getName() + "_timestamp.txt";
			string td = m1.get_timestamp();
			vector<string> vec;
			vec = fileToVector(userStamps.c_str());
			for(i=0;i<vec.size()-1;i++){
				if(vec[i]==td){
					return 0;
				}
			}
			return 1;
		}
		
	public:
		void like_post(User send, User rec, Message m1){
			if(can_like(send,rec,m1)){
				string userStamps = send.getName() + "_timestamp.txt";
				m1.set_likes();
				fstream td;
				td.open(userStamps.c_str(), ios::in | ios::out | ios::app);
				td<<m1.get_timestamp();
				td.close();				
			}else{
				cout<<"You have already liked the post!"<<endl;
			}
		}
};
> how to make the timestamp constant, so it doesen't change every single time
> I try to access it from another class
Message::timestamp is initialised in the constructor and then it's never modified
perhaps you are creating another instance instead of working on the original

show enough code in order to reproduce your issue.
Topic archived. No new replies allowed.