Thread: Hey, the newb is back again.

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    18

    Hey, the newb is back again.

    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.

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

    Thanks again,

    -Yi

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because that's how a switch works: it's just a jump to the label, not a set of mutually exclusive cases that never interfere with each other. (This is why we can say things like case 'Y': case 'y': -- both will do the same thing.) Occasionally it comes in handy (you can do the "save" part of "save and quit" and let it bleed into the "quit" case, to give an extremely contrived example).

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Because cases automatically drop down to the next case unless you break out of the switch. So unless you want more than one case statement to be processed (which sometimes you actually do desire this behavior) you need to put a break.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    /*
     * project2example.c
     *
     *  Created on: Oct 17, 2008
     *      Author: Yi
     */
    #include <stdio.h>
    #include <stdlib.h>
    
    const char *weekdays[] = { "Sun","Mon","Tues","Wednes","Thurs","Fri","Satur"};
    
    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("&#37;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;
    	}
    
    	printf("You were born on a %sday.", weekdays[weekday % 7]);
    	return(EXIT_SUCCESS);
    }

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    oO;; I see now


    master 5001, when you wrote const char, what exactly does this do?


    (Sorry for asking such basic questions, its just that I want to start making my own applications and stuff. Thank you.)

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    It is a string literal which is a fancy way of saying that it is a static block of text that you cannot change. I thought I may as well show you a faster alternative than your switch case statement.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    o_O

    I LIKE IT!! I think I will use something like that from now on.... what type of codes have you made master5001?

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I have done a lot of dev tools, embedded systems, PDA software, console development, internet tools, internet games, database software, the usual suspects.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And yet master is still stuck with the awkward T *var syntax which puts emphasis on *var and not the type (sniff)...
    So in case you haven't covered pointers yet... here's an interesting bit of text: http://www.research.att.com/~bs/bs_faq2.html#whitespace
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Bjarne Stroustrup
    A ``typical C programmer'' writes ``int *p;'' and explains it ``*p is what is the int'' emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.
    You are always badgering me about being a C programmer. So what do you expect coming from me?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    C or C++ doesn't matter - it's all about the concept. The typical C programmer writes T *, but that does not mean all must. And besides, being a C++ dev too, you know that C++ puts heavy emphasis on types, so T* is usually a better approach. So where does your allegiance lie? C or C++?
    And as an informative source to the OP...
    But maybe I actually posted it in error...
    Last edited by Elysia; 10-20-2008 at 11:09 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Duly noted. Though I must admit, my way seems superior when one is declaring multiple pointers. But since I am not in the mood to reply to whatever rhetorical quip you will give in retort, I will simply say, "You sure are right, Elysia. Thank you for setting me free from my evil ways of before!"

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by master5001 View Post
    Duly noted. Though I must admit, my way seems superior when one is declaring multiple pointers.
    That may be true, but remember that it's good advice to define each pointer on a separate row! That goes for variables, too.
    Anyway, it's not a path set in stone. It's ultimately up to you to really choose your way. As long as you're aware of it.

    But since I am not in the mood to reply to whatever rhetorical quip you will give in retort, I will simply say, "You sure are right, Elysia. Thank you for setting me free from my evil ways of before!"
    I won't quote that one, though...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Lol... well you can hardly blame me for trying. I can't brown nose you since it goes against every grain of my moral fiber, yet I would be happy beyond words if one of my semi-sarcastic quips made its way on to your list. Catch-22.

    I typing my types just the one time...

    Example:
    Code:
    int length, *plength, nums[256];
    But I know not everyone is a huge fan of that style either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey all I'm back
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 08-05-2003, 09:41 PM
  2. Yey Im Back!
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-05-2003, 03:07 PM
  3. Problems calling file back in and displaying it?
    By yo_da84 in forum C++ Programming
    Replies: 2
    Last Post: 06-04-2003, 11:38 AM
  4. Hey Board
    By TedRESI in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2001, 09:43 PM