williama
10-17-2011, 07:48 PM
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:
// 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;}
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:
// 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;}