hey all :)

I'm making a console program, and I want to do that when you run it from the terminal, you can give arguments like -debug (./program -debug). How do i get whether an argument is set or not in the code? i've tried #ifdef debug, but it didn't work.
Btw, I'm using g++ compiler (just in case it was different from compiler to compiler)

Recommended Answers

All 2 Replies

Command line arguments are passed as parameters to main. The first parameter gives you the number of arguments (including the program name) and the second gives you an array of strings containing the actual arguments:

#include <iostream>

int main ( int argc, char *argv[] )
{
  // The first argument is the program name, if available
  if ( argc > 1 ) {
    for ( int i = 1; i < argc; i++ )
      std::cout<< argv[i] <<'\n';
  }
}

thanks, it worked :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.