Amazon.com Widgets Chapter 10 Exercise 4
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 21, 2014, 03:02:54 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 10
| | | |-+ Chapter 10 Exercise 4
Pages: [1] Go Down
Print
Author Topic: Chapter 10 Exercise 4 (Read 887 times)
clouded
Full Member
***
Posts: 123






on: May 31, 2012, 12:51:36 PM

This is what I did on this one... short and sweet:

Code: (Objective-C)
// Chapter 10 Exercise 4
// Using typedef and enumerated data types, define a type called Day with the possible
// values Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday.

//  main.m

#import <Foundation/Foundation.h>

int main (int argc, char * argv[])
{
    @autoreleasepool {
        
        typedef enum {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday} Day;
        
        Day day1;
        
        do {
            NSLog (@"Enter day number from 0 (Sun) to 6 (Sat), anything else to end:");
            scanf ("%i", &day1);
            
            switch (day1) {
                case Sunday:
                    NSLog (@"Sunday");
                    break;
                case Monday:
                    NSLog (@"Monday");
                    break;
                case Tuesday:
                    NSLog (@"Tuesday");
                    break;
                case Wednesday:
                    NSLog (@"Wednesday");
                    break;
                case Thursday:
                    NSLog (@"Thursday");
                    break;
                case Friday:
                    NSLog (@"Friday");
                    break;
                case Saturday:
                    NSLog (@"Saturday");
                    break;
                default:
                    NSLog (@"bad day number");
                    day1 = 7;
                    break;
            }
        } while (day1 != 7);
    }
    return 0;
}

Output:
My input is italicized below

Enter day number from 0 (Sun) to 6 (Sat), anything else to end:
0
Sunday
Enter day number from 0 (Sun) to 6 (Sat), anything else to end:
1
Monday
Enter day number from 0 (Sun) to 6 (Sat), anything else to end:
2
Tuesday
Enter day number from 0 (Sun) to 6 (Sat), anything else to end:
3
Wednesday
Enter day number from 0 (Sun) to 6 (Sat), anything else to end:
4
Thursday
Enter day number from 0 (Sun) to 6 (Sat), anything else to end:
5
Friday
Enter day number from 0 (Sun) to 6 (Sat), anything else to end:
6
Saturday
Enter day number from 0 (Sun) to 6 (Sat), anything else to end:
7
bad day number
Logged
mo7ionsickness
Jr. Member
**
Posts: 50






Reply #1 on: August 21, 2012, 06:25:31 AM

Code: (Objective-C)
int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        typedef enum {sunday = 1, monday, tuesday, wednesday, thursday, friday, saturday} Days;
       
        Days myDay;
        int anInt;
       
        while (anInt < 1 || anInt > 7){
            NSLog(@"Choose your a day off");
            NSLog(@"sun=1, mon=2, tue=3, wed=4, thu=5, fri=6, sat=7");
            scanf("%i", &anInt);
        }
           
        myDay = anInt;
       
        switch (myDay) {
            case sunday:
                NSLog(@"Your chosen day off is sunday");
                break;
            case monday:
                NSLog(@"YOur chosen day off is monday");
                break;
            case tuesday:
                NSLog(@"YOur chosen day off is tuesday");
                break;
            case wednesday:
                NSLog(@"YOur chosen day off is wednesday");
                break;
            case thursday:
                NSLog(@"YOur chosen day off is thursday");
                break;
            case friday:
                NSLog(@"YOur chosen day off is friday");
                break;
            case saturday:
                NSLog(@"YOur chosen day off is saturday");
                break;
            default:
                break;
        }
       
    }
    return 0;
}
2012-08-21 14:24:59.821 FractionTest[445:303] Choose your a day off
2012-08-21 14:24:59.823 FractionTest[445:303] sun=1, mon=2, tue=3, wed=4, thu=5, fri=6, sat=7
7
2012-08-21 14:25:08.890 FractionTest[445:303] YOur chosen day off is saturday
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.