Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8860427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:15:52+00:00 2026-06-14T15:15:52+00:00

I want to pass command line arguments to main. It should be done via

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:15:52+00:00Added an answer on June 14, 2026 at 3:15 pm

    You need an additional argv++, argc-- for your two values per option case. Test also, if the second value is already the next option.

    case 'r':
         x_0=atof(argv[2]);
         if (!argv[3] || argv[3][0] == '-') {
                cout<<"Warning: No y_0 specified. Reverting to default."<<endl;
         } else {
            y_0=atof(argv[3]);
            argv++;
            argc--;
         }
         break;
    

    As @ezod suggested, you might use getopt. See Supplying two arguments to command line option using getopt
    and C getopt multiple value for similar requirements regarding getopt.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to pass command-line arguments like --address to the development server from my
I want to pass command line arguments to my java program that is invoked
I'd like to be able to pass command line arguments to programs that are
I want to pass parameters from PHP Command Line Interface, and then read in
Can I pass variables to a GNU Makefile as command line arguments? In other
I have a program that can accept command-line arguments and I want to access
I have a format holding paths to files and command line arguments to pass
I want to pass the output of a Sed command to a variable in
I want to pass a datetime value into my python script on the command
I want to be able to pass anything to a git command (maybe its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.