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 › Command Line Interface with Caesar project: Understanding argc and argv
- This topic is empty.
-
AuthorPosts
-
January 30, 2022 at 4:49 am #176
[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<ctype.h> #include<cs50.h> #include<string.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]
nt 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
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
string name = argv(2);
[/dm_code_snippet]
Also note that subscripting into an array is done with square brackets:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
string name = argv[2];
[/dm_code_snippet]
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
int main(int 2, string argv[3])
[/dm_code_snippet]
One thing you may be missing here is that you are writing a function (main) that will be passed some parameters. You don’t know, when you write the code, what will be passed. You don’t know that it will be 2 for argc (and it’s really 3, but you don’t know that either).
You define the function to take a parameter, and at run time that variable is filled in for you. Your code can then examine the value and make decisions based on that.
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
int main(int argc, string argv[])
[/dm_code_snippet]
So here you have named the parameter argc, and it will be assigned a value by the system just before your program runs. You can think of it as the command line calling your main() function.
Memory for argv automatically allocated by the compiler (correct me if I’m wrong) you don’t need to allocate space yourself. Just leave the brackets empty without space. Besides, why is there 2 after int? You’ve to initialise a variable there which will store the number of arguments passed to the program… C variables can only start with letters or underscores, conventional name for it is argc though you can name it whatever you want
As all other comments have said, I’ll repeat with less jargon.
When you create a function with parameters, do you give it hard coded literal values? Or give it the data type (which you did) and a “variable place holder”? (Which you did not).
argc can be any number and argv can have any size.
You would write:
int addSum(int x, int y)
as opposed to:
int addSum(int 2, int 3)
Didn’t want to use int main as an example since the others already did. But they basically gave you the answer.
[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.