Currently I am developing a couple of C programs on my mac using Xcode. There however is 1 problem. My study requires me to use some sort of input field through the coding.
Sounds to me like you want to get your arguments from the command line and if they are missing, prompt the user for them
Lets assume you want the arguments: number word number
#include <stdio.h>
#include <string.h>
int number1;
char word[128];
int number2;
int main(int argc, const char **argv)
{
if (argc == 4)
{
number1 = atoi(argv[1]);
strcpy(word, argv[2]);
number2 = atoi(argv[3]);
}
else
{
do
printf("Enter number word number: ");
while (scanf("%d %s %d", &number1, word, &number2) != 3);
}
printf("I got %d '%s' %d\n", number1, word, number2);
return 0;
}
Which gives:
$ ./test
Enter number word number: 1 andy 12
I got 1 'andy' 12
$ ./test 2 pandy 56
I got 2 'pandy' 56
Note that the error-checking is poor in this example and can be improved alot (not using atoi()
is one way to start).
It sounds like you need to check argc in the program as RageD points out, otherwise when launching the program with insufficient arguments will cause problems.
gcc is the c compiler - it produces an executable. When you hit 'Run' in Xcode it compiles your program and then runs the executable file created. The executable created is named the same as your Xcode project name.
To run the program you built in Xcode from the command line:
The result will look something similar to the snippet below (for my 'MyCommandLineApp' project):
$ /Users/pliskin/Library/Developer/Xcode/DerivedData/MyCommandLineApp-hbpuxhguakaagvdlpdmqczucadim/Build/Products/Debug/MyCommandLineApp argument1 argument2
HOWTO Pass command line arguments to your program from Xcode IDE
/phi
as the first argument, then 10
as the second.Noteworthy: This is also where you can specify the current working directory of your program at launch rather than the long, temp path Xcode uses when building your binaries. To do so:
That in combination with getting your parameter handling fixed in your program should get you up and running.