Stuck on a program

This is a discussion on Stuck on a program within the C Programming forums, part of the General Programming Boards category; I am writing a program, and I am not sure what I need to do next. I am new to ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    34

    Stuck on a program

    I am writing a program, and I am not sure what I need to do next. I am new to C, but I compiled the program and it didn't come up with any errors, I just don't quite understand what I need to do next. The purpose of the program is to give the day of a date (Sunday, Monday, etc). For example, I want the program to tell me that 10-11-2007 is a Thursday.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	
    	int month, day, year, prevyear, numdays;
    	
    	printf("Please enter a date for month-day-year: ");
    	scanf("%d-%d-%d", &month, &day, &year);
    	
    	prevyear = ((year - 1) * 365 + ((year - 1)/4) - ((year - 1)/100) + ((year - 1)/400)) % 7;
    
    	numdays = 0;
    
    switch(month)
    {
    	case 12:
    		numdays += 30;
    	case 11:
    		numdays += 31;
    	case 10:
    		numdays += 30;
    	case 9:
    		numdays += 31;
    	case 8:
    		numdays += 31;
    	case 7:
    		numdays += 30;
    	case 6:
    		numdays += 31;
    	case 5:
    		numdays += 30;
    	case 4:
    		numdays += 31;
    	case 3:
    		if ((!(year % 4) && (year % 100)) || ! (year % 400))
    			numdays += 29;
    		else numdays += 28;
    	case 2:
    		numdays += 31;
    
    	return(0);
    }
    
    switch(day)
    {
    	case 6:
    		printf("The day was Saturday");
    break;
    	case 5:
    		printf("The day was Friday");
    break;
    	case 4:
    		printf("The day was Thursday");
    break;
    	case 3:
    		printf("The day was Wedensday");
    break;
    	case 2:
    		printf("The day was Tuesday");
    break;
    	case 1:
    		printf("The day was Monday");
    break;
    	case 0:
    		printf("The day was Sunday");
    break;
    }
    	return(0);
    }
    When I compile, it asks for a date, so I enter a date, but obviously it doesn't do anythin else. If anyone could help with this, I would appreciate it. Showing me what I need to do next to get me going in the right direction would be great.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > scanf("&#37;d-%d-%d", &month, &day, &year);
    What did you type in?
    If you typed in 10 10 2007 or 10/10/2007 then it wouldn't work.
    It would need to be 10-10-2007

    > switch(day)
    According to your input, day is 11
    So it's not going to match any of these cases.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And you have a stray
    Code:
    	return(0);
    after case 2:
    which means that you don't get to the code to write the day.

    You also aren't calculating the effect of the day within this year, or the previous year.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    34
    Quote Originally Posted by Salem View Post
    > scanf("%d-%d-%d", &month, &day, &year);
    What did you type in?
    If you typed in 10 10 2007 or 10/10/2007 then it wouldn't work.
    It would need to be 10-10-2007

    > switch(day)
    According to your input, day is 11
    So it's not going to match any of these cases.
    My statement was a bit confusing, looking at it again. What I meant was that it asks me for a date (so I know it works correctly) but it doesn't do anything else. 10-10-2007 is what I entered, but the program does not do anything after asking me for the date. I am pretty sure this will work correctly if I can figure out how to make it calculate the right day because the "% 7" divides the number by 7, so if a remainder of 6 is left, it knows the day is Saturday, or if 5 is less, it knows the day is Friday, etc. I am just confused as to how I apply my formula to give me a day.

    I know I need to use the:

    prevyear = ((year - 1) * 365 + ((year - 1)/4) - ((year - 1)/100) + ((year - 1)/400)) % 7

    but I am not sure how to use it.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you even read my post? It does explain why "it doesn't do anything else". And as you say, your calculation isn't comlete.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  2. somebody help me with this array program
    By hieugene in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2006, 04:53 AM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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