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

       

      
      
      #include<stdio.h>
      #include<cs50.h>
      #include<ctype.h>
      #include<stdlib.h>
      #include<string.h>
      int main(int argc, string argv[])
      {
      string enteredtext = argv[2];
      printf("Entered text by user: %s\n", enteredtext);
      int n = strlen(enteredtext);
      printf("length of entered text: %i\n", n);
      string key = argv[1];
      int convertedkey = atoi(argv[1]);
      int convertedtext = atoi(argv[2]);
          for (int i = 0; i < n; i++)
          {
              if (isupper(enteredtext))//if (isupper(name))
              {
              enteredtext = ((((enteredtext - 65) + convertedkey)%26) + 65);
              }
              else if (islower(enteredtext))
              {
              enteredtext = ((((enteredtext - 97) + convertedkey)%26) + 97);
              }
              else
              {
              enteredtext = enteredtext; //newuppercase_array = enteredtext;
              }
          printf("%c", enteredtext);
          }
      printf("\n");
      }

      The above code though compiling while running showing segmentation fault.

      Particularly, facing hurdles for the data type of key. It was initially int type. But in order to make the program run through command line interface, it was then made string type:

      string key = argv[1];

      Since the later part of the program will use the key as int type as arithmetical calculation needs to be done, I formed another variable convertedkey.

      Seeking help particularly the way I am handling key variable is correct or incorrect.


      Reply


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


      Hmm, you are storing the content of argv[1] & argv[2] in variables… but have you thought what will happen if either of these information isn’t provided at runtime? You’ll need to check if anything exists in argv before assigning it to a variable & converting it to an int.

      Now, I’ve not tested it myself but if you want you can, try commenting out atoi parts of your code, your segmentation will disappear. But ofcourse, this will not make your program work

      Reason for segmentation fault can be many but one which I can think of is when you try to access something which isn’t there. Suppose, there’s an array of 3 elements, if you try to access the element 3 (array[3]) segmentation fault may happen… at least in C other languages will smack you with arrayindexOutOfBound error (correct me if I am wrong, if someone else know more about it, do let me know!)


      Query


      Not clear what is meant by ” storing the content of argv[1] & argv[2] in variables”. To my little understanding, I need to store value of argv[1] which is key somewhere. Same for argv[2], which is the text that the user typed.


      Reply


      You’re right. You need to store both the key & plain text, but what you don’t need to do is to receive plain text at runtime. You should only use command line arguments for receiving the key, plain text is prompted when your program is running. It’s explained in problem description:

      You don’t need to pass the text as a command line argument, you can prompt for it using get_string function afterwards.


      Query


      Also, got an opinion that “There should not be an argv[2]. There are only 2 command line arguments, indices 0 and 1” (https://edstem.org/us/courses/176/discussion/1078125).


      Reply


      There can be as many command line arguments as you want, however, for this problem you don’t need more than one. And that one is the key, i.e, argv[1]


      Query


      After going through https://cs50.harvard.edu/x/2022/psets/2/caesar/ once again, now I realize that indeed only key ar argv[1] needed. Thanks for pointing it out.[learn_press_profile]

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