I want to pass command line arguments to main. It should be done via a option specifier like -a 4. For single char and a singe value everything works fine, but when trying to pass two values per option it only works when put in last.
The following code works as wanted when used like this ./a.out -a 4 -b 6 -r 2 1
a:4
b:6
x_0:2
y_0:1
However using ./a.out -r 2 3 -a 32
only the default values for a
and b
are used.
a:1
b:1
x_0:2
y_0:3
Has it to do with the increment of argv and argc?
Thanks for your help.
Code: Compile with g++ main.cc
//--------------------------------------
// Includes
//--------------------------------------
#include <iostream>
#include <cstdlib>
//--------------------------------------
// Function Declarations
//--------------------------------------
void getArgsToMain(int argc, char *argv[]);
void showVars();
//--------------------------------------
// Variable declarations
//--------------------------------------
double a = 1.;
double b = 1.;
double x_0 = a;
double y_0 = 0.;
// typedefs and namespace
using namespace std;
// === FUNCTION ======================
// Name: main
// Description:
// =====================================
int main ( int argc , char * argv[] )
{
getArgsToMain(argc, argv);
showVars();
return 0;
}// ---------- end of function main ----------
//--------------------------------------
// Main function implementations
//--------------------------------------
void getArgsToMain(int argc, char *argv[])
{
while ((argc>1) && (argv[1][0]=='-'))
{
switch(argv[1][1])
{
case 'a':
a=atof(argv[2]);
break;
case 'b':
b=atof(argv[2]);
break;
case 'r':
x_0=atof(argv[2]);
if (!argv[3])
cout<<"Warning: No y_0 specified. Reverting to default."<<endl;
else
y_0=atof(argv[3]);
break;
default:
cout<<"Usage: blabla";
exit(0);
}
argv++;
argv++;
argc--;
argc--;
}
}
void showVars()
{
cout << endl;
cout << "a:" << a << endl;
cout << "b:" << b << endl;
cout << "x_0:" << x_0 << endl;
cout << "y_0:" << y_0 << endl;
}