Can main function have default argument values? - c++

How can I put the default values for main function arguments like the user defined function?

Well, the standard says nothing which prohibits main from having default arguments and say you've successfully coalesced the compiler to agree with you like this
#include <iostream>
const char *defaults[] = { "abc", "efg" };
int main(int argc = 2, const char **argv = defaults)
{
std::cout << argc << std::endl;
}
Live example. It compiles with no errors or warnings, still it's useless; a futile experiment. It almost always would print 1.
Every time you invoke the program, say, with no arguments (or any number of arguments for that matter), argc gets set to 1 and argv[0] points to the program name, so doing it is pointless i.e. these variables are never left untouched and hence having defaults makes little sense, since the defaults would never get used.
Hence such a thing is usually achieved with local variables. Like this
int main(int argc, char **argv)
{
int const default_argc = 2;
char* const default_args[] = { "abc", "efg" };
if (argc == 1) // no arguments were passed
{
// do things for no arguments
// usually those variables are set here for a generic flow onwards
argc = default_argc;
argv = default_args;
}
}

I think you want to do two different things for the following cases.
When no arguments are passed
When arguments are passed.
Here is how you do it.
int main(int argc, char *argv[])
{
if(argc == 1)
{
// case #1
}
else
{
// case #2
}
}

Using argc and argv? Thoses will pass argument from the command line to your program. You can't really use default arguments. You have to pass them during the call to your program like this :
$> ./my_addition "4" "7"
int main(int argc, char *argv[])
{
// argc <=> 'argument count' (=3)
// argv <=> 'argument vector' (i.e. argv[1] == "4")
// argv[0] is usually the bin name, here "my_addition"
for (int i = 0; i < argc; ++i)
std::cout << argv[i] << std::endl;
return (0);
}
Maybe you could use a script to run your program, this could maybe be the closest solution to default argument for main().
exec_my_prog.sh:
#!/bin/zsh
call_your_program + very_meny_args
And calling ./exec_my_prog.sh would run your program with the "default" arguments.

Related

How to point char *argv[] from Main function to the table

I will try to explain what I want to do. I've got the program (which doesn't work), which is written to be called from CMD console in windows. I'm using parameters from main function
int main(int argc, char *argv[])
But in this case I can't use the debugger to find what i'm doing wrong...
Am I able to somehow connect char *argv[] argument to created table in code ?
Example:
int main(int argc, char *argv[])
{
char tablica[] = { 'K','2','+','1','-','3','*','(','3','+','2',')','*','2' };
tablica = **argv; //// IDK HOW TO CONNECT THESE TWO
here's how you can fake argument passing and substitute with your array
#include <stdio.h>
int main(int argc, char* argv[]){
char *tablica[] = { argv[0],"K","2","+","1","-","3","*","(","3","+","2",")","*","2" };
int i;
argv=tablica;
argc=sizeof(tablica)/sizeof(tablica[0]);
for (i=0;i<argc;i++)
{
printf("%s\n",argv[i]);
}
return 0;
}
result:
your_executable
K
2
+
1
-
3
*
(
3
+
2
)
*
2
notes: in order to be compliant with argc,argv:
I have changed the array of chars to an array of char * (single quotes => double quotes). That seems logical, since without that you could not pass for instance numbers > 9 in your arguments!
I have overridden argc as well (automatic computation from your array)
I have inserted program name in your array, else it would shift argument parsing.
You can do better by separating your code into a testable function. This will allow you to write a unit test project to see if it behaves correctly on known inputs (as you have already prepared), or call it directly from main with argc, argv.
// your code
int your_code (int argc, char* argv[]);
// can be called from main
int main(int argc, char* argv[]){
your_code(argc, argv);
return 0;
}
// or can be tested
void unit_test() {
char * tablica[] = ...;
int arg_count = sizeof(tablica) / sizeof(tablica[0]);
your_code(arg_count, tablica);
}
Note that your code currently assumes that each character is a new element. This prevents numbers larger than 9. If you're committed to that a string does this for you pretty simply.
const auto tablica = string(argv + 1, argc - 1) + "K2+1-3*(3+2)*2"s
Or if you wanted your command line arguments as the suffix:
const auto tablica = "K2+1-3*(3+2)*2"s + string(argv + 1, argc - 1)
If you need to dump this into a separated list you can do:
copy(cbegin(tablica), cend(tablica), ostream_iterator<char>(cout, "\n"))

how to read input files taken as parameters in C++

I've just practiced coding C++. I knew in Java, we could have a public main method, which could read input file names as parameters of the string array argument. But I wonder how I do the same task in C++?
Both Java and C++ follow the same C-like syntax. So it doesn't really differ from what you had in Java. In Java you had a string class :
class Test {
public static void main(String args[]) {
for(int i = 0; i < args.length; i++)
System.out.println("Argument " + i + " = " + args[i]);
}
}
C/C++ mostly use primitive character arrays in order to store strings. Although Standard Template Library also provides string classes, but C++ uses native char arrays to store commandline arguments. The main function takes two variables :
int argc : number of commandline arguments
char *argv[] : an array of character strings
You can also say it can be written as char **argv, because of the underlying representation of two dimensional arrays in C/C++, but both mean the same thing. The equivalent of the above code in C++ would be:
#include <iostream>
int main(int argc, char *argv[]) {
for(int i = 0; i < argc; i++)
std::cout << "Argument " << i << " = "
<< argv[i] << std::endl;
return 0;
}
You do it the same way, with slightly different syntax because C arrays do not store their length, so it is passed as a separate parameter.
int main(int argc, char** argv) {
// Read args from argv, up to argc args.
// argv[0] is the name of the program
// argv[1] is the first argument
}
The main function gives you the argument count and the actual arguments as an array of character arrays.
To safely work with this, you should first turn this information into a std::vector<std::string>.
#include <string>
#include <vector>
int main(int argc, char *argv[]) {
std::vector<std::string> arguments;
for (int index = 0; index < argc; ++index) {
arguments.push_back(argv[index]);
}
}
You will notice that arguments[0] is equal to the filename of the executable (in theory, this depends on the system you're using). If you are on Windows and have an executable called stackoverflow.exe, then starting it with
stackoverflow.exe one two
would result in arguments containing { "stackoverflow.exe", "one", "two" }.

how can I test argc and then assign default values to argv[1]?

I need to give default behavior to a command line app if no arguments are entered.
If no arguments are entered, I need the program to set argv[1][0] = '1' and argv[1][1] = '\0' for the null terminator.
I keep getting a core dump when I try to compile my code in g++, and here is what is causing the issue:
int main(int argc, char * argv[]){
//for testing we put some dummy arguments into argv and manually set argc
//argc = 1;//to inlcude the program name
//we put a defualt value into argv if none was entered at runtime
if(argc == 1){
argv[1][0] = '1';
argv[1][1] = '\0';//add a null terminator to our argv argument, so it can be used with the atoi function
}
Also, I am not on C++ 11.
RE-FACTORED CODE: (Which basically just codes around the issue so that we don't have to manipulate argv[] in the main function)
int argvOneAsInt;
if(argc != 1){
argvOneAsInt = atoi(argv[1]);//use atoi to convert the c-string at argv[1] to an integer
}
else{
argvOneAsInt = 1;
If argc equals 1, then the second value in the array argv is NULL. You are dereferencing that NULL pointer right here:
argv[1][0] = '1';
Instead of trying to manipulate argv, rather change the logic in the code. Use an array you control in memory, copy argv to it and then manipulate the array.
This all looks rather dodgy. I would probably do something like this:
int main(int argc, char* argv[])
{
std::string arg1 = "1"; // set default
if(argc > 1) // override default if present
arg1 = argv[1];
// Now use arg1 and forget about argv[]
}
just to support your question, what ever you wanted was not faulty but you forgot to allocate memory where you wanted to assign your values.
Check this:
#include <string.h>
#include <malloc.h>
using namespace std;
int main(int argc, char * argv[]){
//for testing we put some dummy arguments into argv and manually set argc
//argc = 1;//to inlcude the program name
//we put a defualt value into argv if none was entered at runtime
if(argc == 1){
argv[1] = (char*)malloc(strlen("1\0"));
argv[1][0] = '1';
argv[1][1] = '\0';
//argv[1][2] = '\0';
//argv[1] = '\0';//add a null terminator to our argv argument, so it can be used with the atoi function
}
}
now it should work the way you want.

Checking arguments in terminal

How can I write a program to check the arguments in the terminal are correct?
For example, if I have a program hello.cpp and I want to call it as:
./hello yes 10
I want the program to make sure that the first argument is yes or no and the second argument is a number between 1-10. So how can I read these arguments into my program to do the checking?
Thanks!
Command line arguments are passed as a count and individual strings in the argc and argv arguments to main().
int main(int argc, char *argv[])
{
...
}
Simply check the value in argc and the strings in argv for the appropriate values.
You meant to execute like this, ./hello yes 10
there is an option argc and argv in c
where argc is the number of arguments passed and argv with the index shows the argument passed itself.
Take a look at the below code for iterating through all arguments.
int main(int argc, char *argv[]){
int i = 0;
for (i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
return 0;
}
As mentioned by other users, The main function is the entry point of your program, and the way it gets data from the command line is through its parameters.
The first int argument is the count of all the arguments passed, including the program name, the second char ** argument is a pointer to each parameter passed, including the program name:
int main
(
int argc, // <-- how many parameters has been provided?
char **argv, // <-- what values has each parameter?
)
{
...
return 0;
}
So, knowing that, your call ./hello yes 10 must be like that:
argc = 3
argv[0] = "./hello"
argv[1] = "yes"
argv[2] = "10"
The names argc and argv are just a convention, you can name them at your pleasure, but it's a good practice to keep the names that everyone are used for.
And the argument doesn't are forced to be int, char ** they must follow a quite rigid convention, borrowed from this answer:
It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both the following definitions of main: int main() and int main(int argc, char* argv[])
Knowing that, let's focus on your question:
First of all, you must ensure that 2 arguments are passed, so you must check the argc value and ensure that equals exactly 3.
the first argument is yes or no
Next, you must store your argv[1] (because 0 contains the program name) into a string and compare it with the values "yes" and "no":
std::string YesOrNo(argv[1]);
if (YesOrNo == "yes" || YesOrNo == "no")
And finally, you must store your argv[2] into an integer and check if it is equal or less to 10:
std::stringstream Stream;
int Value = 0;
Stream << argv[2];
Stream >> Value;
if (Value <= 10)
So, the result is:
int main(int argc, char **argv)
{
if (argc == 3)
{
std::string YesOrNo(argv[1]);
if (YesOrNo == "yes" || YesOrNo == "no")
{
std::stringstream Stream;
int Value = 0;
Stream << argv[2];
Stream >> Value;
if (Value <= 10)
{
// Your stuff..
}
}
}
return 0;
}
I let you deal with all the uppercase and lowercase stuff and the false positives with the numeric argument, at least I'm not going to do all your homework ;)

Segmentation Fault With argv

I have a program that reads in a single argument from the command line and performs certain operations on it. I'm using argv and argc. When I fail to pass an argument to the program, it segfaults. I've tried checking if argc isn't a certain value and then printing the value out, but it still segfaults. Here's the code in question. Note that it works as expected when passed a single argument. Here's the code in question:
int main(int argc, char *argv[])
{
int numTimes = atoi(argv[1]); //converts content of argv[1] into integer
if(argc != 2)
{
printf("Enter a valid integer.");
}
You need to check argc before you try to access that argument. Just move the argc test to sometime before before you call atoi(argv[1]).
Just check the number of arguments before trying to accessing a specific element. Something like this:
int main(int argc, char *argv[])
{
if(argc < 2)
{
printf("Enter a valid integer.");
return 0;
}
int numTimes = atoi(argv[1]); // now we're sure to have at least 1 argument passed
// ...
}
You have to do the check before attempting to access the arguments.