help with a loop

This is a discussion on help with a loop within the C++ Programming forums, part of the General Programming Boards category; here is my problem: the user input a started date for a year, and let said user input integer 6 ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    help with a loop

    here is my problem: the user input a started date for a year, and let said user input integer 6 for friday and the month is Jan and year is 2000. so January 2000 started on Saturday and ends on Monday.

    how do i do a loop so it will calculated the correct start date for any given month follows?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    How would you do it in your head or on paper?

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Quote Originally Posted by Daved View Post
    How would you do it in your head or on paper?
    in my program.

    here is the question.
    user inputs a year. the day which that year start and lastly a certain month from that year.

    here is what i have so far.
    i can get the program to to run and display something like this.
    what year: 2000
    first day for this year: 7 (for sunday)
    month to display in this year: 1(for January)
    output:
    Jan 2000
    S M T W T F S
    _________1 (
    2 3 -----------
    --------------
    --------------
    --------------
    30,31


    and that is where i stuck on for now,
    how do i code a loop that will
    get the proper start day for any given month the user wish to display in that year?

    here is what i start so far
    a function call
    int getStartDay(int dayVal, int yearVal, MonthNum)
    {
    int inputStartDay = dayVal;

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    That's a good start. You still have to figure out how you would do it on paper and turn that into an algorithm.

    Because you've gotten a good start I'll give you a hint. I would add up the number of days in the months before the selected month. For example, if they choose January, there are 0 days before January. If they choose February, there are 31 days before February. I would then divide that number by 7 and look at the remainder. For example, 31/7 = 4 remainder 3. So that means that February starts on the day of the week that is three days after the day that January starts on.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    so

    Jan 2000
    1 2 3 4 5 6 7 (start day)
    S M T W T F S (days in a week)
    x x x x x x x 1


    x 31

    here is what i got

    from Jan
    31%7 = 3
    3 is the start day for Feb 2000. which is Tuesday and i check the calender and it is correct.
    for March 2000.
    31+29+31 =91%7 =0 now what do i do.

    from calender March 2000 started on Wednesday.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> 31+29+31 =91
    Are you sure that's how many days are before March?

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Year 2000
    Jan - 31 day
    Feb - 29 day

    before March = 60 days

    before April = 91 days

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    i am talking to myself now.

    from Jan 31%7 =3, which is the date in the week Feb start
    from Jan & Feb 60%7=4, which is the date in the week Mar start
    from Jan, Feb & Mar 91%7=0, so Apr start on the same date as Jan
    so for this year the correct start day for a certain month is just the remainder from the modulus 7.

    then when i apply this to another year like 2001. it doesn't work. somehow i know i need to add the original start day from jan to the equation to make it work. but how?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You still just have to count the days from January 1 2000. Make sure you account for leap years. Otherwise it should still work.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    what do you mean?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You said when you apply this to another year it doesn't work. You're saying that if the user wants to know what day March 2001 starts on, your code isn't working, right?

    Just count the number of days since January 2000. So for March 2001, that's 366 days in 2000 plus 31 days in January plus 28 days in February. Get the remainder from 7 on that and it will tell you what day March 2001 starts on.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    thanks for the help, Daved.
    i can't believe i took so long to figuring out such a simple concept.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 01:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21