Amazon.com Widgets Not getting the output I was expecting at the command line...
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 05:07:00 PM
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
| |-+  Help!!!
| | |-+  Not getting the output I was expecting at the command line...
Pages: [1]   Go Down
Print
Author Topic: Not getting the output I was expecting at the command line...  (Read 733 times)
jblaven
Newbie
*
Posts: 12






« on: February 07, 2010, 09:30:33 PM »

Hey guys,

Below I am trying to figure out how to use the switch statement.  When I "Build and Go", the command line gives me: "What do you want to solve? (s)ell, (m)argin, or (c)ost?".  From there I expect to be able to type in "s" for sell.  Since I am just testing out my code as I go along, I was hoping it would pass the value "s" into keyInput, which would then start the switch statement.  ...and since keyInput would equal "s", the first case would be true, and the following would be shown at the command line: "You are solving for sell".  ...but I don't get this last sentence.  Any ideas on what I am missing? Huh

Thanks,

Joe


Code: (Objective-C)
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

char keyInput;

// Create an instance of GmCalc
//GmCalc *myGmCalc = [[GmCalc alloc] init];

NSLog (@"What do you want to solve? (s)ell, (m)argin, or (c)ost?");
scanf ("%c", &keyInput);   

switch (keyInput) {
case 's':
NSLog (@"You are solving for sell");
break;
case 'm':
// <#statements#>
break;
case 'c':
// <#statements#>
break;
default:
break;
}
// Create an instance of Cost
//Cost *myCost = [[Cost alloc] init];
    [pool drain];
    return 0;
}
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #1 on: February 08, 2010, 10:29:54 AM »

As has been discussed in other places in this forum,  scanf is a fussy function.   Change the format from this

Code: (Objective-C)
   scanf ("%c", &keyInput); 

to this

Code: (Objective-C)
   scanf (" %c ", &keyInput); 

The space character tells scanf to skip any whitespace (space, newline, tab, form feed) characters in the input.

CHeers,

Steve
Logged
jblaven
Newbie
*
Posts: 12






« Reply #2 on: February 08, 2010, 01:21:07 PM »

Thanks Steve,

I guess I wasn't sure what to search the forums for...  it actually makes me feel a little better because it seems like such a subtle difference.

Regards,

Joe

p.s. Strange, can't get it to work.  I still don't get "You are solving for sell" in the command line.  I'll do some searching on the forums too.  I might be missing something else. Undecided
« Last Edit: February 08, 2010, 01:29:24 PM by jblaven » Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #3 on: February 08, 2010, 03:05:45 PM »

See if this thread helps:  http://classroomm.com/objective-c/index.php?topic=899.0

Cheers,

Steve
Logged
jblaven
Newbie
*
Posts: 12






« Reply #4 on: February 08, 2010, 03:40:25 PM »

Yeah,

I've tried both return and enter keys. ...and just as everyone says, the enter key doesn't work, only the return key.  Funny thing is, it works for exercise 5.5, but not for my problem above. Shocked
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #5 on: February 08, 2010, 03:53:54 PM »

Ok, so I tried running your program.  It works for me.  I don't have any other ideas.  Try cleaning all your targets and rebuilding or starting with a brand new project and copying the code over.   I don't know of any other options that would affect it.

Good luck,

Steve
Logged
jblaven
Newbie
*
Posts: 12






« Reply #6 on: February 08, 2010, 05:02:13 PM »

Good idea,  I think I will start over clean with just the code.  Grin
« Last Edit: February 08, 2010, 06:39:23 PM by jblaven » Logged
jblaven
Newbie
*
Posts: 12






« Reply #7 on: February 08, 2010, 07:17:08 PM »

I think I noticed something very important...  the program is not finishing.

I usually get:

The Debugger has exited with status 0.

...but instead, It just sits there like it is still computing something.

Joe
Logged
jblaven
Newbie
*
Posts: 12






« Reply #8 on: February 09, 2010, 08:59:26 AM »

OK,

After a couple of days of banging my head against the desk, I took a different approach.

I found some code of a switch statement online, cut and pasted it into my program.  Ran it to make sure it was error free.  It was.  Then made the modifications one at a time.  As I made each mod, I saved it, and ran it.  That way, I would know if the most recent change was the cause of the error.

Here is the code that I used:
Code: (Objective-C)
#import <Foundation/Foundation.h>

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

   int value;

   printf ("Enter a number between 0 and 5: ");

   scanf ("%i", &value);

   switch (value)
   {
      case 0:
        NSLog (@"zero");
        break;
      case 1:
        NSLog (@"one");
        break;
      case 2:
        NSLog (@"two");
        break;
      case 3:
        NSLog (@"three");
        break;
      case 4:
        NSLog (@"four");
        break;
      case 5:
        NSLog (@"five");
      default:
        NSLog (@"Integer out of range");
        break;
   }
        [pool drain];
        return 0;
}

This code works for me, so I am in the process of modifying it to work for my application.  Once I get it updated, I'll post my code.  Grin
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.