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

  • Home
  • SEARCH
  • 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 646775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:38:59+00:00 2026-05-13T21:38:59+00:00

Okay I have updated my code a little, but I am still not exactly

  • 0

Okay I have updated my code a little, but I am still not exactly sure how to use the vector of command line arguments that I pass. I tried to set it up like the code I have below, but it wont compile. It gives me the error that it cannot find argc and argv:

1>c:\users\chris\documents\visual studio 2008\projects\cplusplustwo\cplusplustwo\application.h(32) : error C2065: ‘argc’ : undeclared identifier
1>c:\users\chris\documents\visual studio 2008\projects\cplusplustwo\cplusplustwo\application.h(32) : error C2065: ‘argv’ : undeclared identifier

main.cpp

#include "application.h"

int main(int argc,char *argv[]){
    vector<string> args(argv, argv + argc);
    return app.run(args);    
}

application.h

#include <boost/regex.hpp>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include "time.h"
using namespace std;



class application{
private:
    //Variables
    boost::regex expression;
    string line;
    string pat;
    string replace;
    int lineNumber;
    char date[9];
    char time[9];


    void commandLine(vector<string> args){
        string  expression="";    // Expression
        string  textReplace="";   // Replacement Text
        string  inputFile="";     // Input File
        string  outputFile="";    // Output Directory
        int optind=1;
        // decode arguments
        for(vector<string>::iterator i = args.begin(); i != args.end(); ++i){
            while ((optind < argc) && (argv[optind][0]=='-')) {
                string sw = argv[optind];
                if (*i == "-e") {
                    optind++;
                    expression = argv[optind];
                }
                else if (*i == "-t") {
                    optind++;
                    textReplace = argv[optind];
                }
                else if (*i == "-i") {
                    optind++;
                    inputFile = argv[optind];
                }
                else if (*i == "-o") {
                    optind++;
                    outputFile = argv[optind];
                }
                else{
                    cout << "Unknown switch: " 
                        << argv[optind] << "Please enter one of the correct parameters:\n" 
                        << "-e + \"expression\"\n-t + \"replacement Text\"\n-i + \"Input File\"\n-o + \"Onput File\"\n";
                    optind++;
                }
            }
        }
    }
    //Functions
    void getExpression(){
        cout << "Expression: ";
        getline(cin,pat);
        try{
            expression = pat;
        }
        catch(boost::bad_expression){
            cout << pat << " is not a valid regular expression\n";
            exit(1);
        }
    }

    void boostMatch(){
        //Define replace {FOR TESTING PURPOSES ONLY!!! REMOVE BEFORE SUBMITTING!!
        replace = "";
        _strdate_s(date);
        _strtime_s(time);
        lineNumber = 0;
        //Files to open
        //Input Files
        ifstream in("files/trff292010.csv");
            if(!in) cerr << "no file\n";
        //Output Files
        ofstream newFile("files/NEWtrff292010.csv");
        ofstream copy("files/ORIGtrff292010.csv");
        ofstream report("files/REPORT.dat", ios.app);
        lineNumber++;
        while(getline(in,line)){
            lineNumber++;
            boost::smatch matches;
            copy << line << '\n';
            if (regex_search(line, matches, expression)){
                for (int i = 0; i<matches.size(); ++i){
                    report << "Time: " << time << "Date: " << date << '\n'
                        << "Line " << lineNumber <<": " << line << '\n';
                    newFile << boost::regex_replace(line, expression, replace) << "\n";

                }
            }else{
                newFile << line << '\n';
            }
        }
    }

public:
    void run(vector<string> args){
        commandLine(vector<string> args);
        getExpression();
        boostMatch();
    }
};

ORIGINAL POST

I want to pass command line arguments out of main. This is homework for an advanced C++ class. I need to pass the command line with a vector and I am not sure if I am doing everything correctly. Would I pass it into a vector like I did? Also is there a copy() command you can use to copy the command line arguments into a vector rather than pushback?

main.cpp

#include "application.h"

int main(int argc,char *argv[]){
    vector<string> args;
    application app;
    for (int i=1;i<argc;i++){
        args.push_back(argv[i]);
    }
    app.run(args);
    return(0);
}

application.h

#include <boost/regex.hpp>
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    #include "time.h"
    using namespace std;

class application{
private:
    //Variables
    boost::regex expression;
    string line;
    string pat;
    string replace;
    int lineNumber;
    char date[9];
    char time[9];


    void commandLine(vector<string> args){
        string  expression="";    // Expression
        string  textReplace="";   // Replacement Text
        string  inputFile="";     // Input File
        string  outputFile="";    // Output Directory
        int optind=1;
        // decode arguments
        for(vector<string>::iterator i = args.begin(); i != args.end(); ++i){
            while ((optind < argc) && (argv[optind][0]=='-')) {
                string sw = argv[optind];
                if (*i == "-e") {
                    optind++;
                    expression = argv[optind];
                }
                else if (*i == "-t") {
                    optind++;
                    textReplace = argv[optind];
                }
                else if (*i == "-i") {
                    optind++;
                    inputFile = argv[optind];
                }
                else if (*i == "-o") {
                    optind++;
                    outputFile = argv[optind];
                }
                else{
                    cout << "Unknown switch: " 
                        << argv[optind] << "Please enter one of the correct parameters:\n" 
                        << "-e + \"expression\"\n-t + \"replacement Text\"\n-i + \"Input File\"\n-o + \"Onput File\"\n";
                    optind++;
                }
            }
        }
    }
    //Functions
    void getExpression(){
        cout << "Expression: ";
        getline(cin,pat);
        try{
            expression = pat;
        }
        catch(boost::bad_expression){
            cout << pat << " is not a valid regular expression\n";
            exit(1);
        }
    }

    void boostMatch(){
        //Define replace {FOR TESTING PURPOSES ONLY!!! REMOVE BEFORE SUBMITTING!!
        replace = "";
        _strdate_s(date);
        _strtime_s(time);
        lineNumber = 0;
        //Files to open
        //Input Files
        ifstream in("files/trff292010.csv");
            if(!in) cerr << "no file\n";
        //Output Files
        ofstream newFile("files/NEWtrff292010.csv");
        ofstream copy("files/ORIGtrff292010.csv");
        ofstream report("files/REPORT.dat", ios.app);
        lineNumber++;
        while(getline(in,line)){
            lineNumber++;
            boost::smatch matches;
            copy << line << '\n';
            if (regex_search(line, matches, expression)){
                for (int i = 0; i<matches.size(); ++i){
                    report << "Time: " << time << "Date: " << date << '\n'
                        << "Line " << lineNumber <<": " << line << '\n';
                    newFile << boost::regex_replace(line, expression, replace) << "\n";

                }
            }else{
                newFile << line << '\n';
            }
        }
    }

public:
    void run(vector<string> args){
        commandLine(vector<string> args);
        getExpression();
        boostMatch();
    }
};
  • 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-05-13T21:38:59+00:00Added an answer on May 13, 2026 at 9:38 pm

    I’d just write:

    vector<string> args(argv + 1, argv + argc + !argc);
    

    This will exclude argv[0], but in a way that’s robust even if argc == 0 (possible under Linux, and maybe other OSs too).

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

Sidebar

Related Questions

Okay I have updated my code quite a bit. I am getting a new
Okay, so not sure if apple.stackexchange is a better place for this, but I
Okay so i have this code: if (isset($_GET['book'])) { $query= SELECT book_id, title, authors.`author`
Okay, I have the following PHP code to extract an email address of the
I have the following code: void Foo() { static std::vector<int>(3); // Vector object is
Okay so I thought I had solved this problem. But the solution still evades
Okay, I have a problem with my code. I want to make some sort
Okay - I have a dilemma. So far my script converts page titles into
okay i have been trying to understand this for hours i am learning VB
Okay I have a large CRUD app that uses tabs with Forms embedded in

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.