• This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #194
    admin
    Keymaster

      Array argv[] will be of length 2.

      argv[0] that will hold ./caesar.c

      argv[1] will contain the key value

      So to begin with, I perhaps need to check if indeed two inputs are entered by the user.

      Here is the code I tried:

      
      
      int main(int argc, string argv[])
      {
          if (argv[]!=2)
          {
          printf("enter key value");
          return 0;
          }
      ............
      }

       

      Fail to figure out what is wrong with the above code.

      Will it be:

      
      
      int main(int argc, string argv[])
      {
          if (int argv[]!=2)
          {
          printf("enter key value");
          return 0;
          }
      ............
      }

      The above code too appears faulty.


      Reply


      argv, as you stated, holds the input parameters, so what you want to do is check if the length of argv is different from two, not if argv itself is different from two. Luckily, the other parameter argc denotes this value – it corresponds to the amount of arguments provided. So try checking if argc =! 2 instead 🙂


      Query


      Out of curiosity or for the sake of learning, is it possible to check if indeed two parameters with argv[] on that line.


      Reply


      You could try error handling with a try and catch. Not something I’m that experienced with. But seems like something to use if you try accessing a third value in an array when there’s only two.

      I would just use argc[] since it’s already known.


      Yes, the number of parameters corresponds to the length of argv, so you can check if the length of argv =! 2


      Number of inputs is stored in argc. Thus to limit the number of inputs, you gotta tweak with argc and not argv[]


      https://edstem.org/us/courses/176/discussion/1094835

      [learn_press_profile]

    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.
    Scroll to Top