Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    new to programming help needed thank you

    Can anyone tell me why this keeps blowing up? The first part of this post is what it is suppose to do and then I post my source code. It needs to be in C and compile on emacs, with gcc -Wall std=99 Thank you all.

    Write a program weekday.c that prompts the user for a date (entered as month/day) this year, and
    outputs the day of the week the date falls on. Use the fact that this year (2011), January 1st was
    a Saturday and that January, March, May, July, August, October, and December have 31 days,
    April, June, September, and November have 30 days, and February this year had 28 days.
    Note: Do not specify in the program the rst day of each month (e.g., that February 1st will fall
    on a Tuesday, etc.). Instead, use one switch statement to calculate the number of days till the date
    entered, and another switch statement to print the day of the week.
    Here is a sample run:
    (~)$ a.out
    Enter today's date: 03/21
    Happy Monday!
    (~)$ a.out
    Enter today's date: 5/14
    Happy Saturday!
    (~)$ a.out
    Enter today's date: 11/24
    Happy Thursday!

    Here is my source code:

    Code:
    // program to return the day of the week given the date (jan 1st Sat.)
    
     
    
    #include <stdio.h>
    int main()
    {char input[6];
    int i,month=0,day=0;
    printf("Enter today's date:(m/d) ");
    scanf("%s",(char*)&input);   //get the date as characters since the input is numbers and a /
    for(i=0;input[i]!='/';i++)   //get all the numbers before the / for the month
         month=month*10+(input[i]-'0');  //convert to integer (the -'0', and for a number
    i++;                          //example  25 is 2*10 + 5
    for(;input[i]!='\0';i++)         //skip over the slash and do the same to create
         day=day*10+(input[i]-'0');  //the day, stopping when you get the null
    day--;      //subtract 1 from the day since starting at day 1 and Jan just is day 1, but 1+1=2
    for(i=1;i<month;i++)    //add the number of days in each complete month
       {switch(i)          // before the date. example March 3
        {case 1:           //add 31 days for Jan, and 28 for Feb to 3
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:   day+=31;
                   break;
        case 2:    day+=28;
                   break;
        default:   day+=30;
                   break;
         }
    }
    day%=7;              //now have the day of the year your date is get it into 0 through 7
    switch(day)           //and find out which day it is.  Since Jan 1 was a Saturday, day 0 is Saturday
    {
    case 1:
    printf("Happy Sunday!\n");
    break;
    case 2:
    printf("Happy Monday!\n");
    break;
    case 3:
    printf("Happy Tuesday!\n");
    break;
    case 4:
    printf("Happy Wednesday!\n");
    break;
    case 5:
    printf("Happy Thursday!\n");
    break;
    case 6:
    printf("Happy Friday!\n");
    
    break;
    case 0:
    printf("Happy Saturday!\n");
    break;
    }
    return 0;}
    Last edited by WA; 10-17-2011 at 08:15 PM.

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    "Blowing up" is a very unhelpful description. Help us help you and describe what exactly isn't working. Does it not compile or does it not run correctly? In the latter case describe when exactly does it all go wrong and do you get any error messages?
    OracleGuy

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The emacs compiler shows:

    weekday.c: In function 'main':
    weekday.c:8: error: 'month' undeclared (first use in this function)
    weekday.c:8: error: (Each undeclared identifier is reported only once
    weekday.c:8: error: for each function it appears in.)
    weekday.c:8: error: 'day' undeclared (first use in this function)
    weekday.c:9: error: expected ',' or ';' before 'int'
    weekday.c:19: error: missing terminating " character
    weekday.c:8: warning: unused variable 'value2'
    weekday.c:8: warning: unused variable 'value1'
    weekday.c:59: warning: control reaches end of non-void function

  • #4
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts
    The compile errors and warning you are getting don't appear to coincide with the code you have posted. Are you sure the emac printout is for that exact code?
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...

  • #5
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I don't know if it helps,I have compiled successfully your code in dev c++
    No errors at my end.

    I really reccomend you to use the dev c++ compiler or the Turbo c++ (ipreffer the dev one)

    Regards.

  • #6
    New Coder
    Join Date
    Apr 2011
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I must agree with some friends you should use c++ compiler, i would like to prefer c++


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •