tags:

views:

38

answers:

1

I'm trying to pass arguments in XCode and understand you need to add them from the Args tab, using the Get Info button, in the Executables of the Groups and Files pane. I'm trying to see if I can get it to work, but am having some difficulty. My program is simply:

#include <iostream>
#include <ostream>
using namespace std;

int main(int argc, char *argv[]) {

    for (int i = 0; i < argc; i++) {
        cout << argv[i];
    }

    return 0;

}

And in the Args tab, I have the number 2 and then in another line the number 1. I do not get any output when I run the program. What am I doing wrong? Thanks!

A: 

Your code works fine and it displays the arguments. You may want to print a new line after each argument to make the output more readable:

cout << argv[i] << "\n";

Output is visible in the console (use Command+Shift+R to bring up the console).

diciu
Why do you have to run it in the Console vs double clicking on the exe that you select from the Groups and Files pane. It looks like it brings it up in Terminal.
Crystal
If you double click the executable from Groups and Files/Products it's executed without any arguments and outside XCode. The arguments set up in the Executables section are only used when the executable is run under XCode.If you want to run it outside XCode you can navigate with Terminal to the build/Debug folder and run the binary yourself (e.g. ./exename 1 2)
diciu
thanks, that helps a lot!
Crystal