Amazon.com Widgets 10.4 how to scanf it?????
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 02, 2014, 01:35:01 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
|-+ Old Stuff
| |-+ Answers to Exercises
| | |-+ Chapter 10
| | | |-+ 10.4 how to scanf it?????
Pages: [1] Go Down
Print
Author Topic: 10.4 how to scanf it????? (Read 7169 times)
sir
Full Member
***
Posts: 118


Email




on: March 18, 2009, 06:37:35 PM

Hey all,

I wanted to add a scanf to this exercise so that I can input the day of the week.  What do I put in the scanf so that uses my new typedef Day?

Code: (Objective-C)
/*
Using typedef and enumerated data types,
define a type called Day with the possible values
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday.
*/
#import <Foundation/Foundation.h>
#import <stdio.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    typedef enum { Sunday, Monday, Tuesday, Wednesday,
   Thursday, Friday, Saturday } Day;

Day myDayOff;

NSLog(@"Type in the day:");
scanf (" %Day", myDayOff);

NSLog(@"My day off is: ");

switch (myDayOff) {
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(@"NEVER!!!!!!");
break;
}

    [pool drain];
    return 0;
}


Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: March 18, 2009, 06:49:11 PM

Ahh, you can't use enumerated data types that way.  They're really treated just like integers at the lower level by the system.  So you could use this scanf call:

Code: (Objective-C)
	scanf ("%i", &myDayOff);

and type in an integer from 0-6.  That will work based on the fact that first enumeration (Sunday) is assigned the integer value 0, the second the integer value 1, and so on.

Cheers,

Steve Kochan


Logged
sir
Full Member
***
Posts: 118


Email




Reply #2 on: March 18, 2009, 07:11:50 PM

OK, I see.  Thanks.

Nicholas
Code: (Objective-C)
/*
Using typedef and enumerated data types,
define a type called Day with the possible values
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday.
*/
#import <Foundation/Foundation.h>
#import <stdio.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];


    typedef enum { Sunday = 1, Monday, Tuesday, Wednesday,
   Thursday, Friday, Saturday } Day;

Day myDayOff;

NSLog(@"Sunday    is imput with 1");
NSLog(@"Monday    is imput with 2");
NSLog(@"Tuesday   is imput with 3");
NSLog(@"Wednesday is imput with 4");
NSLog(@"Thursday  is imput with 5");
NSLog(@"Friday    is imput with 6");
NSLog(@"Saturday  is imput with 7");

NSLog(@"Type in the number for the day:");
scanf (" %i", &myDayOff);

NSLog(@"My day off is: ");

switch (myDayOff) {
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(@"NEVER!!!!!!");
break;
}

    [pool drain];
    return 0;
}

Logged
jclermont
Jr. Member
**
Posts: 55


Email




Reply #3 on: July 12, 2009, 12:05:28 PM

Is there any way to take an enum value (like 1) and convert it to the name (like Monday)?

In .NET I would do something like Enum.GetName(value) and I'd get the friendly label back as a string.
Logged
Hugo
Newbie
*
Posts: 42



Email




Reply #4 on: August 23, 2009, 05:40:32 PM

I have the same question as jclermont. How can we use scanf so that the user can input the name of the day instead of the number?

I am trying to use the following but it's not working:
Quote
typedef enum { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } Day;
   
Day myDay = Saturday;

NSLog(@"Enter day:");
scanf("%s", &myDay);
Logged

Website about all things electronic: GadgetTechie.net
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #5 on: August 23, 2009, 06:15:28 PM

No, there's no way to do what you describe.  The closest approximation would be to read the day in as a string of characters, or as an integer, as noted in my earlier reply to this post.

Cheers,

Steve Kochan
Logged
Hugo
Newbie
*
Posts: 42



Email




Reply #6 on: August 23, 2009, 06:21:59 PM

Your previous reply only mentions using integers. How do you read in a string of characters?
Logged

Website about all things electronic: GadgetTechie.net
MarkReid
Full Member
***
Posts: 173






Reply #7 on: October 05, 2009, 07:03:08 AM

In my scanf line I get the warning

Format '%i' expects type 'int *', but argument 2 has type 'enum Day *'

Is that normal?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #8 on: October 05, 2009, 10:26:33 AM

Theoretically, enumerated data types are distinct types, even though the values are internally represented as integers.  There's no way around the scanf warning, unless you read the value into an integer first and then do the assignment using casting, like so:


Code: (Objective-C)
int dayValue;
enum Day myDay;

scanf ("%i", &dayValue);
myDay = (enum Day) dayValue;

Effectively, you're doing the same thing, so I would just let it go.

Cheers,

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