How to assign values against struct properties in 5 Dimensional Array in c++ ?

hi,
I have 5 dimensional array of struct in pointer form.

I want to assign value properties at 5th dimension.

please see my code and suggest me.

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
 #include<iostream>
#include<fstream>
#include <cstring>
#include <string>
using namespace std;
struct Activity
{
	char* title;
	char* userid;
	int duration;
	float priority;
};
char* mystrtok(char* s, char d)
{
    // Stores the state of string
    static char* input = NULL;
 
    // Initialize the input string
    if (s != NULL)
        input = s;
 
    // Case for final token
    if (input == NULL)
        return NULL;
 
    // Stores the extracted string
    char* result = new char[strlen(input) + 1];
    int i = 0;
 
    // Start extracting string and
    // store it in array
    for (; input[i] != '\0'; i++) {
 
        // If delimiter is not reached
        // then add the current character
        // to result[i]
        if (input[i] != d)
            result[i] = input[i];
 
        // Else store the string formed
        else {
            result[i] = '\0';
            input = input + i + 1;
            return result;
        }
    }
 
    // Case when loop ends
    result[i] = '\0';
    input = NULL;
 
    // Return the resultant pointer
    // to the string
    return result;
}





int main()
{

	Activity***** calendar;
	calendar = new Activity ****[12];
	Activity** singleActivity = new Activity* [5];
	int acivityLength = sizeof(singleActivity);
	for( int ai = 0; ai < acivityLength; ai++ ) {	
		Activity* row = new Activity[acivityLength-1];	
		singleActivity[ai] = row;	
	}


	for (int i = 0; i < 12; i++)
	{
		if (i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11)
		{
			calendar[i] = new Activity ***[31];
		}
		else if (i == 3 || i == 5 || i == 8 || i == 10)
		{
			calendar[i] = new Activity ***[30];
		}
		else
		{
			calendar[i] = new Activity ***[28];
		}

		for (int j = 0; j < sizeof(calendar[i]); j++)
		{
			calendar[i][j] = new Activity **[24];

			for (int k = 0; k < 24; k++)
			{
				calendar[i][j][k] = new Activity * [1];
				calendar[i][j][k][0] = new Activity;				
			}
		}
	}


char delim = ',';
				 char *arrayp;    
			    arrayp = &line[0];
				char *ptr = mystrtok(arrayp, delim);
				int lineIndex = 1;
				 while(ptr != NULL)
				 {
					if(lineIndex == 1) 
					{
						char daybuff[2];
						memcpy(daybuff, &ptr[0], 2);
						daybuff[2] = '\0';
						day = stoi(daybuff);
						char monthbuff[2];
						memcpy(monthbuff, &ptr[1],2);
						if(/*monthbuff != null &&*/ monthbuff[0] == '/')
						{									
							month = stoi(new char(monthbuff[1]));
						}
						else
						{
							monthbuff[2] = '\0';
							month = stoi(monthbuff);
						}						
					  //day =stoi(mySubStr(ptr,1,2));
					}
					else if(lineIndex == 2)
					{
						char startbuff[2];
						memcpy(startbuff, &ptr[0],2);
						startbuff[2] = '\0';
						stime = stoi(startbuff);
					}
					else if(lineIndex == 3)
					{
						char endbuff[2];
						memcpy(endbuff, &ptr[0],2);
						endbuff[2] = '\0';
						etime = stoi(endbuff);
					}
					else if(lineIndex == 4)
					{
						/*char userIdbuff[10];
						memcpy(userIdbuff, &ptr[0],10);
						userIdbuff[10] = '\0';
						userid = userIdbuff; */
						userid = &ptr[0];			
					}
					else if(lineIndex == 5)
					{
						/*char actIdbuff[150];
						memcpy(actIdbuff, &ptr[0],150);
						actIdbuff[150] = '\0';
						useractivity = actIdbuff; */
						useractivity = &ptr[0];			 											
					}
					else if(lineIndex == 6)
					{
						titleStr = &ptr[0];
					}
					else if(lineIndex == 7)
					{
						char prbuff[6];
						memcpy(prbuff, &ptr[0],6);
						prbuff[6] = '\0';
						priority = atof(prbuff);						
					}

				 	//printf("'%s'\n", ptr);
				 	ptr = mystrtok(NULL, delim);
					 lineIndex ++;
				 }
				// day = parseDay(line);	
				int duration = etime - stime;	

	*(calendar[month - 1][day - 1][stime][0]).title = &titleStr ;



1
2
3
*(calendar[month - 1][day - 1][stime][0]).title = &titleStr ;
I am getting this error while assigning the value


Above line is the problem area. I don't know to assign value to title or fifth dimensional object property ?

1
2
3
 error: request for member 'title' in '*(*((*((*(calendar + (((sizetype)(((long long unsigned int)month) * 8)) + 18446744073709551608))) + (((sizetype)(((long long unsigned int)day) * 8)) + 18446744073709551608))) + ((sizetype)(((long long unsigned int)stime) * 8))))', which is of pointer type 'Activity*' (maybe you meant to use '->' ?)
  901 |                                 *(calendar[month - 1][day - 1][stime][0]).title = &titleStr ;
https://wiki.c2.com/?ThreeStarProgrammer
There are a couple of things you need to do.
1. Learn how to use C++
- ditch all the manual string handling, and use std::string
- ditch all the manual array allocation and use std::vector

2. Learn how to split the code up into functions that perform specific testable steps.
- ie, not a massively bloated 100's of lines long main()

> please see my code and suggest me.
Those would be my suggestions.
You have a pointer to Activity, hence:

calendar[month - 1][day - 1][stime][0]->title = &titleStr ;

It's kind of insane to use 5 dimensions. If you want this consider using std::map. See:

http://www.cplusplus.com/reference/map/map/?kw=map
Agree, more than 3 dimensions is very difficult to manage. You can sure do it, but if you can find a better way, you should.
interestingly, a 64 bit integer can hold time to a great resolution.
YYYYMMDDHHMMSS for example will fit in a 64 bit int unsigned which has 19 digits, the timestamp I gave you only needs 14 digits. You can even add 2-3 digits of fractional seconds if you need to do so, but a typical day planner would actually stop on the min fields.
Then you can do a single operation lookup in the aforementioned map idea, but that is one of many ways to think about it without all the dimensions.
thank you c++ experts.

I am agree with you guys but this is my assignment and my instructor want me to do it in pointer form
Activity*****

@maifs. You have a misconception re sizeof(). This does NOT provide the amount of memory allocated but the number of bytes used by the underlying data type. So

 
int acivityLength = sizeof(singleActivity);


will set acivityLength to either 4 or 8 (32 bit or 64 bit compile) for the number of bytes used for a pointer - irrespective of the number of bytes allocated.

L104 - what is line??

For lines 64-99, consider something like (NOT tried):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	constexpr int days[12] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	auto calendar {new Activity**** [12]};

	for (int i = 0; i < 12; ++i) {
		const int& dnum {days[i]};

		calendar[i] = new Activity*** [dnum];

		for (int j = 0; j < dnum; ++j) {
			calendar[i][j] = new Activity** [24];

			for (int k = 0; k < 24; ++k) {
				calendar[i][j][k] = new Activity* [1];
				calendar[i][j][k][0] = new Activity;
			}
		}
	}

	auto singleActivity {new Activity* [5]};

	for (int ai = 0; ai < 5; ++ai)
		singleActivity[ai] = new Activity[5 - 1];


What about a leap year?
Last edited on
Yes i agree about sizeof. this is my mistake. i didn't read it properly

but we can consider int acivityLength = 5;


the code sent by you is the solution or a suggesting way ?


The way i am looking for is to assign title

*(calendar[month - 1][day - 1][stime][0]).title = &titleStr .

Can you explain a bit more about your suggested code ? As I am presuming that you re-write the line no 64 - 99 in my above code in a better way.

Don't worry about leap year even if we can handle it quickly then we can ignore it for the time being.
Last edited on
As your posted code doesn't compile I can't test it. What I posted is a possible suggestion as replacement code without being tried/tested. It's just based upon the original code re-coded to remove sizeof() and to have a more logical way of allocating memory to the various dimensions.

If you post a complete program that compiles, I'll look at it further (my tomorrow).

PS. This compiles:

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
int main() {
	constexpr int days[12] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	auto calendar {new Activity**** [12]};

	for (int i = 0; i < 12; ++i) {
		const int& dnum {days[i]};

		calendar[i] = new Activity*** [dnum];

		for (int j = 0; j < dnum; ++j) {
			calendar[i][j] = new Activity** [24];

			for (int k = 0; k < 24; ++k) {
				calendar[i][j][k] = new Activity* [1];
				calendar[i][j][k][0] = new Activity;
			}
		}
	}

	auto singleActivity {new Activity* [5]};

	for (int ai = 0; ai < 5; ++ai)
		singleActivity[ai] = new Activity[5 - 1];

	int month = 10, day = 5, stime = 8;

	char titleStr[] {"hello"};

	calendar[month - 1][day - 1][stime][0]->title = titleStr;
}

Last edited on
OK , this is code which you can test and modify and run.



Create a txt file (name as temp1.dat) and paste these lines in it

28/12,1,3,user36,act1994,romania pular,0.94
5/4,0,8,user0,act1995,middle age human,0.49
18/7,2,12,user86,act1996,result oriented,0.57
9/2,12,14,user84,act1997,best student,0.97
30/10,5,6,user40,act1998,archer the power,0.12



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
#include<iostream>
#include<fstream>
#include <cstring>
#include <string>
using namespace std;
struct Activity
{
	char* title;
	char* userid;
	int duration;
	float priority;
};

char* mystrtok(char* s, char d)
{
    // Stores the state of string
    static char* input = NULL;
 
    // Initialize the input string
    if (s != NULL)
        input = s;
 
    // Case for final token
    if (input == NULL)
        return NULL;
 
    // Stores the extracted string
    char* result = new char[strlen(input) + 1];
    int i = 0;
 
    // Start extracting string and
    // store it in array
    for (; input[i] != '\0'; i++) {
 
        // If delimiter is not reached
        // then add the current character
        // to result[i]
        if (input[i] != d)
            result[i] = input[i];
 
        // Else store the string formed
        else {
            result[i] = '\0';
            input = input + i + 1;
            return result;
        }
    }
 
    // Case when loop ends
    result[i] = '\0';
    input = NULL;
 
    // Return the resultant pointer
    // to the string
    return result;
}


int main()
{

	Activity***** calendar;
	calendar = new Activity ****[12];
	Activity** singleActivity = new Activity* [5];
	int acivityLength = 5;
	for( int ai = 0; ai < acivityLength; ai++ ) {	
		Activity* row = new Activity[acivityLength-1];	
		singleActivity[ai] = row;	
	}


	int totalmonthdays = 0;
	for (int i = 0; i < 12; i++)
	{
		if (i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11)
		{
			calendar[i] = new Activity ***[31];
			totalmonthdays = 31;
		}
		else if (i == 3 || i == 5 || i == 8 || i == 10)
		{
			calendar[i] = new Activity ***[30];
			totalmonthdays = 30;
		}
		else
		{
			calendar[i] = new Activity ***[28];
			totalmonthdays = 28;
		}

		for (int j = 0; j < totalmonthdays; j++)
		{
			calendar[i][j] = new Activity **[24];

			for (int k = 0; k < 24; k++)
			{
				calendar[i][j][k] = new Activity * [1];
				calendar[i][j][k][0] = new Activity;				
			}
		}
	}

	int c = 0, i = 0, linecount = 0, x = 0, coma_count = 0, id_count = 0, title_count = 0;
	int useridx, activityaycount = 0;
	int activitycount = 0;
	int day = 0, month = 0, stime = 0, etime = 0;
	double priority = 0.0;
	int floatcount = 0;
	char* userid;
	char* useractivity;
	char* titleStr;
	ifstream ifile;    
    ofstream out_stream;     
	string line;  
	ifile.open("temp1.dat");
     if (ifile.fail())
        {
            cout << "Input file opening failed";
            exit(1);
        }
	else if (ifile.is_open())
	{
		while (getline (ifile,line))
		{            
			
			if (line != "\0")
			{				
				coma_count = 0;		
				char delim = ',';
				 char *arrayp;    
			    arrayp = &line[0];
				char *ptr = mystrtok(arrayp, delim);
				int lineIndex = 1;
				 while(ptr != NULL)
				 {
					if(lineIndex == 1) 
					{
						char daybuff[2];
						memcpy(daybuff, &ptr[0], 2);
						daybuff[2] = '\0';
						day = stoi(daybuff);
						char monthbuff[2];
						memcpy(monthbuff, &ptr[1],2);
						if( monthbuff[0] == '/')
						{									
							month = stoi(new char(monthbuff[1]));
						}
						else
						{
							monthbuff[2] = '\0';
							month = stoi(monthbuff);
						}					
					
					}
					else if(lineIndex == 2)
					{
						char startbuff[2];
						memcpy(startbuff, &ptr[0],2);
						startbuff[2] = '\0';
						stime = stoi(startbuff);
					}
					else if(lineIndex == 3)
					{
						char endbuff[2];
						memcpy(endbuff, &ptr[0],2);
						endbuff[2] = '\0';
						etime = stoi(endbuff);
					}
					else if(lineIndex == 4)
					{
						
						userid = &ptr[0];			
					}
					else if(lineIndex == 5)
					{
						useractivity = &ptr[0];			 											
					}
					else if(lineIndex == 6)
					{
						titleStr = &ptr[0];
					}
					else if(lineIndex == 7)
					{
						char prbuff[6];
						memcpy(prbuff, &ptr[0],6);
						prbuff[6] = '\0';
						priority = atof(prbuff);						
					}

				 	//printf("'%s'\n", ptr);
				 	ptr = mystrtok(NULL, delim);
					 lineIndex ++;
				 }
				// day = parseDay(line);	
				int duration = etime - stime;				
				
				calendar[month - 1][day - 1][stime][0][0].priority = priority;
				calendar[month - 1][day - 1][stime][0][0].duration = duration;					
				calendar[month - 1][day - 1][stime][0][0].userid = userid;
				calendar[month - 1][day - 1][stime][0][0].title = titleStr;

				cout << calendar[month - 1][day - 1][stime][0][0].title << endl;
			
		
				
				coma_count = 0, c = 0, day = 0, month = 0, activityaycount = 0, activitycount = 0, priority = 0.0, floatcount = 0, duration = 0;
				x++;

			}
			else
			{
				break;

			}


		}	
		ifile.close();
		system("pause");
	}
}



Last edited on
You've still got a bloated main.

> calendar[i][j][k] = new Activity * [1];
This is a pointless dimension.

1
2
3
4
char daybuff[2];
memcpy(daybuff, &ptr[0], 2);
daybuff[2] = '\0';
day = stoi(daybuff);

Every single one of your attempts at this is a buffer overrun.

> if (line != "\0")
This isn't the same thing as saying it's a blank line.
ok, ignoring the stuff out there because I can't make sense of it, this is probably what you are asking.

activity ***** a5d;
a5d = new activity****[12]; //months

for(int i = 0; i < 12; i++)
a5d[i] = new activity***[31]; //days

for(int m = 0; m < 12; m++)
for(int d = 0; d < 31; d++)
a5d[m][d] = new activity**[24]; //hours...

like that ^^^
not sure what the other 2 dimensions are; one looks like activities as an array for a given timestamp, and the other looks like a pointless extra dimension so that you access it all from a pointless pointer (?). But each time you allocate your loop grows a leg, so the next one will have 3 for loops, similar to the double loop above but now each day also gets a chunk.
alternately since these are fixed dimensions you can unroll it with casting from a 1-d allocation back to 5d with smoke and mirrors, so you can new/delete in a single go. that requires bogus typedefs, easy to do though...
is this what you are asking?
once you have the dimensions, its just
thing[month][day][time][activitynumber]->
or
*thing[... as above
whatever notation you like.
Thank you guys for such marvellous sharing.

I like the way you are all telling about.

Seeplus line of code is helping me alot.
The jonnin and saleem c , i tried to get your point, definitely it is remarkable but couldn' fully get your point.

Can you metion some code if we follow the way of seeplus or your way around.

Appreciated

These are the explaination of 5 D.

As shown in the picture, the calendar is an array of size 12 (one entry
per month), where each entry is a pointer to a dynamic array whose size
is equal to the number of days in that month in the year 2022.For
example, January’s array should have 31 entries, Feb’s array should
have 28 etc.
• Each entry in a month’s array contains a pointer pointing to an array of
the days in that month. All these day arrays are of size 24 as there are
24 hours in a day. If there’s no activity in a particular day – for
example, if there is no activity on January 5, then the pointer of that
day’s array is set to nullptr.
• Each entry in the 24-size ‘days array’ contains a pointer to an array of
pointers to activities in that hour of that day. If an hour has no activity
this pointer is nullptr. Of course, the sizes of this activities’ arrays will
be different, depending on the number of activities in that particular
hour of that day.
• Each single activity (a box allocated on the heap) contains the
information as described in the description of the activity struct above.
Note that in each activity, the title and userid are dynamically allocated
as well.
So the calendar as a whole will be pointed to by a pointer of the type
activity ***** calendar; to be allocated dynamically according these
specifications.
3. How will the data
Last edited on
That is oddly worded! hmm. let me see if I can make a little example of what I was saying.
Last edited on
I hope this is right... this is starting to get beyond my ability to visualize it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct Activity
{
	char* title;
	char* userid;
	int duration;
	float priority;
};

int main()
{
  	Activity** base = new Activity*[12*31*24*100]{}; //100 activities per day? no value was given. 
	//ok, now we have a nice fat container of activity pointers.  
	//we need to reshape them. 
	typedef Activity* reshape[31][24][100];
	
	auto forizzle = (reshape*)base;
	cout << typeid(forizzle).name() << endl;
	forizzle[1][2][3][4] = new Activity;
	
}


this allocates more than you need, eg more days for some months. but as long as it hits the nullptr on those, I think its OK since you will write code to complain about the wrong month values anyway, and the user need never know that you have extras. BUT... the professor will know. You may not want to do this, but it saves all that idiotic looping and allocating in bits and pieces -- of course, that may be the point of the exercise...!
Last edited on
Thank you Jonnin and Seeplus. I now try your solutions
As a first possible refactor, consider:

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
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <cctype>
#include <iomanip>

struct Activity {
	char* title {};
	char* userid {};
	int duration {};
	double priority {};
};

const char* mystrtok(const char* s, char d) {
	// Stores the state of string
	static const char* input {};

	// Initialize the input string
	if (s != nullptr)
		input = s;

	// Case for final token
	if (input == nullptr)
		return nullptr;

	// Stores the extracted string
	auto result {new char[strlen(input) + 1]};
	size_t i {};

	// Start extracting string and
	// store it in array
	for (; input[i]; ++i) {
		// If delimiter is not reached
		// then add the current character
		// to result[i]
		if (input[i] != d)
			result[i] = input[i];
		// Else store the string formed
		else {
			result[i] = '\0';
			input += i + 1;
			return result;
		}
	}

	// Case when loop ends
	result[i] = '\0';
	input = nullptr;

	return result;
}

int main() {
	std::ifstream ifile("temp1.dat");

	if (!ifile)
		return (std::cout << "Input file opening failed"), 1;

	constexpr int days[12] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	// Activity [12][days][24][1][1]
	auto calendar {new Activity**** [12]};

	for (int i = 0; i < 12; ++i) {
		const int& dnum {days[i]};

		calendar[i] = new Activity*** [dnum];

		for (int j = 0; j < dnum; ++j) {
			calendar[i][j] = new Activity** [24];

			for (int k = 0; k < 24; ++k) {
				calendar[i][j][k] = new Activity* [1];
				calendar[i][j][k][0] = new Activity;
			}
		}
	}

	/* ????
	auto singleActivity {new Activity * [5]};

	for (int ai = 0; ai < 5; ++ai)
		singleActivity[ai] = new Activity[5 - 1];
	*/

	constexpr char delim {','};

	for (std::string line; std::getline(ifile, line); ) {
		size_t elemIndex {1};
		unsigned day {}, month {}, stime {}, etime {};
		double priority {};

		for (auto ptr {mystrtok(line.c_str(), delim)}; ptr; ptr = mystrtok(nullptr, delim), ++elemIndex) {
			switch (elemIndex) {
				case 1:
					{
						char daybuff[3] {*ptr++};

						if (isdigit(*ptr))
							daybuff[1] = *ptr++;

						day = std::stoul(daybuff);

						char monthbuff[3] {*++ptr};

						if (isdigit(*ptr))
							monthbuff[1] = *++ptr;

						month = std::stoul(monthbuff);
					}
					break;

				case 2:
					stime = std::stoul(ptr);
					break;

				case 3:
					etime = std::stoul(ptr);
					break;

				case 4:
					calendar[month - 1][day - 1][stime][0][0].userid = new char[strlen(ptr) + 1];
					strcpy(calendar[month - 1][day - 1][stime][0][0].userid, ptr);
					break;

				case 5:
					break;

				case 6:
					calendar[month - 1][day - 1][stime][0][0].title = new char[strlen(ptr) + 1];
					strcpy(calendar[month - 1][day - 1][stime][0][0].title, ptr);
					break;

				case 7:
					priority = std::stod(ptr);
					break;
			}
		}

		calendar[month - 1][day - 1][stime][0][0].priority = priority;
		calendar[month - 1][day - 1][stime][0][0].duration = etime - stime;

		std::cout << std::setw(2) << day << " " << std::setw(2) << month << "  ";
		std::cout << std::setw(2) << stime << " " << std::setw(2) << etime << "  ";
		std::cout << std::setw(8) << std::left << calendar[month - 1][day - 1][stime][0][0].userid << " ";
		std::cout << std::setw(20) << calendar[month - 1][day - 1][stime][0][0].title << "  ";
		std::cout << std::right << calendar[month - 1][day - 1][stime][0][0].priority << '\n';
	}
}



28 12   1  3  user36   romania pular         0.94
 5  4   0  8  user0    middle age human      0.49
18  7   2 12  user86   result oriented       0.57
 9  2  12 14  user84   best student          0.97
30 10   5  6  user40   archer the power      0.12


Note that memory is allocated in multiple places using new but delete is never used, Before the program exits, all allocated memory should be delete'd!

Also, there is minimal input validation - so any invalid input can cause errors!
Last edited on
thank you very much seeplus
As a second refactor. mystrtok() isn't needed as strtok() can be used with .data(). Also memory is now deleted before the program ends, and other various 'tidy-ups':

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
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <cctype>
#include <iomanip>

struct Activity {
	char* title {};
	char* userid {};
	int duration {};
	double priority {};
};

constexpr size_t d1st {12}, d3rd {24}, d4th {1}, d5th {1};
constexpr unsigned days[d1st] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
constexpr const char* delim {","};

// Activity [12][days][24][1][1]
auto create() {
	auto calendar {new Activity ****[d1st]};

	for (unsigned i {}; i < d1st; ++i) {
		const auto& dnum {days[i]};

		calendar[i] = new Activity ***[dnum];

		for (unsigned j {}; j < dnum; ++j) {
			calendar[i][j] = new Activity **[d3rd];

			for (unsigned k {}; k < d3rd; ++k) {
				calendar[i][j][k] = new Activity *[d4th];

				for (unsigned l {}; l < d4th; ++l)
					calendar[i][j][k][l] = new Activity[d5th];
			}
		}
	}

	return calendar;
}

void remove(Activity*****& calendar) {
	for (unsigned i {}; i < d1st; ++i) {
		for (unsigned j {}; j < days[i]; ++j) {
			for (unsigned k {}; k < d3rd; ++k) {
				for (unsigned l {}; l < d4th; ++l)
					delete[] calendar[i][j][k][l];

				delete[] calendar[i][j][k];
			}

			delete[] calendar[i][j];
		}

		delete[] calendar[i];
	}

	delete calendar;
	calendar = nullptr;
}

int main() {
	std::ifstream ifile("temp1.dat");

	if (!ifile)
		return (std::cout << "Input file opening failed"), 1;

	auto calendar {create()};

	for (std::string line; std::getline(ifile, line); ) {
		size_t elemIndex {1};
		unsigned day {}, month {}, stime {}, etime {};
		double priority {};

		for (auto ptr {strtok(line.data(), delim)}; ptr; ptr = strtok(nullptr, delim), ++elemIndex) {
			switch (elemIndex) {
				case 1:
					{
						char daybuff[3] {*ptr++};

						if (isdigit(*ptr))
							daybuff[1] = *ptr++;

						day = std::stoul(daybuff);

						char monthbuff[3] {*++ptr};

						if (isdigit(*ptr))
							monthbuff[1] = *++ptr;

						month = std::stoul(monthbuff);
					}
					break;

				case 2:
					stime = std::stoul(ptr);
					break;

				case 3:
					etime = std::stoul(ptr);
					break;

				case 4:
					calendar[month - 1][day - 1][stime][0][0].userid = new char[strlen(ptr) + 1];
					strcpy(calendar[month - 1][day - 1][stime][0][0].userid, ptr);
					break;

				case 5:
					break;

				case 6:
					calendar[month - 1][day - 1][stime][0][0].title = new char[strlen(ptr) + 1];
					strcpy(calendar[month - 1][day - 1][stime][0][0].title, ptr);
					break;

				case 7:
					priority = std::stod(ptr);
					break;
			}
		}

		calendar[month - 1][day - 1][stime][0][0].priority = priority;
		calendar[month - 1][day - 1][stime][0][0].duration = etime - stime;

		std::cout << std::setw(2) << day << " " << std::setw(2) << month << "  ";
		std::cout << std::setw(2) << stime << " " << std::setw(2) << etime << "  ";
		std::cout << std::setw(8) << std::left << calendar[month - 1][day - 1][stime][0][0].userid << " ";
		std::cout << std::setw(20) << calendar[month - 1][day - 1][stime][0][0].title << "  ";
		std::cout << std::right << calendar[month - 1][day - 1][stime][0][0].priority << '\n';
	}

	remove(calendar);
}

Last edited on
Topic archived. No new replies allowed.