Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. use argv while debugging

use argv while debugging

Scheduled Pinned Locked Moved C / C++ / MFC
comtestingbeta-testinghelpquestion
8 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chipp_zanuff
    wrote on last edited by
    #1

    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;
    

    }

    L 1 Reply Last reply
    0
    • C chipp_zanuff

      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;
      

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      If you call the program via:

      myprog.exe

      then argc will equal 1, and argv[1] will not exist, hence the exception. Remember, array indexes start from zero, not one.

      C 1 Reply Last reply
      0
      • L Lost User

        If you call the program via:

        myprog.exe

        then argc will equal 1, and argv[1] will not exist, hence the exception. Remember, array indexes start from zero, not one.

        C Offline
        C Offline
        chipp_zanuff
        wrote on last edited by
        #3

        i know, but how to respond users if there's argument(s) submitted?

        L 1 Reply Last reply
        0
        • C chipp_zanuff

          i know, but how to respond users if there's argument(s) submitted?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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 if argc is greater than 1.

          C 1 Reply Last reply
          0
          • L Lost User

            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 if argc is greater than 1.

            C Offline
            C Offline
            chipp_zanuff
            wrote on last edited by
            #5

            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)?

            L 1 Reply Last reply
            0
            • C chipp_zanuff

              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)?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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 and argv 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 the argv 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.exe

              There are no input parameters, argc equals 1 as there is only one item in argv, and that is argv[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++\Test

              If 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 half

              So argv now contains 4 items, the program name followed by each item that is separated by a space or tab. Not that argv[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.

              C 1 Reply Last reply
              0
              • L Lost User

                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 and argv 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 the argv 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.exe

                There are no input parameters, argc equals 1 as there is only one item in argv, and that is argv[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++\Test

                If 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 half

                So argv now contains 4 items, the program name followed by each item that is separated by a space or tab. Not that argv[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.

                C Offline
                C Offline
                chipp_zanuff
                wrote on last edited by
                #7

                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!!

                L 1 Reply Last reply
                0
                • C chipp_zanuff

                  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!!

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  You are welcome.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups