Hi everyone. Its Yi again. Well I just wanted to thank you for helping me with my program back earlier in this month. Let me show you my second program that I typed up today.
So its to determine if you were born on a Sunday---Saturday. Haha, its pretty funny and neat.... But this is where I wanted to use switches instead of a lot of if statements.
So, when I did this the first time I put in the switch(weekday) etc. But when it got down to the cases, it displayed every one of them, so then I asked my friend why it does that, and he told me that I need to put "break" after it. So what I'm wondering is why so I need a break after it? Sorry for asking such a newb question, its just something I was wondering today.Code:/* * project2example.c * * Created on: Oct 17, 2008 * Author: Yi */ #include <stdio.h> #include <stdlib.h> int main(void) { int month, day, year, z, weekday; printf("Please enter the month you were born followed be ENTER: [1-12] "); fflush(stdout); scanf("%d", &month); printf("Please enter the day you were born followed by ENTER: [1 -31]"); fflush(stdout); scanf("%d", &day); printf("Please enter the day you were born followed by ENTER:"); fflush(stdout); scanf("%d", &year); if (month < 3) { z = year - 1; } else { z = year; } weekday = 23*month/9 + day + 4 + year + z/4 - z/100 + z/400; if (month >= 3); { weekday = weekday - 2; } weekday = weekday % 7; printf("You were born on a "); switch (weekday) { case 0: printf("Sunday."); break; case 1: printf("Monday."); break; case 2: printf("Tuesday."); break; case 3: printf("Wednesday."); break; case 4: printf("Thursday."); break; case 5: printf("Friday."); break; case 6: printf("Saturday."); break; } return(EXIT_SUCCESS); }
Thanks again,
-Yi