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

      Initially, I approached Caesar project ignoring command line argument.

      
      
      #include<stdio.h>
      #include<cs50.h>
      #include<string.h>
      #include<ctype.h>
      int main(void)
      {
      string name = get_string("Enter: ");
      printf("Entered text by user: %s\n", name);
      int n = strlen(name);
      printf("length of entered text: %i\n", n);
      int key = get_int("enter key: ");
      //char newuppercase_array[n];
          for (int i = 0; i < n; i++)
          {
                if (isupper(name))//if (isupper(name))
               {
                name = ((((name - 65) + key)%26) + 65);//newuppercase_array = ((((name - 65) + key)%26) + 65);
               }
              else if (islower(name))
              {
               name = ((((name - 97) + key)%26) + 97);//newuppercase_array = ((((name - 97) + key)%26) + 97);
              }
              else
              {
               name = name; //newuppercase_array = name;
              }
             printf("%c",name);// printf("%c",newuppercase_array);
          }
          printf("\n");
      }

       

      Now, it appears I need to modify
      to something like
      
      
      int main(int arg c, string arg v [])
      

      Also, need to remove:
      string name = get_string(“Enter: “);
      int key = get_int(“enter key: “);
      Since there are two inputs, will arg c be 2?
      Unable to figure out what will be arg v[] for the above code? Though seems unlikely, will it be equal to name ?

      Reply


      argv[] is an array of strings:
      
      
      argv[0] is the command itself, for example "./caesar"
      argv[1] is the first argument/parameter, for example "11"
      argv[2] is the second ...... etc

      So yes, if program is started correctly, then argc is 2. You will need to verify that in your code.

      Remember that you are receiving the arguments as strings! You will need to verify the input is correct format and a valid number.


      Query


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

       

      int main(int 2, string argv[3])

      Is my formatting correct for the Caesar program that will have two inputs from the user at command line interface, resulting in three argv[3].

      argv[0] will be the command ./casear (as entered by the user while running the program).

      argv will be the key entered by the user on command line interface

      argv[2] will be the plain text entered by the user on command line interface.


      Reply


      Not entirely 🙂

      
      
      int main(int argc, string argv[])
      

      Then you can in your code check if argc is 2. If it is then and only then you can use argv[1]. You will have to check if argv[1] is correct, if it is all digits. The function isdigit(..) can be helpful here. Look up that function here: https://manual.cs50.io/3/isdigit

      Query


      I believe within the main, I need to include details about argv which will take care of argc. Will it be in rough pseudocode be:

      arg v[0] = ./caesar

      argv[1] = key (which will be an int)

      argv[2} = text to be entered by user on CLI


      Reply


      Above in my example argv[2] was just to show in case there are more arguments. In this case you will have to do:

      
      
      1. check if argc is 2
         if yes, all fine
         if no, exit program
      2. check if argv[1] really is a number
         if no, exit program
         if yes, all fine
      3. convert the key from string to int
         (remember argv is array of strings)

      If you try to access argv[1] and there is no key (in case the user just started the program like this: “./caesar”) you will get a “segmentation fault” and your program will crash 🙂 So always first check if argc is as expected!


      Query


      Here is my revised code (with faults):

      
      
      include<stdio.h>
      include<cs50.h>
      include<string.h<
      include<ctype.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 isdigit(string key);
          for (int i = 0; i < n; i++)
          {
              if (isupper(enteredtext))//if (isupper(name))
              {
              enteredtext = ((((enteredtext - 65) + key)%26) + 65);//newuppercase_array = ((((enteredtext - 65) + key)%26) + 65);
              }
              else if (islower(enteredtext))
              {
              enteredtext = ((((enteredtext - 97) + key)%26) + 97);//newuppercase_array = ((((enteredtext - 97) + key)%26) + 97);
              }
              else
              {
              enteredtext = enteredtext; //newuppercase_array = enteredtext;
              }
              printf("%c",enteredtext);// printf("%c",newuppercase_array);
          }
          printf("\n");
      }

      Particularly,

      string key = argv[1];

      int isdigit(string key);//convert datatype of key from string type to int type

      Is the above code in the right direction or conceptually faulty?

      Also, any more guidance on the code appreciated.


      Reply


      Remember that the first element of an array has index 0.

      The program is started correctly like this:

      In this case argv[0] is “./caesar” and argv[1] is “10”. There is no argv[2].

      But what if the user just starts the program like this:

      In this case the program is not started correctly. You will need to detect that in your code, tell the user that this is wrong and exit the program. If you try to access argv[1] in this case, the program will crash because argv[1] does not exist!

      You will need to check if argc is correct BEFORE you even consider touching argv 🙂

      Read the manual page on isdigit(). You will see that this function checks one character, not a string.


      If I remember correctly, caesar asked for number in the ./caesar () and then plaintext. The output of the program is the ciphertext. If what I am remembering is correct, you take the argv() and transfer it to “key” you have in your code then you can output your ciphertext base on the numbers entered. Would take a look at all the cs50 manual to see what sort of #include does that for you.[learn_press_profile]

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