OutStream problem

This is actually a followup to my last post

So I have this list of titles, dates, times, and durations and I managed to get all them to to output in an orderly fashion however there seems to be some problems with the time in particular.

Also I need these 5 functions for argc-argv however I cannot do this yet because the time is kind of screwed up (specifically with pm):

• ./a.out -ps
o Print my daily schedule in order by starting time using standard time format. The
appointments must be displayed in a table format with proper labels.
• ./a.out -p "time"
o Print all appointments at specified military time.
• ./a.out -a "Appointment data"
o Add an appointment given in the format:
"title|year|month|day|time|duration".
Time is given in standard time and duration is given in minutes. Leading and
trailing spaces must be removed.
Example: " Meeting with Bob | 2019 |4 |29 |8:30 aM |15 "
• ./a.out -dt "title"
o Delete all appointments that match a title exactly. It should work for both upperand lower-case characters with leading and trailing spaces removed.
• ./a.out -dm "time"
o Delete a all appointments that match the starting military time exactly.

Output I have at the moment:
Department meeting  2021-10-29  9:30am  15       
Meeting with Bob  2021-10-29  8:30am  15
Meeting with Jim  2021-10-29  9:00am  15
Doctor's appointment  2021-10-29  10:30am  15    
Meeting with Bob  2021-10-29  11:30am  15        
Lunch meeting with dean  2021-10-29  11:45am  15
Invalid time 12:30 pM
Lunch with the guys  2021-10-29  12:00am  60
Invalid time 12:30 pM
Lunch with the guys  2021-10-29  12:00am  60
Invalid time 12:30 pM
Lunch with the guys  2021-10-29  12:00am  60
Meeting With BOB  2021-10-29  1:30pm  15
Chair meeting  2021-10-29  2:30pm  15
Meeting WITH Bob  2021-10-29  3:30pm  20
Invalid month 0
Fishing with Donald and Donald  2021-1-29  8:14am  115
Fishing with Donald and Billy  2021-10-29  2:45pm  15
Appointment with Donald  2021-10-29  8:56pm  115
Appointment with Fred  2021-10-29  8:30pm  50
Invalid appointment format!
N/A  1-1-1  12:00am  1
Lunch  2021-10-29  10:58pm  115
Fishing with Bob and Fred  2021-10-29  2:45pm  10
Skiing with Juedes  2021-10-29  9:15am  60


^ Decent but not quite the best

Here's the original raw text file (wonky spaces and all):


 Department meeting | 2021 |10 |29 |9:30 aM  |15 
    Meeting with Bob | 2021 |10 |29 |8:30 aM  |15 
 Meeting with Jim | 2021 |10 |29 |9:00 aM  |15 

 Doctor's appointment | 2021 |10 |29 |10:30 aM  |15 
 Meeting with Bob | 2021 |10 |29 |11:30 aM  |15 
    Lunch meeting with dean | 2021 |10 |29 |11:45 aM  |15 
 Lunch with the guys | 2021 |10 |29 |12:30 pM  |60 
    Lunch with the guys | 2021 |10 |29 |12:30 pM  |60 
         Lunch with the guys | 2021 |10 |29 |12:30 pM  |60 
 Meeting With BOB | 2021 |10 |29 |1:30 pM  |15 

 Chair meeting | 2021 |10 |29 |2:30 PM  |15 
 Meeting WITH Bob | 2021 |10 |29 |3:30 pm  |20 
         Fishing with Donald and Donald|2021|0|29|8:14AM| 115
Fishing with Donald and Billy|  2021|10|29  | 2:45 PM|15
Appointment with Donald|2021  |10|29|8:56PM |115
Appointment with Fred|2021|10|29|8:30PM|50
            


Lunch|2021|10  | 29|10:58PM |115
Fishing with Bob and Fred|2021 |10| 29| 2:45PM|10
Skiing with Juedes|  2021|10  | 29|9:15 am|60 


Here's my code so far (thanks SeePlus):
Last edited on
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <sstream>
#include <cctype>
#include <fstream>

using namespace std;

class Appointment
{
public:
    Appointment();
    Appointment(const string &appData);

    // Getters
    string getTitle() const;
    unsigned getYear() const;
    unsigned getMonth() const;
    unsigned getDay() const;
    unsigned getTime() const; // military time
    unsigned getDuration() const;
    string getDate() const;
    string getStandardTime() const;

    // Setters
    bool setTitle(const string &newTitle);
    bool setYear(unsigned newYear);
    bool setMonth(unsigned newMonth);
    bool setDay(unsigned newDay);
    bool setTime(unsigned newTime); // military time
    bool setDuration(unsigned newDuration);
    bool setDate(unsigned newYear, unsigned newMonth, unsigned newDay);

    // Helpers
    static string militaryToStandard(unsigned time);
    static unsigned standardToMilitary(const string &time);

private: /// Technical ctor (replaces the default appointment ctor)
    string title{"N/A"};
    unsigned year{1};
    unsigned month{1};
    unsigned day{1};
    unsigned time{0};
    unsigned duration{1};
};

ostream &operator<<(ostream &os, const Appointment &app) /// output
{
    return os << app.getTitle() << "  " << app.getDate() << "  " << app.getStandardTime() << "  " << app.getDuration();
}

Appointment::Appointment() {} // default ctor (Look in private data)

Appointment::Appointment(const string &appData) /// ctor
{
    // appData = " Meeting with Bob | 2021 | 11 | 23 | 8 : 30 aM | 15 ";
    string token{};
    vector<string> vs;
    for (const auto &ch : appData)
        if (ch != '|')
            token += ch;
        else
        {
            auto itr{token.begin()};
            auto itb{token.rbegin()};

            for (; itr != token.end() && *itr == ' '; ++itr)
                ;
            for (; itb != token.rend() && *itb == ' '; ++itb)
                ;
            vs.emplace_back(itr, itb.base());
            token.clear();
        }

    if (token.length())
        vs.push_back(token);

    // for (const auto& a : vs)
    // cout << a << '\n';

    if (vs.size() != 6) /// Error checker
    {
        cout << "Invalid appointment format!\n";
        
        return;
    }

    if (!setTitle(vs[0]))
        cout << "No title\n";

    if (!setYear(stoul(vs[1])))
        cout << "Invalid year " << vs[1] << '\n';

    if (!setMonth(stoul(vs[2])))
        cout << "Invalid month " << vs[2] << '\n';

    if (!setDay(stoul(vs[3])))
        cout << "Invalid day " << vs[3] << '\n';

    if (!setTime(standardToMilitary(vs[4])))
        cout << "Invalid time " << vs[4] << '\n';

    if (!setDuration(stoul(vs[5])))
        cout << "Invalid duration " << vs[5] << '\n';
}

bool Appointment::setTitle(const string &newTitle) /// setTitle
{
    if (!newTitle.empty())
    {
        title = newTitle;
        return true;
    }

    return false;
}

bool Appointment::setYear(unsigned newYear) /// setYear
{
    if (newYear > 1970)
    {
        year = newYear;
        return true;
    }

    return false;
}

bool Appointment::setMonth(unsigned newMonth) /// setMonth
{
    if (newMonth > 0 && newMonth <= 12)
    {
        month = newMonth;
        return true;
    }

    return false;
}

bool Appointment::setDay(unsigned newDay) /// setDay
{
    if (newDay > 0 && newDay <= 31)
    {
        day = newDay;
        return true;
    }

    return false;
}

bool Appointment::setTime(unsigned newTime) /// setTime
{
    if (newTime < 2400)
    {
        time = newTime;
        return true;
    }

    return false;
}

bool Appointment::setDuration(unsigned newDuration) /// setDuration
{
    if (newDuration > 0)
    {
        duration = newDuration;
        return true;
    }

    return false;
}

bool Appointment::setDate(unsigned newYear, unsigned newMonth, unsigned newDay) /// setDate
{
    return setYear(newYear) && setMonth(newMonth) && setDay(newDay);
}

string Appointment::getTitle() const /// gettitle
{
    return title;
}

unsigned Appointment::getYear() const /// getyear
{
    return year;
}

unsigned Appointment::getMonth() const /// getmonth
{
    return month;
}

unsigned Appointment::getDay() const /// getday
{
    return day;
}

unsigned Appointment::getTime() const /// getTime
{
    return time;
}

unsigned Appointment::getDuration() const /// getDuration
{
    return duration;
}

string Appointment::getDate() const /// getdate
{
    ostringstream oss;

    oss << year << '-' << month << '-' << day;
    return oss.str();
}

string Appointment::getStandardTime() const // standard time getter
{
    return militaryToStandard(getTime());
}

string Appointment::militaryToStandard(unsigned miltime) /// MtS converter
{
    unsigned minute{miltime % 100};
    unsigned hour{miltime / 100 % 24};
    bool PM{};

    if (hour == 0)
        hour += 12;
    else if (hour >= 12)
    {
        hour -= 12 * (hour > 12);
        PM = true;
    }

    ostringstream out;

    out << hour << ':' << setw(2) << setfill('0') << minute << (PM ? "pm" : "am"); /// pm am checker and string collector
    return out.str();
}

unsigned Appointment::standardToMilitary(const string &time) /// convertStM
{
    istringstream iss(time);
    unsigned hour{}, minute{};
    char del{};
    string ampm;

    // NOTE - no error checking! Sorry
    iss >> hour >> del >> minute >> ampm;

    ampm[0] = static_cast<char>(toupper(static_cast<unsigned char>(ampm[0])));
    ampm[1] = static_cast<char>(toupper(static_cast<unsigned char>(ampm[1])));

    const bool pm{ampm == "PM"};

    return ((hour == 12 && minute == 0 && !pm) ? 0 : hour += 12 * (pm == true)) * 100 + minute;
}

bool operator==(const Appointment &first, const Appointment &second)
{
    return first.getYear() == second.getYear() && first.getMonth() == second.getMonth() &&
           first.getDay() == second.getDay() && first.getTime() == second.getTime() &&
           first.getDuration() == second.getDuration();
}

int main(int argc, char *argv[])
{
    //const Appointment myApp{"Skiing with Juedes|  2021|10  | 29|12:10 am|60 "}; // example for testing

    //cout << myApp << '\n'; // test checker!

    cout << "argc: " << argc << endl;
    if(argc > 4) {
        cout << "Too many Args" << endl;
    }
    for (size_t i = 0; i < argc; i++)
    {
        string temp = argv[i];
        cout << "Arg " << i << ": " << temp << endl;
    }

    ifstream inStream;
    string fileName = "agenda.txt";
    inStream.open(fileName);
    if (inStream.fail())
    {
        cout << "Error: file not found " << fileName << endl;
        exit(0);
    }

    string line;
    while (getline(inStream, line))
    {
        if (line != "")
        {
            const Appointment myApp{line};
            cout << myApp << '\n';
        }   

    }
    
}
Ok. Without the command line args, then:

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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <sstream>
#include <cctype>
#include <fstream>

using namespace std;

class Appointment {
public:
	Appointment();
	Appointment(const string& appData);

	bool isvalid() const { return valid; }

	// Getters
	string getTitle() const;
	unsigned getYear() const;
	unsigned getMonth() const;
	unsigned getDay() const;
	unsigned getTime() const; // military time
	unsigned getDuration() const;
	string getDate() const;
	string getStandardTime() const;

	// Setters
	bool setTitle(const string& newTitle);
	bool setYear(unsigned newYear);
	bool setMonth(unsigned newMonth);
	bool setDay(unsigned newDay);
	bool setTime(unsigned newTime); // military time
	bool setDuration(unsigned newDuration);
	bool setDate(unsigned newYear, unsigned newMonth, unsigned newDay);

	// Helpers
	static string militaryToStandard(unsigned time);
	static unsigned standardToMilitary(const string& time);
	static string trim(const std::string s);

private: /// Technical ctor (replaces the default appointment ctor)
	string title {"N/A"};
	unsigned year {1};
	unsigned month {1};
	unsigned day {1};
	unsigned time {0};
	unsigned duration {1};
	bool valid {};
};

ostream& operator<<(ostream& os, const Appointment& app) /// output
{
	return os << app.getTitle() << "  " << app.getDate() << "  " << app.getStandardTime() << "  " << app.getDuration();
}

string Appointment::trim(const std::string token) {
	auto itr {token.begin()};
	auto itb {token.rbegin()};

	for (; itr != token.end() && *itr == ' '; ++itr);
	for (; itb != token.rend() && *itb == ' '; ++itb);

	return itr != token.end() ? string(itr, itb.base()) : string {};
}

Appointment::Appointment() {}

Appointment::Appointment(const string& appData) {
	// appData = " Meeting with Bob | 2021 | 11 | 23 | 8 : 30 aM | 15 ";
	string token {};
	vector<string> vs;

	for (const auto& ch : appData)
		if (ch != '|')
			token += ch;
		else {
			if (const auto t {trim(token)}; !t.empty())
				vs.push_back(t);

			token.clear();
		}

	if (!token.empty())
		if (const auto t {trim(token)}; !t.empty())
			vs.push_back(t);

	// for (const auto& a : vs)
	// cout << a << '\n';

	if (vs.empty())
		return;

	if (vs.size() != 6) {
		cout << "Invalid appointment format!\n";
		return;
	}

	if (!setTitle(vs[0]))
		cout << "No title\n";

	if (!setYear(stoul(vs[1])))
		cout << "Invalid year " << vs[1] << '\n';

	if (!setMonth(stoul(vs[2])))
		cout << "Invalid month " << vs[2] << '\n';

	if (!setDay(stoul(vs[3])))
		cout << "Invalid day " << vs[3] << '\n';

	if (!setTime(standardToMilitary(vs[4])))
		cout << "Invalid time " << vs[4] << '\n';

	if (!setDuration(stoul(vs[5])))
		cout << "Invalid duration " << vs[5] << '\n';

	valid = true;
}

bool Appointment::setTitle(const string& newTitle) /// setTitle
{
	if (!newTitle.empty()) {
		title = newTitle;
		return true;
	}

	return false;
}

bool Appointment::setYear(unsigned newYear) /// setYear
{
	if (newYear > 1970) {
		year = newYear;
		return true;
	}

	return false;
}

bool Appointment::setMonth(unsigned newMonth) /// setMonth
{
	if (newMonth > 0 && newMonth <= 12) {
		month = newMonth;
		return true;
	}

	return false;
}

bool Appointment::setDay(unsigned newDay) /// setDay
{
	if (newDay > 0 && newDay <= 31) {
		day = newDay;
		return true;
	}

	return false;
}

bool Appointment::setTime(unsigned newTime) /// setTime
{
	if (newTime < 2400) {
		time = newTime;
		return true;
	}

	return false;
}

bool Appointment::setDuration(unsigned newDuration) /// setDuration
{
	if (newDuration > 0) {
		duration = newDuration;
		return true;
	}

	return false;
}

bool Appointment::setDate(unsigned newYear, unsigned newMonth, unsigned newDay) /// setDate
{
	return setYear(newYear) && setMonth(newMonth) && setDay(newDay);
}

string Appointment::getTitle() const /// gettitle
{
	return title;
}

unsigned Appointment::getYear() const /// getyear
{
	return year;
}

unsigned Appointment::getMonth() const /// getmonth
{
	return month;
}

unsigned Appointment::getDay() const /// getday
{
	return day;
}

unsigned Appointment::getTime() const /// getTime
{
	return time;
}

unsigned Appointment::getDuration() const /// getDuration
{
	return duration;
}

string Appointment::getDate() const /// getdate
{
	ostringstream oss;

	oss << year << '-' << month << '-' << day;
	return oss.str();
}

string Appointment::getStandardTime() const // standard time getter
{
	return militaryToStandard(getTime());
}

string Appointment::militaryToStandard(unsigned miltime) /// MtS converter
{
	unsigned minute {miltime % 100};
	unsigned hour {miltime / 100 % 24};
	bool PM {};

	if (hour == 0)
		hour += 12;
	else if (hour >= 12) {
		hour -= 12 * (hour > 12);
		PM = true;
	}

	ostringstream out;

	out << hour << ':' << setw(2) << setfill('0') << minute << (PM ? "pm" : "am"); /// pm am checker and string collector
	return out.str();
}

unsigned Appointment::standardToMilitary(const string& time) {
	istringstream iss(time);
	unsigned hour {}, minute {};
	char del {};
	string ampm;

	// NOTE - no error checking! Sorry
	iss >> hour >> del >> minute >> ampm;

	ampm[0] = static_cast<char>(toupper(static_cast<unsigned char>(ampm[0])));
	ampm[1] = static_cast<char>(toupper(static_cast<unsigned char>(ampm[1])));

	const bool pm {ampm == "PM"};

	if (hour == 12) {
		if (!pm)
			hour = 0;
	} else if (pm)
		hour += 12;

	return hour * 100 + minute;
}

bool operator==(const Appointment& first, const Appointment& second) {
	return first.getYear() == second.getYear() && first.getMonth() == second.getMonth() &&
		first.getDay() == second.getDay() && first.getTime() == second.getTime() &&
		first.getDuration() == second.getDuration();
}

int main(int argc, char* argv[]) {
	//const Appointment myApp{"Skiing with Juedes|  2021|10  | 29|12:10 am|60 "}; // example for testing

	cout << "argc: " << argc << '\n';

	if (argc > 4)
		return (cout << "Too many Args\n"), 1;

	for (size_t i {}; i < argc; ++i) {
		const string temp {argv[i]};

		cout << "Arg " << i << ": " << temp << '\n';
	}

	const std::string fileName {"agenda.txt"};

	ifstream inStream(fileName);

	if (!inStream)
		return (cout << "Error: file not found " << fileName << '\n'), 2;

	for (string line; getline(inStream, line); )
		if (!line.empty()) {
			const Appointment myApp {line};

			if (myApp.isvalid())
				cout << myApp << '\n';
		}
}



Department meeting  2021-10-29  9:30am  15
Meeting with Bob  2021-10-29  8:30am  15
Meeting with Jim  2021-10-29  9:00am  15
Doctor's appointment  2021-10-29  10:30am  15
Meeting with Bob  2021-10-29  11:30am  15
Lunch meeting with dean  2021-10-29  11:45am  15
Lunch with the guys  2021-10-29  12:30pm  60
Lunch with the guys  2021-10-29  12:30pm  60
Lunch with the guys  2021-10-29  12:30pm  60
Meeting With BOB  2021-10-29  1:30pm  15
Chair meeting  2021-10-29  2:30pm  15
Meeting WITH Bob  2021-10-29  3:30pm  20
Invalid month 0
Fishing with Donald and Donald  2021-1-29  8:14am  115
Fishing with Donald and Billy  2021-10-29  2:45pm  15
Appointment with Donald  2021-10-29  8:56pm  115
Appointment with Fred  2021-10-29  8:30pm  50
Lunch  2021-10-29  10:58pm  115
Fishing with Bob and Fred  2021-10-29  2:45pm  10
Skiing with Juedes  2021-10-29  9:15am  60


There is only the one error now correctly reported - invalid month.

Oh Thank you! Also how would you got about sorting the times so that the entire lines are printed in order

I'm just trouble with sorting the entire line:

1
2
3
4
5
6
7
8
9
10
if (option == "-ps") //All "-ps" is a command line arg
    {
        while (getline(inStream, line))
        {
            if(!line.empty()){
                //Something to do with printing the sorted timeline
            }
        }
        
    }


TLDR: Basically I'm trying to sort the schedule in order of time

UPDATE: I managed to make a code for the -p function however the output may be a bit iffy:
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
const string fileName{"agenda.txt"};

    ifstream inStream(fileName);
    string option = argv[1];
    int timeState = atoi(argv[2]);

    if (!inStream)
        return (cout << "Error: file not found " << fileName << '\n'), 2;

    for (string line; getline(inStream, line);)
        if (!line.empty())
        {
            const Appointment myApp{line};

            if (myApp.isvalid())

                if (option == "-ps") //This Doesn't Work at the moment
                {
                    for (size_t i = 0; i < myApp; i++)
                    {
                        sort(myApp.getTime().begin(), myApp.getTime().end());
                    }
                }

            if (option == "-p") //Works!
            {
                if (myApp.getTime() == timeState)
                {
                    cout << myApp << endl;
                }
            }
        }


The output:
./a.exe -p "1230"
Lunch with the guys  2021-10-29  12:30pm  60
Lunch with the guys  2021-10-29  12:30pm  60
Lunch with the guys  2021-10-29  12:30pm  60
Invalid month 0



Last edited on
If you want to sort the appointments by time (or any other element), then you're going to have to store the appointments in a container (probably a vector) and then sort by date and then by time. Something like:

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
#include <vector>
#include <algorithm>
...
int main(int argc, char* argv[]) {
	//const Appointment myApp{"Skiing with Juedes|  2021|10  | 29|12:10 am|60 "}; // example for testing

	cout << "argc: " << argc << '\n';

	if (argc > 4)
		return (cout << "Too many Args\n"), 1;

	for (size_t i {}; i < argc; ++i) {
		const string temp {argv[i]};

		cout << "Arg " << i << ": " << temp << '\n';
	}

	const std::string fileName {"agenda.txt"};

	ifstream inStream(fileName);

	if (!inStream)
		return (cout << "Error: file not found " << fileName << '\n'), 2;

	vector<Appointment> apps;

	for (string line; getline(inStream, line); )
		if (!line.empty()) {
			const Appointment myApp {line};

			if (myApp.isvalid())
				//cout << myApp << '\n';
				apps.push_back(myApp);
		}

	sort(apps.begin(), apps.end(), [](const auto& a, const auto& b) {
		if (a.getYear() < b.getYear()) return true;
		if (a.getYear() == b.getYear()) {
			if (a.getMonth() < b.getMonth()) return true;
			if (a.getMonth() == b.getMonth()) {
				if (a.getDay() < b.getDay()) return true;
				if (a.getDay() == b.getDay()) {
					if (a.getTime() < b.getTime()) return true;
				}
			}
		}
		return false;
		});

	for (const auto& a : apps)
		std::cout << a << '\n';
}



Fishing with Donald and Donald  2021-10-29  8:14am  115
Meeting with Bob  2021-10-29  8:30am  15
Meeting with Jim  2021-10-29  9:00am  15
Skiing with Juedes  2021-10-29  9:15am  60
Department meeting  2021-10-29  9:30am  15
Doctor's appointment  2021-10-29  10:30am  15
Meeting with Bob  2021-10-29  11:30am  15
Lunch meeting with dean  2021-10-29  11:45am  15
Lunch with the guys  2021-10-29  12:30pm  60
Lunch with the guys  2021-10-29  12:30pm  60
Lunch with the guys  2021-10-29  12:30pm  60
Meeting With BOB  2021-10-29  1:30pm  15
Chair meeting  2021-10-29  2:30pm  15
Fishing with Donald and Billy  2021-10-29  2:45pm  15
Fishing with Bob and Fred  2021-10-29  2:45pm  10
Meeting WITH Bob  2021-10-29  3:30pm  20
Appointment with Fred  2021-10-29  8:30pm  50
Appointment with Donald  2021-10-29  8:56pm  115
Lunch  2021-10-29  10:58pm  115


when the invalid 0 month in the file is corrected to 10.
Last edited on
Okay So I've made some serious progress and I used that vector sorting function and now I have 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
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
int main(int argc, char *argv[])
{
    // const Appointment myApp{"Skiing with Juedes|  2021|10  | 29|12:10 am|60 "}; // example for testing

    // cout << "argc: " << argc << '\n';

    if (argc > 4)
        return (cout << "Too many Args\n"), 1;

    // for (size_t i{}; i < argc; ++i)
    // {
    //     const string temp{argv[i]};

    //     cout << "Arg " << i << ": " << temp << '\n';
    // }

    const string fileName{"agenda.txt"};

    ifstream inStream(fileName);
    string option = argv[1];
    int timeState = atoi(argv[2]);
    if (option == "-a")
    {
        string newA = argv[2];
        Appointment newApp{newA};
        cout << newApp << endl;
    }

    if (!inStream)
        return (cout << "Error: file not found " << fileName << '\n'), 2;

    for (string line; getline(inStream, line);)
        if (!line.empty())
        {
            const Appointment myApp{line};

            if (myApp.isvalid())
            {

                if (option == "-ps")
                {
                    vector<Appointment> apps;

                    for (string line; getline(inStream, line);)
                        if (!line.empty())
                        {
                            const Appointment myApp{line};

                            if (myApp.isvalid())
                                // cout << myApp << '\n';
                                apps.push_back(myApp);
                        }

                    sort(apps.begin(), apps.end(), [](const auto &a, const auto &b)
                         {
		if (a.getYear() < b.getYear()) return true;
		if (a.getYear() == b.getYear()) {
			if (a.getMonth() < b.getMonth()) return true;
			if (a.getMonth() == b.getMonth()) {
				if (a.getDay() < b.getDay()) return true;
				if (a.getDay() == b.getDay()) {
					if (a.getTime() < b.getTime()) return true;
				}
			}
		}
		return false; });

                    for (const auto &a : apps)
                        std::cout << a << '\n';
                }
            }

            if (option == "-p")
            {
                if (myApp.getTime() == timeState)
                {
                    cout << myApp << endl;
                }
            }

            // if (option == "-dt")
            // {
            //     string outTitle = argv[2];
            //     string tempTitle = myApp.getTitle();
            //     for (int i = 0; i < tempTitle.size(); i++)
            //     {
            //         tempTitle.tolower(i);
            //         outTitle.tolower(i);
            //     }

            //     if (!(tempTitle == outTitle))
            //     {
            //         cout << myApp << endl;
            //     }
            // }
        }
}


Would you know how to convert an entire string to lowercase? I've attempted it here but got some errors
re the sort. If you provide a member function to return the date as a number in the format yyyymmdd, then the sort can be simplified:

1
2
3
4
5
6
7
8
9
10
11
12
13
unsigned getnumDate() const;
...
unsigned Appointment::getnumDate() const {
	return year * 10000 + month * 100 + day;
}
...
sort(apps.begin(), apps.end(), [](const auto& a, const auto& b) {
		if (a.getnumDate() < b.getnumDate()) return true;
		if (a.getnumDate() == b.getnumDate())
			if (a.getTime() < b.getTime()) return true;

		return false;
		});

This will convert a string to lowercase:

1
2
3
4
void toLower(string& s) {
	for (auto& c : s)
		c = static_cast<char>(tolower(static_cast<unsigned char>(c)));
}

Seeplus, I don't think it would be necessary to sort with the date included as all the days, months, and years are exactly the same. I'm just worried about sorting ONLY the time here and nothing else. While I could include a member function to allow me to sort with dates and times it is only asking me to sort by time (or a one day time planner)
Hey I encountered another problem with processing the file specifically with the day? What's going on here?

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
int main(int argc, char *argv[])
{
    // const Appointment myApp{"Skiing with Juedes|  2021|10  | 29|12:10 am|60 "}; // example for testing

    // cout << "argc: " << argc << '\n';

    if (argc > 4)
        return (cout << "Too many Args\n"), 1;

    // for (size_t i{}; i < argc; ++i)
    // {
    //     const string temp{argv[i]};

    //     cout << "Arg " << i << ": " << temp << '\n';
    // }

    const string fileName{"agenda.txt"};

    ifstream inStream(fileName);

    ofstream outStream;
    outStream.open("trust.txt");
    if (outStream.fail())
    {
        cout << "Error" << endl;
    }

    string option = argv[1];
    int timeState = atoi(argv[2]);

    if (option == "-a")
    {
        string newA = argv[2];

        Appointment newApp{newA};
        outStream << newApp << endl;
    }

    if (!inStream)
        return (cout << "Error: file not found " << fileName << '\n'), 2;

    vector<Appointment> apps;

    for (string line; getline(inStream, line);)
    {
        if (!line.empty())
        {
            const Appointment myApp{line};

            if (myApp.isvalid())
            {
                // cout << myApp << '\n';
                apps.push_back(myApp);

                if (myApp.isvalid())
                {

                    if (option == "-p")
                    {
                        if (myApp.getTime() == timeState)
                        {
                            outStream << myApp << endl;
                        }
                    }

                    if (option == "-dt")
                    {
                        string outTitle = argv[2];
                        string tempTitle = myApp.getTitle();
                        transform(outTitle.begin(), outTitle.end(), outTitle.begin(), ::tolower);
                        transform(tempTitle.begin(), tempTitle.end(), tempTitle.begin(), ::tolower);

                        if (!(tempTitle == outTitle))
                        {
                            outStream << myApp << endl;
                        }
                    }

                    if (option == "-dm")
                    {
                        if (!(myApp.getTime() == timeState))
                        {
                            outStream << myApp << endl;
                        }
                    }
                }
            }
        }
    }

    if (option == "-ps")
    {

        sort(apps.begin(), apps.end(), [](const auto &a, const auto &b)
             {
		if (a.getYear() < b.getYear()) return true;
		if (a.getYear() == b.getYear()) {
			if (a.getMonth() < b.getMonth()) return true;
			if (a.getMonth() == b.getMonth()) {
				if (a.getDay() < b.getDay()) return true;
				if (a.getDay() == b.getDay()) {
					if (a.getTime() < b.getTime()) return true;
				}
			}
		}
		return false; });

        for (const auto &a : apps)
            outStream << a << '\n';
    }
}


./a.exe -ps

trust.txt:
Fishing with Donald and Donald  2021-1-29  8:14am  115
Meeting with Bob  2021-10-1  8:30am  15
Meeting with Jim  2021-10-1  9:00am  15
Skiing with Juedes  2021-10-1  9:15am  60
Department meeting  2021-10-1  9:30am  15
Doctor's appointment  2021-10-1  10:30am  15
Meeting with Bob  2021-10-1  11:30am  15
Lunch meeting with dean  2021-10-1  11:45am  15
Lunch with the guys  2021-10-1  12:30pm  60
Lunch with the guys  2021-10-1  12:30pm  60
Lunch with the guys  2021-10-1  12:30pm  60
Meeting With BOB  2021-10-1  1:30pm  15
Chair meeting  2021-10-1  2:30pm  15
Fishing with Donald and Billy  2021-10-1  2:45pm  15
Fishing with Bob and Fred  2021-10-1  2:45pm  10
Meeting WITH Bob  2021-10-1  3:30pm  20
Appointment with Fred  2021-10-1  8:30pm  50
Appointment with Donald  2021-10-1  8:56pm  115
Lunch  2021-10-1  10:58pm  115


./a.exe -a "Dinner with Paul Allen | 2000 | 8 | 8 | 8:30pm | 65" 

trust.txt (with -a)
Dinner with Paul Allen  2000-8-1  8:30pm  65



EDIT: This issue has been solved
Last edited on
Dorsia? On a Tuesday night? How'd you swing that?
Okay so my program is 99.999% done its just I need one single thing to work. I have this file called "agenda.txt" and I was wondering how to update the file whenever something is outstreamed to the file. For example:
./a.exe -ps

This should print the schedule like this (it did print like this just in a different file):
Fishing with Donald and Donald | 2021 | 1 | 29 | 8:14am | 115
Meeting with Bob | 2021 | 10 | 29 | 8:30am | 15
Meeting with Jim | 2021 | 10 | 29 | 9:00am | 15
Skiing with Juedes | 2021 | 10 | 29 | 9:15am | 60
Department meeting | 2021 | 10 | 29 | 9:30am | 15
Doctor's appointment | 2021 | 10 | 29 | 10:30am | 15
Meeting with Bob | 2021 | 10 | 29 | 11:30am | 15
Lunch meeting with dean | 2021 | 10 | 29 | 11:45am | 15
Lunch with the guys | 2021 | 10 | 29 | 12:30pm | 60
Lunch with the guys | 2021 | 10 | 29 | 12:30pm | 60
Lunch with the guys | 2021 | 10 | 29 | 12:30pm | 60
Meeting With BOB | 2021 | 10 | 29 | 1:30pm | 15
Chair meeting | 2021 | 10 | 29 | 2:30pm | 15
Fishing with Donald and Billy | 2021 | 10 | 29 | 2:45pm | 15
Fishing with Bob and Fred | 2021 | 10 | 29 | 2:45pm | 10
Meeting WITH Bob | 2021 | 10 | 29 | 3:30pm | 20
Appointment with Fred | 2021 | 10 | 29 | 8:30pm | 50
Appointment with Donald | 2021 | 10 | 29 | 8:56pm | 115
Lunch | 2021 | 10 | 29 | 10:58pm | 115


However whenever the Outstream is linked to the same file it erases everything
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
int main(int argc, char *argv[])
{
    // const Appointment myApp{"Skiing with Juedes|  2021|10  | 29|12:10 am|60 "}; // example for testing

    // cout << "argc: " << argc << '\n';

    if (argc > 4)
        return (cout << "Too many Args\n"), 1;

    // for (size_t i{}; i < argc; ++i)
    // {
    //     const string temp{argv[i]};

    //     cout << "Arg " << i << ": " << temp << '\n';
    // }

    const string fileName{"agenda.txt"};

    ifstream inStream(fileName);

    ofstream outStream;
    outStream.open("trust.txt"); //I want this to be agenda.txt
    if (outStream.fail())
    {
        cout << "Error" << endl;
    }

    string option = argv[1];
    int timeState = atoi(argv[2]);

    if (!inStream)
        return (cout << "Error: file not found " << fileName << '\n'), 2;

    vector<Appointment> apps;

    for (string line; getline(inStream, line);)
    {
        if (!line.empty())
        {
            const Appointment myApp{line};

            if (myApp.isvalid())
            {
                // cout << myApp << '\n';
                apps.push_back(myApp);

                if (myApp.isvalid())
                {

                    if (option == "-p")
                    {
                        if (myApp.getTime() == timeState)
                        {
                            outStream << myApp << endl;
                        }
                    }

                    if (option == "-dt")
                    {
                        string outTitle = argv[2];
                        string tempTitle = myApp.getTitle();
                        transform(outTitle.begin(), outTitle.end(), outTitle.begin(), ::tolower);
                        transform(tempTitle.begin(), tempTitle.end(), tempTitle.begin(), ::tolower);

                        if (!(tempTitle == outTitle))
                        {
                            outStream << myApp << endl;
                        }
                    }

                    if (option == "-dm")
                    {
                        if (!(myApp.getTime() == timeState))
                        {
                            outStream << myApp << endl;
                        }
                    }

                    if (option == "-a")
                    {
                        outStream << myApp << endl;
                    }
                }
            }
        }
    }

    if (option == "-a")
    {
        string newA = argv[2];
        Appointment newApp{newA};
        outStream << newApp << endl;
    }

    if (option == "-ps")
    {

        sort(apps.begin(), apps.end(), [](const auto &a, const auto &b)
             {
		if (a.getYear() < b.getYear()) return true;
		if (a.getYear() == b.getYear()) {
			if (a.getMonth() < b.getMonth()) return true;
			if (a.getMonth() == b.getMonth()) {
				if (a.getDay() < b.getDay()) return true;
				if (a.getDay() == b.getDay()) {
					if (a.getTime() < b.getTime()) return true;
				}
			}
		}
		return false; });

        for (const auto &a : apps)
            outStream << a << '\n';
    }
}
Last edited on
ofstream deletes everything in the file when it is opened. If you want to append to the end of the data, then you need to open in append mode (look it up).

However, isn't this operation what is required? Read in the file contents to the vector, change as required as per the command line argument(s) and then write the whole vector contents back to the file?
I looked it up however it is still quite confusing. Where would I place the outstream so the instream processes before the outstream.

Here's what I have:
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
 //...
string fileName{"agenda.txt"};

    ifstream inStream(fileName);

    ofstream outStream;
    outStream.open(fileName);
    if (outStream.fail())
    {
        cout << "Error" << endl;
    }

    string option = argv[1];
    int timeState = atoi(argv[2]);

    if (!inStream)
        return (cout << "Error: file not found " << fileName << '\n'), 2;

    vector<Appointment> apps;

    for (string line; getline(inStream, line);)
    {
        if (!line.empty())
        {
            const Appointment myApp{line};
//... 
Last edited on
Where would I place the outstream so the instream processes before the outstream.


At the end of main after all processing. Basically in pseudo code:

1
2
3
open input file and read all data
loop processing until all actions performed
open output file and write all data

Topic archived. No new replies allowed.