Hot answers tagged

6

GAR, figured it out. I didn't take the details of "your program should yell at you" literally. This works now. if (argc != 2) { printf("YOU SCREWED UP!"); return 1; }


5

Yes, you are correct. char* text = (argc == 3) ? argv[2] : argv[1]; actually means char* text = ((argc == 3) ? argv[2] : argv[1]); i.e. assign some value to char *text with the help of Ternary Operator. Ternary operators work as some_condition ? execute_this_if_true : execute_this_if_false ; Here, firstly, argc == 3 is evaluated. Depending on ...


4

You have popped with many questions at the same time. Lets start with the simplest one. What's the relation between return value of a function and command line arguments? You don't return 1 because you have 1 command line argument. The return value of any function(including main()) is almost independent of the arguments passed (unless you do something ...


3

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" ...


3

If I understand your question correctly, and it's "what is the role of argc" the answer is that we need it to know how many arguments were passed to main from the command line. So if the command was ./hello-4 Zamyla we have argc == 2 and argv[0] == "./hello-4" and argv[1] == "Zamyla". Without argc you wouldn't know the length of argv.


3

What do argc and argv have to do with the subject of cryptography? Nothing. But they DO have a lot to do with how you get data into a program when it is started. Just in case you didn't comprehend it, here's what those two items do. argc is a built-in variable in C, one of the few. It tells the program the number of parameters that were in the call to ...


2

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 ...


1

There are a few problems here. First, it's checking whether a string, argv[1], is greater than 0, which will always be true, no matter what the number that results after the atoi() call may be. All ASCII chars are nonnegative numbers. Next, the code has a mechanism to prompt for a new key if the one given as a parameter is invalid. However, if a bad key is ...


1

Because the arguments passed to your program are only 2: the name of your program (./arg-0) the argument you pass to it (pri). The > file.txt part of the command is used by the command line to redirect the standard output of your program, and is not passed as an argument in your program.


1

Well your program is correct, in the sense that it doesn't have any compiling errors, but it doesn't do anything. It just checks whether the user gave 2 arguments or not. You are correctly returning 1 in case he provides any other number of arguments except from 2 (you can use echo $? to see this) but that is not all the program should do. Also you should ...


1

Your program is actually working as designed. What you're probably overlooking is how the OS shell is handling the inputs to the program. Single quotes, double quotes and the single back quote (the ` and ~ key below the escape key on non-apple keyboards) are treated differently. They would be used, for instance, to identify a complete string. As an example, ...


1

If you're trying to printf an apostrophe, or other special characters in C, you need to enter the proper escape sequence. You've already seen an example of this...the \n that you have to type in order to enter a newline character. For an apostrophe, the character is \' To do a check on an apostrophe, you would want to do something like the following: if ( ...


1

I had the same error until I moved my declared variables AFTER the if... if (argc !=2) { printf("Oops! Only 2 arguments allowed"); return 1; } int k = atoi(argv[1]); string p;


1

Does anybody know why? Because you're trying to access a string that doesn't really exist in case it's not passed as a command-line argument to your program! So you should first check whether you get the correct number of command-line arguments passed to your program then start dealing with them. Also, not really sure why I should return 1 if an ...


1

Mate, I understand at times silly things turns frustrating. Especially when working on big project. But you need to take care of small things. As I noticed in the following line int key = atoi(argv[1]) you forgot semi-colon. Please correct the line as follows int key = atoi(argv[1]); Next, assume that user simply forgot about the command line arguments. ...


Only top voted, non community-wiki answers of a minimum length are eligible