Hot answers tagged

7

main in Python no longer has a special meaning, but is a regular function, its name only chosen to conform to conventions based in C. Try exit(1) instead of return 1. Relevant python docs: https://docs.python.org/3/library/sys.html#sys.exit


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; }


6

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


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


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


2

Actually, it's not always changing to 1 on the last line. The result is always dependent on the input to argv[1]. Specifically, the last number printed depends on where the first 'a' appears in the input string. Here's what's happening. In the last printf() call, strlen(argv[1]) is measured by the appearance of the end of string marker in the string. But, ...


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


2

you need to follow the pset specs literally as it's machine-graded. if argv[1]<>1, you need to return 1 right away instead of giving an error message


1

Its a simple and common issue with new programmers. ;-) Yes, argv[] is an array of strings. So, argv[1] is a string. Now, say that argv[1] contains the string "cat". Then argv[1][0] contains the char 'c', argv[1][1] contains 'a', argv[1][2] contains 't' and argv[1][3] contains the end of string marker, '\0'. That last one is very important, ...


1

I have no idea what this code is doing. But that doesn't really matter. You said that the intent is to test the chars in argv[1] to see if they're digits. Well, there's absolutely no code here to do that. I don't see any calls to isdigit() or anything that checks argv[1][i]. Looks like there's a bunch of missing code here that has yet to be added. ;-) If ...


1

It's because you have return 1; after your if statement, that will just exit the program in that line and will not proceed to the next lines. That return should be inside your if statement therefore it will only be executed if the if statement is true.


1

It looks like it will fail here else if (isalpha(argv) == 0) (I did not read the code further). argv is an array of strings. Review the man page for isalpha to be reminded that it takes a single character as an argument.


1

Let's look at some of the code: char *tmp = argv[1]; tmp[strlen(argv[1]) + 1] = '\0'; ... for (int i = 0, n = strlen(tmp); i < n; i++) { if (!isdigit(tmp[i])){ fprintf(stderr, "Usage: ./resize n infile outfile\n"); return 1; } } First, why is the code inserting an end of line marker? This is automatically done when each ...


1

It's quite literally what it says. First, understand that all warnings are treated as errors because of one of the flags in the compile statement. This would otherwise just be a warning. Now to the real issue. It literally means that the var argv[] is not being used anywhere in the program. If you were to do anything with the input vars, like print them ...


1

The best and most efficient way is to out grow CS50.h use char* newString = argv[1]; argv will always be there so you might as well point to it or use it.


1

string keyword = argv[1]; will work because you are assigning the address of string argv[1] to keyword. You can now treat keyword as a char array and access the individual characters as keyword[i]. You could also do this: char keyword[strlen(argv[1])+1]; strcpy(keyword, argv[1]); Instead of keyword pointing at the same memory location as argv[1], this ...


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;


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