//flex table opened by JP

Click to See Complete Forum and Search --> : Another C++ question =)


nothing
12-05-2003, 07:07 PM
I'm trying to write a little program and one of its taks is to keep track of the weeks of the month ( Week 1, Week 2, Week 3, and Week 4 ). For example...if the user opens the program on the 13th, that Would be week number 2. If the user opens the program on the 27th, that would be week number 4, and so on. How can I know how many days this month will have using the computer's calendar? Thank you much.

nothing
12-05-2003, 08:25 PM
Nevermind, I don't need to do that :eek:

nothing
12-05-2003, 11:53 PM
Will this work?


if( ( day_as_int > 1 ) && ( day_as_int < 7 ) )
cout << "Week 1\n";

else if( ( day_as_int > 7 ) && ( day_as_int < 14 ) )
cout << "Week 2\n";

else if( ( day_as_int > 14 ) && ( day_as_int < 21 ) )
cout << "Week 3\n";

else
cout << "Week 4\n";


day_as_int is a number in the range 1 - 31. Thank you.

ScaryBinary
12-06-2003, 12:08 AM
...you may want ">=1" and "<=7", etc., but other than that it looks like it should work.

You could shorten it a bit, too, with something like

if (day_as_int <= 7)
{
cout << "Week 1\n";
}
else if (day_as_int <=14)
{
cout << "Week 2\n";
}

etc...

Is that really what you want though? I mean, if the 1st is a Wednesday, do you want week 1 to be the 1st through the 4th, with Week 2 starting on the next Sunday, or Week 1 being the first 7 days, Week 2 the next 7 days (which is what your code will produce)? Just curious. :)

nothing
12-06-2003, 01:04 AM
Let me try to explain...

A friend of mine works for a siding company and there is this guy who works for him. My friend( the boss) knows that I am learning C++ and he decided to "challenge" me; He asked me to write a program that will keep track of the hours his helper works. I have everything in my head so I will just explain what I want to do in the file where the hours will be written to. I want the file to look like this:


Week 1
Day1
Day2
Day3
Day4
Day5
Day6
Day7

Week 2
Day1
Day2
Day3
Day4
Day5
Day6
Day7

Week 3
Day1
Day2
Day3
Day4
Day5
Day6
Day7

Week 4
Day1
Day2
Day3
Day4
Day5
Day6
Day7


I don't know when I'll finish writing the program and I also don't know when my friend will run it for the first time, so I can't assume that the first day he inputs the hours will be on week 1 so that's why I need to find a way to check in what week that day is. Did you understand or I messed it all up? :p

nothing
12-06-2003, 12:57 PM
fishybawb, where are you? You've got a question to asnwer now :p

fishybawb
12-06-2003, 02:18 PM
Here I am :D

I'm not sure what you're asking really... I take it that you sorted the problem in your first question out, and now want some help with writing to files? Do you just want the code to create a file containing this?


Week 1
Day1
Day2
Day3
Day4
Day5
Day6
Day7

... etc


:t

nothing
12-07-2003, 12:28 PM
Hi. Sorry if I did not make myself clear and sorry for taking a long time to reply to your post( I was out the whole day yesterday :p )

Here is my plan...
My program will be run at the end of everyday. My first problem is that I don't know when it will be used for the first time and I can't just tell the person that I am writing the program to to "run the program for the first time on a Sunday only!". So I want to do something like this everytime the program is being used:


if the file hours.txt does not exist
get today's date and figure out in what week (1-4 ( or 1-5? ))
of the month that day is in

write at the beginning of the file what week it is
For example: today is Sunday, the 7th, so the program
would write this on the file....

Week 2
Sunday, December 07

else (the file already exists and has stuff on it )
the program would write
Monday, December 08 right bellow Sunday, December 07
and so on, until it is Sunday again. That's when it will have
to figure out again in what week that day is in and write it
at the top of the file, not deleting the previous information


This is harder than I thought :eek:

Overall, the file has alway to be in rotation, if you know what I mean. It will keep track of the four weeks of the month and when a new month starts, the program starts overwriting that month with the new month. If the program was used for the first time on December 1st and used everyday the whole month, this is how the file would look like:


Week 1
Monday December 01
Tuesday December 02
Wednesday December 03
Thursday December 04
Friday December 05
Saturday December 06

Week 2
Sunday December 07
Monday December 08
Tuesday December 09
Wednesday December 10
Thursday December 11
Friday December 12
Saturday December 13

Week 3
Sunday December 14
Monday December 15
Tuesday December 16
Wednesday December 17
Thursday December 18
Friday December 19
Saturday December 20

Week 4
Sunday December 21
Monday December 22
Tuesday December 23
Wednesday December 24
Thursday December 25
Friday December 26
Saturday December 27

You see the problem? This month has 31 days and the 4 weeks are already filled up. What should I do here?
Anyway. Let's pretend this month has only 27 days lol. When the user runs the program again, let's say January
9 (he was on vacation and couldn't run the program on the 1st) this is what would happen with the file

Week 1
Friday January 09

Week 1
Monday December 01
Tuesday December 02
Wednesday December 03
Thursday December 04
Friday December 05
Saturday December 06

Week 2
Sunday December 07
Monday December 08
Tuesday December 09
Wednesday December 10
Thursday December 11
Friday December 12
Saturday December 13

Week 3
Sunday December 14
Monday December 15
Tuesday December 16
Wednesday December 17
Thursday December 18
Friday December 19
Saturday December 20

Week 4
Sunday December 21
Monday December 22
Tuesday December 23
Wednesday December 24
Thursday December 25
Friday December 26
Saturday December 27
------------------------------------------------------------------
This is how the file would look like at the end of the month

Week 1
Friday January 09
Saturday January 10

Week 2
Sunday January 11
Monday January 12
Tuesday January 13
Wednesday January 14
Thursday January 15
Friday January 16
Saturday January 17

Week 3
Sunday January 18
Monday January 19
Tuesday January 20
Wednesday January 21
Thursday January 22
Friday January 23
Saturday January 24

Week 4
Sunday January 25
Monday January 26
Tuesday January 27
Wednesday January 28
Thursday January 29
Friday January 30
Saturday January 31

Week 1
Monday December 01
Tuesday December 02
Wednesday December 03
Thursday December 04
Friday December 05
Saturday December 06

Week 2
Sunday December 07
Monday December 08
Tuesday December 09
Wednesday December 10
Thursday December 11
Friday December 12
Saturday December 13

Week 3
Sunday December 14
Monday December 15
Tuesday December 16
Wednesday December 17
Thursday December 18
Friday December 19
Saturday December 20

Week 4
Sunday December 21
Monday December 22
Tuesday December 23
Wednesday December 24
Thursday December 25
Friday December 26
Saturday December 27
---------------------------------------------------
This is how the file would look like on February 01

Week 1
Sunday February 01
and so on...

Week 1
Friday January 09
Saturday January 10

Week 2
Sunday January 11
Monday January 12
Tuesday January 13
Wednesday January 14
Thursday January 15
Friday January 16
Saturday January 17

Week 3
Sunday January 18
Monday January 19
Tuesday January 20
Wednesday January 21
Thursday January 22
Friday January 23
Saturday January 24

Week 4
Sunday January 25
Monday January 26
Tuesday January 27
Wednesday January 28
Thursday January 29
Friday January 30
Saturday January 31

Did you notice the rotation?

If you think this is too complicated and will give you a headache, don't resitate to tell me ok? I don't want you to write the code for me, I just need ideas you know. Thank you very much :D

P.S: I hope I explained this good enough. My head really hurts right now :rolleyes:

Paco103
12-07-2003, 12:56 PM
Originally posted by nothing
. . . I can't just tell the person that I am writing the program to to "run the program for the first time on a Sunday only!". . . . .

Shhhh - keep it down. . . you're going to give Billy ideas for his new "We own you" licensing terms!

Anyway, that output starts looking rather nasty. Obviously you'll need atleast 5 weeks, since most months are 4 1/2. I'm not sure about C++ libraries, but for instance I believe Java has the ability to tell you the week number. If C++ doesn't have that ability, you could always do the math yourself. i.e. The 1st will ALWAYS be in week 1, the 8th will ALWAYS be in week 2, the 15th will ALWAYS be in week 3, and so on. So say it is opened on the 20th, which this month is a saturday. You can get from the library easily. 20 - 15 (your closest key) is 5. Since Saturday-5 days does not land on or pass saturday again(because saturday is the last day of a week, or use Sunday if your weeks are going to start on monday)), you know it is in the same week as the 15th, which you know is in week 3 as well. I would probably enumerate the days of the week.

Sunday = 0;
Monday = 1;
. . . . .
Saturday = 7;
(or whatever works for you).

As far as your file output, might I suggest something like getting the month and year from the calandar, and outputing to say month+year (as strings).txt or whatever extension you want? Unless the output is strictly for the program reference. I just think that would be more user friendly output, looking for something like.

December2003.txt, rather than trying to find it in one huge output file.

{edit}You also won't be deleting his last months that way if you're just resetting the output file, since he may want to look back at the previous month or sooner {/edit}

As for the algorithm idea, it was just off the top of my head, and it's early, so if it sounds too complicated or impractical, don't worry about it, but if it sounds like something you'd like to use feel free. I can also try to translate it from an abstract to code later if that would help. Good Luck!

ScaryBinary
12-07-2003, 01:01 PM
Hmm....

Here are some things you may or may not want to think about...

If you store the entire date each time you log hours, you can always query the data and compute the weeks later. I don't quite understand why you're so concerned with figuring out what week you're in, but if you have the date you can figure out on the fly what day of the week it is, or what week in the month it is, for reporting purposes. That way you can start logging hours any old day, and then when you report your data just format it a week at a time. Your data file would just be a list of dates and hours logged, then if you wanted all the hours for a particular week you'd compute a range of days in that week then query your data for those days.

You could just append data to the data file (i.e., you wouldn't have to overwrite it all the time). Then your friend would have a complete history of his helper's hours, not just the last month's.

(I have more of a database background than a C++ background, so maybe this doesn't make sense.)

Just some thoughts. Feel free to ignore me. :D

fishybawb
12-07-2003, 03:47 PM
I haven't got much to add to what the guys have already said, but I'd definitely store the data as seperate files for each month. As Paco said, you can pretty much guarantee that the guy's going to turn around one day and say "oh yeah, I need to check something from 2 months ago" - as you're only working with text files there isn't going to be a storage space problem, so it makes sense to keep the data instead of going for the overwriting option. Plus it's easier to implement, which is always nice :D

nothing
12-07-2003, 08:51 PM
Paco103, I like your ideas and of course I would love to see some code snippet, especially for figuring out in what week of the month a certain day is.

ScaryBinary, I am concerned with figuring out what week I'm in because the guy pays his helper every Monday so the days worked must be separated into weeks.

fishybawb, as always, thank you for the help.

Thank you all

:D

nothing
12-08-2003, 07:41 PM
#include <iostream>
using std::cout;
using std::endl;

#include <string>
using std::string;

#include <ctime>

int main()
{
time_t dateTime;
time(&dateTime);
string date = ctime(&dateTime);

cout << date << endl;

return 0;
}


This will display something like this

Mon Dec 08 18:40:20 2003

Is there a way to make it display "Monday" instead of "Mon" ? Thanks!

ScaryBinary
12-08-2003, 08:09 PM
Here's a sample I stole from Microsoft and edited a bit.

/* TIMES.C illustrates various time and date functions including:
* time _ftime ctime asctime
* localtime gmtime mktime _tzset
* _strtime _strdate strftime
*
* Also the global variable:
* _tzname
*/

#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>

void main()
{
char tmpbuf[128];
time_t ltime;
struct tm *today;

/* Set time zone from TZ environment variable. If TZ is not set,
* the operating system is queried to obtain the default value
* for the variable.
*/
_tzset();


/* Get UNIX-style time and display as number and string. */
time( &ltime );

/* Use time structure to build a customized time string. */
today = localtime( &ltime );

/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128,
"Today is %A, day %d of %B in the year %Y.\n", today );
printf( tmpbuf );
}


That produced
Today is Monday, day 08 of December in the year 2003.

I think the ctime function is dependent upon your localized time settings, so it may be different from machine to machine. The strftime function lets you specify the format.

Check out the MSDN help for strftime for info on the formatting ("the %A, %B, etc.).

Hmmm....just realized I assumed you were using Visual C++. I don't know if the strftime function is a Microsoft thing only or not....

fishybawb
12-08-2003, 09:05 PM
Referring back to that date/time function stuff:


#include <iostream>
#include <string>
using namespace std;
#include <ctime>

int main()
{
string days[7] = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday",
"Friday", "Saturday"};

time_t dateTime;
time(&dateTime);

tm getDay = *(localtime(&dateTime));

string day = days[getDay.tm_wday];
cout << day << endl;

return 0;
}


That will "cout" the current day as a full string. It all goes back into pointers with the localtime() function, but basically the localtime function converts the long int variable dateTime (the unprocessed date in "time elapsed since 1900 and blah" form) to a struct of type tm. I stole the layout of the tm struct from my MSDN reference - each of the variables are ints:


tm_sec
Seconds after minute (0 – 59).
tm_min
Minutes after hour (0 – 59).
tm_hour
Hours after midnight (0 – 23).
tm_mday
Day of month (1 – 31).
tm_mon
Month (0 – 11; January = 0).
tm_year
Year (current year minus 1900).
tm_wday
Day of week (0 – 6; Sunday = 0).
tm_yday
Day of year (0 – 365; January 1 = 0).
tm_isdst
Daylight saving


So the code uses the tm_wday variable within the structure (number of weekday where Sunday = 0) to access the full string in the "days" array.

Anyway, I hope you get all that :D

nothing
12-09-2003, 12:15 AM
Let me see if I got this right. This is how structure tm looks like right?

typedef struct {
int tm_hour; /* hour (0 - 23) */
int tm_isdst; /* daylight saving time enabled/disabled */
int tm_mday; /* day of month (1 - 31) */
int tm_min; /* minutes (0 - 59) */
int tm_mon; /* month (0 - 11 : 0 = January) */
int tm_sec; /* seconds (0 - 59) */
int tm_wday; /* Day of week (0 - 6 : 0 = Sunday) */
int tm_yday; /* Day of year (0 - 365) */
int tm_year; /* Year less 1900 */
}


When this two lines execute

time_t dateTime;
time(&dateTime);

, something like this:
Mon Dec 08 21:16:41 2003
will be stored in dateTime right?

When this line executes

tm getDay = *(localtime(&dateTime));

, this is how structure tm will look like right?

typedef struct {
int tm_hour = 21; /* hour (0 - 23) */
int tm_isdst; I don't know what this is/* daylight saving time enabled/disabled */
int tm_mday = 08; /* day of month (1 - 31) */
int tm_min = 16; /* minutes (0 - 59) */
int tm_mon = 11; /* month (0 - 11 : 0 = January) */
int tm_sec = 41; /* seconds (0 - 59) */
int tm_wday = 1; /* Day of week (0 - 6 : 0 = Sunday) */
int tm_yday = whatever ; /* Day of year (0 - 365) */
int tm_year = 2003; /* Year less 1900 */
}


I didn't quite understand the * before localtime() ( I know it is a pointer).

When this line executes

string day = days[getDay.tm_wday];

I didn't understand this line really good either :( (sorry).

So, did I ALMOST get it right? :D