...

View Full Version : new to programming help needed thank you



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;}

oracleguy
10-17-2011, 08:45 PM
"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?

williama
10-17-2011, 09:14 PM
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

bobleny
10-17-2011, 10:51 PM
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?

CodeBeast
10-26-2011, 01:04 PM
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.

logoonlinepros
10-28-2011, 12:01 PM
I must agree with some friends you should use c++ compiler, i would like to prefer c++



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum