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. C++ Program Parameter Problem

C++ Program Parameter Problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
7 Posts 4 Posters 0 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.
  • B Offline
    B Offline
    bneacetp
    wrote on last edited by
    #1

    int main(int argc, char* argv[])
    {
    switch(argc)
    {
    case 1:
    //.....
    break;
    case 2:
    if(argv[1] == "-help")
    {
    cout << "Help" << endl;
    }
    //...
    break;
    default:
    //... code...
    break;
    }

    return 0;
    

    }

    If the program in this case is named "prog.exe", when I type "prog.exe -help" into the command line, my understanding is that "Help" should be printed into the command line window. Any ideas as to why it isn't working? :confused: Also, if there is a better way to check to see which program parameters have been passed in than the way I am trying to do so, please let me know. I am open to suggestions. :)


    John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life."

    V D 2 Replies Last reply
    0
    • B bneacetp

      int main(int argc, char* argv[])
      {
      switch(argc)
      {
      case 1:
      //.....
      break;
      case 2:
      if(argv[1] == "-help")
      {
      cout << "Help" << endl;
      }
      //...
      break;
      default:
      //... code...
      break;
      }

      return 0;
      

      }

      If the program in this case is named "prog.exe", when I type "prog.exe -help" into the command line, my understanding is that "Help" should be printed into the command line window. Any ideas as to why it isn't working? :confused: Also, if there is a better way to check to see which program parameters have been passed in than the way I am trying to do so, please let me know. I am open to suggestions. :)


      John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life."

      V Offline
      V Offline
      valikac
      wrote on last edited by
      #2

      One solution is strcmp(). Kuphryn

      B 1 Reply Last reply
      0
      • V valikac

        One solution is strcmp(). Kuphryn

        B Offline
        B Offline
        bneacetp
        wrote on last edited by
        #3

        Thanks for the help! :D


        John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life."

        1 Reply Last reply
        0
        • B bneacetp

          int main(int argc, char* argv[])
          {
          switch(argc)
          {
          case 1:
          //.....
          break;
          case 2:
          if(argv[1] == "-help")
          {
          cout << "Help" << endl;
          }
          //...
          break;
          default:
          //... code...
          break;
          }

          return 0;
          

          }

          If the program in this case is named "prog.exe", when I type "prog.exe -help" into the command line, my understanding is that "Help" should be printed into the command line window. Any ideas as to why it isn't working? :confused: Also, if there is a better way to check to see which program parameters have been passed in than the way I am trying to do so, please let me know. I am open to suggestions. :)


          John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life."

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          bneacetp wrote: if(argv[1] == "-help") Herein lies your problem.

          B 1 Reply Last reply
          0
          • D David Crow

            bneacetp wrote: if(argv[1] == "-help") Herein lies your problem.

            B Offline
            B Offline
            bneacetp
            wrote on last edited by
            #5

            Yep. I agree. I changed

            if(argv[1] == "-help")

            to

            if(strcmp(argv[1], "-help") == 0)

            and that solved the problem. Thanks for taking the time to point that out. :)


            John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life."

            B 1 Reply Last reply
            0
            • B bneacetp

              Yep. I agree. I changed

              if(argv[1] == "-help")

              to

              if(strcmp(argv[1], "-help") == 0)

              and that solved the problem. Thanks for taking the time to point that out. :)


              John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life."

              B Offline
              B Offline
              Blake Miller
              wrote on last edited by
              #6

              Unless you REALLY need to look exactly for "-help" I would recommend a case insensitive string comparison. Not all users rememebr you want all lower case for command line arguments, for example - see stricmp

              B 1 Reply Last reply
              0
              • B Blake Miller

                Unless you REALLY need to look exactly for "-help" I would recommend a case insensitive string comparison. Not all users rememebr you want all lower case for command line arguments, for example - see stricmp

                B Offline
                B Offline
                bneacetp
                wrote on last edited by
                #7

                Alright. That certainly is a good suggestion. Thanks. :) I believe I will make that change. Thanks for taking the time to let me know about the stricmp() function. :)


                John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life."

                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