VenushkaT
VenushkaT

Reputation: 1150

Run c++ program using unix terminal

I have C++ program. there i have used C++ . in order to run that program i'm passing values via unix terminal. But i couldn't run code snippet by passing args values.

  1. obtain bash build path as

Users/venushka/Library/Developer/Xcode/DerivedData/testFunctions-blcisknaxkrqribioawblwrttsib/Build/Products/Debug

  1. i executed myFunctions bash as

    ./testFunctions

  2. i pass args as

    ./testFunctions 1

please find below the code snippet i used

int main(int argc, const char * argv[]) {
    // insert code here...
    //std::cout << "Hello, World!\n";
    if (*argv[0] == 1) {
        std::cout << "Hello, World!\n";
        //test();
    }
    return 0;
}

when i gives 1 as arg my programe line hello world hasn't executed. what am i do wrong please help me. thank you . please find the image belowMy terminal image

Upvotes: 0

Views: 93

Answers (1)

John Zwinck
John Zwinck

Reputation: 249153

This is wrong:

if (*argv[0] == 1)

argv[0] is a string which is the name of your program. Dereferencing it (with *) gives you the first character. It will never match 1 which is a non-printing ASCII code. Instead:

if (strcmp(argv[1], "1") == 0)

That will check if the first argument to the program (not its name) is the string "1".

Upvotes: 5

Related Questions