command line programming help

Discussion in 'Mac Programming' started by twoodcc, Dec 17, 2007.

  1. twoodcc macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #1
    i really just want to start with command-line programming for the mac, in obj-c. where would i go to get me started?

    i have already taken some programming classes in other languages, so it's not totally new to me.

    and a quick question, how do i ask for input from the user in obj-c?

    thanks
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Objective-C is a pure superset of C. If you are running at the command line level you are forgoing most of Cocoa (although you still have the Foundation classes easily available), so you will have to use the normal C functions to get user input.
     
  3. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #3
    well, the most basic way is to use the formatted I/O family of functions from the standard C library on stdin/stdout (printf, scanf, etc.)
     
  4. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #4
    i think i am a little confused about cocoa and obj-c. is there a difference?

    basically, what language is this:

    Code:
    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        // insert code here...
        NSLog(@"Hello, World!");
        [pool drain];
        return 0;
    }
    
    and how would i ask for input?

    could you give me an example of one of those commands?
     
  5. CaptainZap macrumors regular

    Joined:
    Jan 17, 2007
    #5
    Examples -

    Code:
    #import <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	int x;
    	printf ("Hello, enter a number- ");
    	scanf ("%i",&x);
    	printf ("You entered %i", x);
    
    	return 0;
    }
    
    This prints out "Hello, enter a number- " and then "scans" the value and puts it into the integer x. Then it prints the value to the user.
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Objective-C is the language, Cocoa is the complete set of frameworks, predominantly FoundationKit and the AppKit, but these days expanding to include CoreAnimation, CoreImage etc. The example you posted is simply Objective-C using the FoundationKit: not really Cocoa. A quick Google search will give examples of the normal C I/O functions.
     
  7. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #7
    thanks. i'll try to work with that.

    oh ok. so the code i listed isn't what i really want to use?

    also, can i use a matrix with obj-c ?
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    You can do anything you can do in C. The normal C solution would be to use an array, or if the bounds of the matrix are not known a more complex data structure (say a linked list). As you have the choice of using Foundation you can use NSMutableArrays...
     
  9. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #9
    thanks. so how would i use NSMutableArrays? sorry to ask all of this, i just ordered a book, but until it gets here.....:eek:
     
  10. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #10
  11. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
  12. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
  13. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #13
    Of course. That is a standard C statement.
     
  14. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #14
    oh ok, just checking.

    could you give me just a little example of one in obj-c?

    thanks
     
  15. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #15
    Not sure I don't use Obj-C. I'd just do :

    switch(int)
    {
    case 1: statement
    break;

    case 2: statement
    break;

    default:
    break;
    }

    depends what you want to do though.

    Edit : It should be the same in Obj-C. I see no reason why it would change. If it has that method should still work anyway.
     
  16. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #16
    thanks. i got it halfway working.

    if i choose case 1, and it does what i want, but then it exits. i don't want it to exit after i choose case 1.

    edit: how would i get it to go back to the switch statement beginning?
     
  17. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #17
    twoodcc, pick up a book on C. Then you can learn step-by-step how to use loops, control statements, and input and output. Knowing C will help you learn Objective-C, because the Objective-C language uses C if-statements, for/while/do loops, etc. You need to know those concepts before understanding classes in Objective-C.
     
  18. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #18
    That is how a switch statement is meant to act. What is it that you are trying to do? A for or while loop maybe a better option depending.
     
  19. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #19
    thanks, i already have ordered a book. but i have taken a class in C#, and matlab, and visual basic, so i do have some understanding of these concepts. i just don't know the syntax in obj-c

    well, i know i've done this before, where you start off with a switch statement, with options for the user to pick from. but i don't want the program to exit until they pick the exit option
     
  20. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #20
    Can you post your code?

    So I assume you are trying to have a menu or some such where the user inputs a number and then is taken to the explicit part of the program that they have selected depending on what their option was?

    Just do something like :

    Code:
    switch(menuInt)
    {
    case 1: randomFunction();
    break;
     
    case 2: randomFunctionTwo();
    break;
     
    default:
    break;
    }
     
    exit(0);
     
    void randomFuntion(void)
    {
        //put code for first menu option here
    }
     
    void randomFunctionTwo(void)
    {
       //put code for second menu option here
    }
    Edit : The curses library is great for these types of terminal application.
     
  21. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #21
    you are right. that is what i want to do.

    here is my code, but it isn't finished yet:

    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[]) {
        float miles;
    	float gallons;
    	float price;
    	float mpg;
    	float ppm;
    	float totalMiles;
    	float totalGallons;
    	float totalPrice;
    	int text;
    	
    	printf ("Hello, what would you like to do?\n");
    	printf ("1 - enter miles\n");
    	printf ("2 - enter gallons\n");
    	printf ("3 - enter price\n");
    	printf ("4 - see summary\n");
    	printf ("5 - exit\n");
    	
    	switch (scanf ("%i", &text))
    	{
    		case 1:
    		{
    			printf ("Hello, enter miles driven- ");
    			scanf ("%f", &miles);
    			totalMiles += miles;
    		}
    		
    		case 2:
    		{
    			printf ("Hello, enter gallons used- ");
    			scanf ("%f", &gallons);
    			totalGallons += gallons;
    		}
    		
    		case 3:
    		{
    			printf ("Hello, enter price paid- ");
    			scanf ("%f", &price);
    			totalPrice += price;
    		}
    		
    		case 4:
    		{
    			mpg = miles/gallons;
    			ppm = price/miles;
    			printf("You drove %.2f miles.\n", miles);
    			printf("You used %.3f gallons.\n", gallons);
    			printf("You paid $%.2f\n", price);
    			printf("You got %.2f miles per gallon.\n", mpg);
    			printf("You got $%.2f per mile\n", ppm);
    		}
    		
    		case 5:
    			printf("Goodbye");
    			break;
    
    	}
    	
    	
       
        return 0;
    }
    
     
  22. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #22
    You could just stick it in a while loop and then if the user picks option 5 change the value of the variable so the while loop state is true and thus drops out.
     
  23. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #23
    i've thought about that, but i know i did this before, in matlab i think. i'll try to find what i did before.
     
  24. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #24
    That's the problem. Objective-C IS C, but with some other features. So asking how to do a switch statement or a loop in Objective-C is really asking how to do it in C.

    If you learn C before Objective-C, it will make Objective-C a whole lot easier, especially if you're not doing GUI apps.
     
  25. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #25
    well, like i said, i should have the book here in less than a week. but til then, i think i see the problem with my code, or at least i hope so.

    it looks like i'm scanning for text, but shouldn't i be scanning for an 'int' in my switch statement?
     

Share This Page