Function converting from 1-7 for days of the week to actual strings

This is a discussion on Function converting from 1-7 for days of the week to actual strings within the C Programming forums, part of the General Programming Boards category; Originally Posted by Elysia I can't get the asserts to work, though. If I don't comment them out, the compiler ...

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Elysia View Post
    I can't get the asserts to work, though. If I don't comment them out, the compiler will cry out syntax error.
    Code:
    #include <assert.h>

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Already did that.
    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.

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Aaargh, we're going horribly backwards here!

    strftime - okay so it's not supported. That's a shame, but never mind.

    stanlvw: Why on earth did you write such an abomination when the code I gave you was absolutely ideal? You don't have to specify strings arrays by individually specifying each character constant individually. And then you certainly don't have to declare 7 pointers to those 7 character arrays. What I wrote does exactly the same thing but with far less code and in a much more efficient way.
    If you don't understand how it worked then just ask.

    zacs7, even your code overcomplicates things a little, and has the same array declaration bug that pankaj401 originally posted with the array index. You'll notice it wont compile.
    Are you sure you want to introduce runtime error handling to this? I think an assert is much better in this case.

    Elysia: That isn't really an improvement in most cases. Sure if all you want is a pointer to a constant day of week name string then that will work. But most likely you want to place that piece of text in some other string, such as to build up a date string. In that case you would need to add a strcpy outside this function in every place you use it. In my code the strcpy is done in one place inside the function.

    If you're unfamiliar with asserts, then I strongly suggest you learn about and use them.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Quote Originally Posted by iMalc View Post
    If you're unfamiliar with asserts, then I strongly suggest you learn about and use them.
    Not unfamiliar with them. I've used plenty of MFC ASSERT, but not CRT assert.
    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.

  5. #20
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    The C89 rules about variable declarations and where they are placed still apply in the case of macros like assert.

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    True enough, that does the trick.
    A little mistake on iMalc's part, then, I guess. Proper C89 code would be:

    Code:
    void getDayOfWeekName(int inDayIndex, char *outDayName)
    {
        static const char* const days[] =
            { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
        assert(1 <= inDayIndex && inDayIndex <= 7);
        assert(outDayName != NULL);
        strcpy(outDayName, days[inDayIndex - 1]);
    }
    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.

  7. #22
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I dont know what asserts are but somehow this just looks wrong:
    assert(1 <= inDayIndex && inDayIndex <= 7);

  8. #23
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    It's not. Just remove the assertion for a second and see for yourself:
    Code:
    #include <assert.h>
    #include <stdio.h>
    #include <string.h>
    
    #define WYSIWYG(maybe) printf( #maybe##", %d\n", (maybe) );
    
    void getDayOfWeekName( int inDayIndex, char *outDayName )
    {
        static const char* const days[] =
            { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
         WYSIWYG( ( 1 <= inDayIndex && inDayIndex <= 7 ) )
        assert(outDayName != NULL);
        strcpy(outDayName, days[inDayIndex - 1]);
    }
    
    int main( void )
    {
        int i;
        char p[10];
    
        for ( i = 1; i <= 7; i++ ) {
            getDayOfWeekName( i, p );
        }
        return 0;
    }
    Last edited by whiteflags; 01-01-2008 at 01:08 PM.

  9. #24
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oh yeah I'm dumb >_<

  10. #25
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    My preference would be to return a const char*, for two reasons:
    1) any need for an strcpy() is defered to the caller
    2) ability to indicate a bad param error condition (return NULL)

    gg

  11. #26
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Quote Originally Posted by mike_g View Post
    I dont know what asserts are but somehow this just looks wrong:
    I guess I confused things by putting that in there, and in the wrong place for C89.
    I write it like this: 1 <= x && x <= 7 because it kinda looks like 1 <= x <= 7, which is pretty much mathematical syntax to show x being between 1 and 7 inclusive. Kinda nifty once you get used to it I think.
    Feel free to write x >= 1 though instead of course.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #27
    Registered User
    Join Date
    Nov 2007
    Location
    Bangalore, India
    Posts
    24
    Quote Originally Posted by pankaj401 View Post
    How about indexing the character array conatining day strings with the values [1...7] returned in the byte.
    Code:
    char days[7][]={ "Sunday", "Monday",... "Saturday" }; // You could keep this global in .text or .rodata
    
    uByte i = ... // The byte value that you read in, assuming returns 1 to 7.
    And you return
    Code:
    return days[i - 1];
    My mistake: Should have been
    Code:
     char days[7][10]={ "Sunday", "Monday",... "Saturday" }; // You could keep this global in .text or .rodata

  13. #28
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    I would also go with the return a const char* approach since it seems more flexible.
    In many cases you might not want to reassign the actual string you get. You might want to compare it or such. Then a const char* is perfect.
    However, if you really do need to modify the contents or use it in any way, you're free to do the copy yourself. Thus you may save time, as well. New function:

    Code:
    const char* getDayOfWeekName(int inDayIndex)
    {
    	static const char* const days[] = 
    		{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    	assert(inDayIndex >= 1 && inDayIndex <= 7);
    	return days[inDayIndex - 1];
    }
    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. #29
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Yup that's good too IMHO.
    Using const char* as the return value is useful.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #30
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Is the assertion used for error-checking to ensure that the number is within range? It seems that mikroC doesn't support that.

    I came up with a way of getting around it such as:

    Code:
    const char* getDayOfWeekName(int inDayIndex)
    {
            static const char* const days[] =
                    { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
    "Saturday" };
            if (!(inDayIndex >= 1 && inDayIndex <= 7)){
               return "Error";
            }
            return days[inDayIndex - 1];
    
    }
    Please let me know what you think. Thanks.
    Last edited by stanlvw; 01-02-2008 at 07:27 PM.

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21