Using command line parameters outside of main function

AI Thread Summary
The discussion focuses on how to read a command line parameter in C++ and use it outside the main function. The user initially struggles to pass the command line argument to a function defined outside of main. It is suggested that the user can declare a global variable and assign it within main, allowing later functions to access it. The conversation emphasizes the importance of understanding variable scope and function parameters in C++. Additionally, it encourages the user to explore C++ programming resources for better comprehension of the language's structure.
CAF123
Gold Member
Messages
2,918
Reaction score
87
I would like a program to read in a file entered by the user via the command line, which is then used in the main body of the code.
See example code below:

C++:
#include <iostream>
#include <fstream>

struct run_t {

  std::string file;

};

run_t run;

const POINTER* ptr = toy(run.file,0);

int main (int argc , char* argv[])
{
  run_t run;
  run.file = argv[1];
 
  std::cout <<  ptr->func(1,2,3) << std::endl;

  return 0;
}

The actual toy and func's are software specific and recognised by additional #include files. If I manually hardcode run.file in toy(run.file,0) with the string FILENAME, compile and execute with a single command line parameter (./exe 1, say) then the program works. My question is, how to modify the above code so that the value FILENAME entered in the command line is read in instead as run.file? That is, to make ./exe FILENAME work? I have tried with declaring argv[1] as the argument of toy but I have not yet got this to work.
 
Technology news on Phys.org
You are first constructing your run variable with default (empty) value for file and then you call toy(run.file), so no matter what you later assign to run.file it is already too late. In your main function you should probably 1) check argc that you have at least one parameter (argc >= 2), if so then 2) set ptr = toy(argv[1], 0), and 3) evaluate ptr->func.
 
Thanks for the reply! But do I understood you mean to just declare ptr in int main as follows?
C++:
int main (int argc , char* argv[])
{

  if (argc >= 2) {
  ptr = toy(argv[1], 0);
  };

  std::cout <<  ptr->func(1, 2, 3) << std::endl;

  return 0;
}
This of course works but actually I was wondering in general how to use the command line argument outside the main function (as I have other functions outside of main that need the value ptr->func)?
 
Last edited:
CAF123 said:
I was wondering in general how to use the command line argument outside the main function
In general you provide access to data via variables. These can either be global variables, function parameters or, since you are using C++, member variables. The usual pattern is to let the main method (or a sub-function) parse the arguments into whatever variables you need and then pass them in as arguments for functions or object constructors that need them. For a single string parameter what you have already done may be just fine, but otherwise there are also plenty of libraries that can assist you with a more flexible parsing.

However, like much else in C++ where there are so many way to structure your program depending on your exact requirements so it is a bit difficult to give an easy answer that magically will match what you need. I will suggest you search for and look over various relevant examples and then try code some of your own and return here with specific questions if it becomes tricky. Depending on how much C/C++you already know this will likely also require you to learn more about the language in general (e.g. different types of variables, how functions are structured, and such).
 
Thanks. Yes I had tried to access the argv[1] in the way you described through the struct, creating an object and then accessing the member. May I paste here a slightly updated version of the code in the OP:

C++:
#include <iostream>
#include <fstream>


struct run_t {


  std::string file;


};


run_t run;


const POINTER* ptr = toy(run.file,0);
//const POINTER* ptr = toy(FILENAME,0); //here hardcoded the FILENAME as a string => code works. I am trying to get it to work when I read in the filename from the first entry on the command line upon program execution

double Toy1(double a, double b, double c) {

return ptr->func(a,b,c);

};

double Toy2(double d) {

double factor = pow(d,2); //some dummy prefactor

return factor*Toy1(4,5,6);

};

int main (int argc , char* argv[])
{
  run_t run;
  run.file = argv[1];
 
  std::cout <<  Toy2(1) << std::endl;


  return 0;
}

So here I use the ptr in Toy1 and the result of that is then fed into Toy2 (with this I was trying to demonstrate why I didn't want to do simply the ptr->toy(argv[1],0) in main)
 
Note that while your "run" variable needs to be declared before the functions are defined (as you have also done), you can assign a value to it at any time. When such a variable is used in a function its value is only evaluated when the function is called (i.e. when the code in the function "runs past" where the variable is referenced), and not at the time when the function is defined. In your latest example that means you can declare the "run" and "ptr" variable and then assign a value to them later, e.g. in main, before calling your functions.

You confusion indicate that you likely lack basic understanding on how C/C++ or similar languages work in general so I again suggest you try to get some of that covered by, say, searching for some good beginners guide. If you already know a bit of programming with another language then you may benefit from trying to find a "Beginners guide to C++ for XXX-programmers" or something similar. I am sure others here more familiar with teaching C++ can also provide a good list of learning resources.
 
  • Like
Likes pbuk and CAF123
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top