Objective C - Enumerated data type

Discussion in 'Mac Programming' started by Torchi12, Oct 9, 2011.

  1. Torchi12 macrumors newbie

    Joined:
    Oct 2, 2011
    #1
    Wrote exactly as seen in a tutorial:

    Code:
    #import <Foundation/Foundation.h>
    
    int main (int argc, const char* argv[])
    {
    
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        
        enum day {m=1, t, w, h, f};
        enum day entry;
        
        NSLog(@"Enter a number of day of the week: ");
        scanf("%i", &entry);
        
        NSLog(@"entry = %i", entry);
        
        switch(entry)
        {
            case m:
                NSLog(@"Monday!");
                break;
            case t:
                NSLog(@"Tuesday!");
                break;
            case w:
                NSLog(@"Wednesday!");
                break;
            case h:
                NSLog(@"Thursday!");
                break;
            case f:
                NSLog(@"Friday");
                break;
            default:
                NSLog(@"Wtf, you high?");
                break;
        }
        
        NSLog(@"done");
        
        [pool drain];
        return 0;
    }
    
    If I enter 1-5 it works fine.
    If I enter m,t,w,h or f it will act weird.

    If I do it in xcode it will the output is:
    Code:
    2011-10-09 06:21:25.916 MyNewProject[860:903] Enter a number of day of the week: 
    w
    2011-10-09 06:25:48.575 MyNewProject[860:903] entry = 0
    2011-10-09 06:25:48.580 MyNewProject[860:903] Wtf, you high?
    2011-10-09 06:25:48.585 MyNewProject[860:903] done
    Program ended with exit code: 0
    
    
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    You're scanning (when you call scanf) for an integer (%i), not a character (%c), so it doesn't make sense to enter a character.
     
  3. Torchi12 thread starter macrumors newbie

    Joined:
    Oct 2, 2011
    #3
    I was sure he used a letter in the input ;S
    But he didnt. Thanks ;d
     
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    Blindly following through a tutorial is easy. But do you really understand what's going on? Would you now be able to write that code from scratch?

    To answer those, test your understand of enums and input scanning by changing the program so that you do enter a letter instead of a number.
     
  5. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    A simple question testing the understanding of enums: What changes if you change the enum to


    Code:
    enum day {monday=1, tuesday, wednesday, hursday, friday};
    or

    Code:
    enum day {sunday=1, monday, tuesday, wednesday, hursday, friday, saturday};
     
  6. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #6
    Okay, do what gnasher729 says above first, then do what I said.

    @gnasher729 I gave this exercise for a specific reason. The OP had showed a specific flaw in his understanding on the nature of enums in C. The goal of this exercise is to reveal, challenge, and repair that flaw.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    No tea on Thursdays?
     

Share This Page