I know that the answer seems obvious but I will explain further the reason for my question: in my program I only used for the key int k = GetInt() without the argv, argc etc. and the program seems to work. Maybe I am missing something and I admit I still struggle to undestand the argv and argc (I watched the shorts and walkthroughs, I/m just trying to understand their point because I haven't seen them in other programming languages).Why do we need to convert the key to int after the user input when we can simply store it as an int? Thank you in advance :)
In this problem set, you are not asked to get input from the user after the program starts but rather get it using command line arguments. command line arguments are basically just input that the user can provide before running the program by typing it after the program name (e.g. ./caesar 13). To allow your program to make use of such input, the "main" function shouldn't take in (void) but you'd want it to take in strings (typed after the program name). you do that with two variables, the number of the arguments , usually given argc (argument coun) but you can call it whatever you like (e.g. typing "./caesar" in the commandline would make argc = 1, while "./caesar 13:-> argc = 2, "./caesar 13 hello" -> argc = 3, and so forth), and an array of strings representing the arguments in the commandline (e.g. typing "./caesar" would make argv = "./caesar", typing "./caesar 13" would make argv = "./caesar" and argv = "13" and so on) so your main function becomes something like this : int main(int argc, string argv) since commandline arguments (argv) can only take strings, you need to convert that string into arithmetic numbers in order to use it as such. The atoi (alphabetical to integer) does that for you where it takes in any string and converts the numbers inside the string into arithmetic numbers. Hope I've cleared this up for you, and good luck :)
You aren't supposed to take input from the user using
GetInt. Rather, you are supposed to take input as command-line arguments.
As for your question
Why do we need to convert the key to int after the user input when we can simply store it as an int?
Simply because command-line arguments are
strings. And since you need your key to be an
int, you have to convert it to an
int. One way to do that is using a function from the standard library, namely,
atoi. This function does exactly that for you — it takes in a
string, converts it to an
int, then returns that
In case you're still wondering why we need to convert the key from a
string to an
int, well clearly because we use that key in arithmetic expressions and we'd get a type-checking error if we tried to use a
string in such expressions in C.
In case you're wondering about the magic behind
atoi and how it converts a
string to an
int, you could do a similar job manually by your own (though it's not required because you already have a pre-defined function that does that for you) and this is another way to do it
int n = 0; for (int i = strlen(key)-1 , p = 1; i >= 0; i--, p *= 10) n += (key[i] - '0') * p;
Hope that helps!