0

I'm a bit confused on the cipher problem. The problem says the program must take a single argument being a non-negative integer. Here is how I tried to write the main function to take an integer as arg:

int main(int argc, int k)

However I must be completely off mark because I get the error

second parameter must be of type char** 

I'm thinking only the first parameter is needed (int argc in this case), but this is what I'm using to keep track of the number of arguments (so I can return 1 or 0 in main).

So I have no idea what I'm supposed to do. Do I take a string and convert it to an integer later in the code?

2

The reason you're having trouble is that main must have a specific form or it will not compile. You can't use something different.

If there will be no parameters passed to the program, it must look like one of the following:

int main()
int main(void)

Either is acceptable, but the latter is considered to more formally say that there are no parameters expected.

If your program takes parameters, it MUST have the following form:

int main(int argc, char* argv[])

This is the required standard and can't be changed. argc and argv usage is predefined in C. The first argument, argc, is a count of the number of elements in the array argv[]. The first element in the array, or argv[0], is the program name as it was invoked. Each element after that will contain the parameters from the invocation of the program, in order. So, if you have a program that is executed with the following line:

./testprog alpha 1 15 yes

Then argc=5, argv[0]=./testprog argv[1]=alpha argv[2]=1 and so on. Also, remember that every element in argv is a string. If you need to use one as a number, you need to use the appropriate function, like atoi() to convert it to the appropriate number type.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

1
  • That clears it up, thanks! – mlamp Jun 24 '15 at 19:32
1

exactly as the error says. according to the standard, main can be declared in either of these ways

int main(void)

or

int main(int argc, string argv[]) // assuming cs50.h is included

you use the second way when your program wants to access command-line arguments. command-line arguments are space-separated strings passed to the program as you start executing it. see https://www.youtube.com/watch?v=X8PmYwnbLKM!

that said, you can't take an int as a command-line argument. alternatively, you can take a string that only consists of digits and convert that string into an int using a function like atoi from the standard library.

read the specs for more details: https://cdn.cs50.net/2015/x/psets/2/pset2/pset2.html#hail_caesar!

3
  • thanks for the help! – mlamp Jun 24 '15 at 19:36
  • @kareem, why did you comment //assuming cs50.h is included ? I ran a test program without it and the behavior appears the same. Did I miss something? – Cliff B Jun 24 '15 at 19:43
  • @CliffB I'm using the type string in the parameter list. – Kareem Jun 24 '15 at 23:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .