IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2: [Arrays] – Functions, Variable and Scope, Debugging, Arrays, and Command Line Arguments › Understanding command line interface with Caesar project
Tagged: arg c, arg v, Caesar project, cli, command line interface in c
- This topic is empty.
-
AuthorPosts
-
January 28, 2022 at 7:10 am #174
Initially, I approached Caesar project ignoring command line argument.
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#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[i]))//if (isupper(name[i])) { name[i] = ((((name[i] - 65) + key)%26) + 65);//newuppercase_array[i] = ((((name[i] - 65) + key)%26) + 65); } else if (islower(name[i])) { name[i] = ((((name[i] - 97) + key)%26) + 97);//newuppercase_array[i] = ((((name[i] - 97) + key)%26) + 97); } else { name[i] = name[i]; //newuppercase_array[i] = name[i]; } printf("%c",name[i]);// printf("%c",newuppercase_array[i]); } printf("\n"); }
[/dm_code_snippet]
Now, it appears I need to modify[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
int main(void)
[/dm_code_snippet]
to something like[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
int main(int arg c, string arg v [])
[/dm_code_snippet]
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 [i]?
Reply
argv[] is an array of strings:[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
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
[/dm_code_snippet]
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
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#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[i]))//if (isupper(name[i])) { name[i] = ((((name[i] - 65) + key)%26) + 65);//newuppercase_array[i] = ((((name[i] - 65) + key)%26) + 65); } else if (islower(name[i])) { name[i] = ((((name[i] - 97) + key)%26) + 97);//newuppercase_array[i] = ((((name[i] - 97) + key)%26) + 97); } else { name[i] = name[i]; //newuppercase_array[i] = name[i]; } printf("%c",name[i]);// printf("%c",newuppercase_array[i]); } printf("\n"); }
[/dm_code_snippet]
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[i] 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 🙂
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
int main(int argc, string argv[])
[/dm_code_snippet]
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:
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
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)
[/dm_code_snippet]
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):
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
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[i]))//if (isupper(name[i])) { enteredtext[i] = ((((enteredtext[i] - 65) + key)%26) + 65);//newuppercase_array[i] = ((((enteredtext[i] - 65) + key)%26) + 65); } else if (islower(enteredtext[i])) { enteredtext[i] = ((((enteredtext[i] - 97) + key)%26) + 97);//newuppercase_array[i] = ((((enteredtext[i] - 97) + key)%26) + 97); } else { enteredtext[i] = enteredtext[i]; //newuppercase_array[i] = enteredtext[i]; } printf("%c",enteredtext[i]);// printf("%c",newuppercase_array[i]); } printf("\n"); }
[/dm_code_snippet]
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:
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
./caesar 10
[/dm_code_snippet]
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:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
./caesar
[/dm_code_snippet]
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]
-
AuthorPosts
- You must be logged in to reply to this topic.