use argv while debugging
-
i'm just testing to run prog using parameters, but why i got error when debugging? screenshot[^] this is my code:
#include
int main (int argc, char * argv []) {
if (argc > 0) { std::cout << \*argv\[1\] << std::endl; } std::cin.ignore(std::numeric\_limits::max(), '\\n'); std::cout << "press ENTER to close program."; std::cin.get(); return 0;
}
-
i'm just testing to run prog using parameters, but why i got error when debugging? screenshot[^] this is my code:
#include
int main (int argc, char * argv []) {
if (argc > 0) { std::cout << \*argv\[1\] << std::endl; } std::cin.ignore(std::numeric\_limits::max(), '\\n'); std::cout << "press ENTER to close program."; std::cin.get(); return 0;
}
-
If you call the program via:
myprog.exe
then
argc
will equal1
, andargv[1]
will not exist, hence the exception. Remember, array indexes start from zero, not one.i know, but how to respond users if there's argument(s) submitted?
-
i know, but how to respond users if there's argument(s) submitted?
You have two choices: 1. Your program needs specific argument values which you must check by iterating over the items in
argv
. The actual number and type is for you to decide. 2. Your program does not accept command line arguments in which case you just ignore them. You may wish to display a warning message ifargc
is greater than 1. -
You have two choices: 1. Your program needs specific argument values which you must check by iterating over the items in
argv
. The actual number and type is for you to decide. 2. Your program does not accept command line arguments in which case you just ignore them. You may wish to display a warning message ifargc
is greater than 1.1. you mean like loop? i tried this but still error:
#include
int main (int argc, char * argv []) {
int itr = 0; if (argc > 0) { while (itr <= argc) { std::cout << argv\[itr\] << std::endl; ++itr; } } std::cin.ignore(std::numeric\_limits::max(), '\\n'); std::cout << "press ENTER to close program."; std::cin.get(); return 0;
}
2. so how to make my program accept argument(s)?
-
1. you mean like loop? i tried this but still error:
#include
int main (int argc, char * argv []) {
int itr = 0; if (argc > 0) { while (itr <= argc) { std::cout << argv\[itr\] << std::endl; ++itr; } } std::cin.ignore(std::numeric\_limits::max(), '\\n'); std::cout << "press ENTER to close program."; std::cin.get(); return 0;
}
2. so how to make my program accept argument(s)?
You did not mention what error you received, but I assume it is because you are still trying to access an item which does not exist. The values for
argc
andargv
are as follows: 1.argv
is an array of strings, which will always contain at least one item: the name of the executable that is used to initiate the program. 2.argc
contains the number of items in theargv
array. So it will always be at least 1. Let's look at a couple of examples: 1. A simple command line call to run my program, which is called Test.exe produces the following:C:\Users\user1\Documents\Code\C++>Test.exe
argc = 1
argv[0] = Test.exeThere are no input parameters,
argc
equals 1 as there is only one item inargv
, and that isargv[0]
which contains the program name. If I use the full path to run the program the output will be:C:\Users\user1\Documents\Code\C++>C:\Users\user1\Documents\Code\C++\Test
argc = 1
argv[0] = C:\Users\user1\Documents\Code\C++\TestIf we add some parameters to the command line we will get something like
C:\Users\user1\Documents\Code\C++>Test.exe one two "three and a half"
argc = 4
argv[0] = Test.exe
argv[1] = one
argv[2] = two
argv[3] = three and a halfSo
argv
now contains 4 items, the program name followed by each item that is separated by a space or tab. Not thatargv[3]
contains four words, since they were delineated by double quotes in the command line. The code to list these values is as follows:std::cout << "argc = " << argc << std::endl;
for (int i = 0; i < argc; ++i)
{
std::cout << "argv[" << i << "] = " << argv[i] << std::endl;
}Generally you are not interested in the program name so you can start the above loop from 1 rather than 0. The actual values in each parameter are for you to decide. You can use simple strings in a specific or random order, option letters or names preceded by single or double dashes or forward slashes:
Test check \foo\bar\filename.txt
Test -c C:\user1\Documents\file.jpg
Test --check somefilename... etc.
-
You did not mention what error you received, but I assume it is because you are still trying to access an item which does not exist. The values for
argc
andargv
are as follows: 1.argv
is an array of strings, which will always contain at least one item: the name of the executable that is used to initiate the program. 2.argc
contains the number of items in theargv
array. So it will always be at least 1. Let's look at a couple of examples: 1. A simple command line call to run my program, which is called Test.exe produces the following:C:\Users\user1\Documents\Code\C++>Test.exe
argc = 1
argv[0] = Test.exeThere are no input parameters,
argc
equals 1 as there is only one item inargv
, and that isargv[0]
which contains the program name. If I use the full path to run the program the output will be:C:\Users\user1\Documents\Code\C++>C:\Users\user1\Documents\Code\C++\Test
argc = 1
argv[0] = C:\Users\user1\Documents\Code\C++\TestIf we add some parameters to the command line we will get something like
C:\Users\user1\Documents\Code\C++>Test.exe one two "three and a half"
argc = 4
argv[0] = Test.exe
argv[1] = one
argv[2] = two
argv[3] = three and a halfSo
argv
now contains 4 items, the program name followed by each item that is separated by a space or tab. Not thatargv[3]
contains four words, since they were delineated by double quotes in the command line. The code to list these values is as follows:std::cout << "argc = " << argc << std::endl;
for (int i = 0; i < argc; ++i)
{
std::cout << "argv[" << i << "] = " << argv[i] << std::endl;
}Generally you are not interested in the program name so you can start the above loop from 1 rather than 0. The actual values in each parameter are for you to decide. You can use simple strings in a specific or random order, option letters or names preceded by single or double dashes or forward slashes:
Test check \foo\bar\filename.txt
Test -c C:\user1\Documents\file.jpg
Test --check somefilename... etc.
this is really embarrassing! this was my mistake... thank you very much! i just fix the code and now it's working![^] this is the final test code:
#include
int main (int argc, char * argv []) {
if (argc > 1) { std::cout << argv\[1\] << std::endl; } std::cin.ignore(std::numeric\_limits::max(), '\\n'); std::cout << "press ENTER to close program."; std::cin.get(); return 0;
}
really appreciate your reply!!
-
this is really embarrassing! this was my mistake... thank you very much! i just fix the code and now it's working![^] this is the final test code:
#include
int main (int argc, char * argv []) {
if (argc > 1) { std::cout << argv\[1\] << std::endl; } std::cin.ignore(std::numeric\_limits::max(), '\\n'); std::cout << "press ENTER to close program."; std::cin.get(); return 0;
}
really appreciate your reply!!