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 :)
2 Answers
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[0] = "./caesar", typing "./caesar 13" would make argv[0] = "./caesar" and argv[1] = "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 :)
-
Thanks! I was actually getting the segmentation fault error the first time I used the int key = atoi(argv[1]) and I thought the problem is that I haven't provided any input. But i forgot that I actually need to type in the terminal ./caesar 13 (as you suggested). i was typing ./caesar but I thought that maybe I need to provide the input in the program itself (to prompt the user to do it).– GiPCommented Nov 23, 2014 at 15:53
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 string
s. 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 int
value.
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!
-
Thank you! it was indeed really confusing for me why at first the key is a string when you can simply get an int input from the user. now it doesn't seem that pointless :)– GiPCommented Nov 23, 2014 at 15:55
-
@GiP you may accept this answer if it answers your question! Thank you!– kzidaneCommented Nov 23, 2014 at 16:53