Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Torchi12

macrumors newbie
Original poster
Oct 2, 2011
6
0
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
 
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.
 
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.

I was sure he used a letter in the input ;S
But he didnt. Thanks ;d
 
I was sure he used a letter in the input ;S
But he didnt. Thanks ;d

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.
 
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.

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};
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.