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

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
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.)
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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.

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?

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

could you give me an example of one of those commands?
 

CaptainZap

macrumors regular
Jan 17, 2007
170
0
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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?

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.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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.

thanks. i'll try to work with that.

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.

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

also, can i use a matrix with obj-c ?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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 ?

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

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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...

thanks. so how would i use NSMutableArrays? sorry to ask all of this, i just ordered a book, but until it gets here.....:eek:
 

Cromulent

macrumors 604
Oct 2, 2006
6,810
1,100
The Land of Hope and Glory
oh ok, just checking.

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

thanks

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.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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.

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?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

Cromulent

macrumors 604
Oct 2, 2006
6,810
1,100
The Land of Hope and Glory
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?

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.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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.

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

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.

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
 

Cromulent

macrumors 604
Oct 2, 2006
6,810
1,100
The Land of Hope and Glory
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

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.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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;
}
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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.

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.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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

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.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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.

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