0

I have some basic questions on the Caesar problems which I would appreciate assistance on. Apologies in advance if the questions seem quite basis but I am putting a lot of hours into this and am getting confused with somewhat basic concepts.

My code for the initial part of the problem is as follows:

int main (int argc, string argv[])
{
    //declare variables
    int key=0;
    string plaintext;

    //ask for key
    printf("Please enter key");
    return key;

    //accept only 2 command line arguments including argv[], or return error code
    if (argc != 2)
    {
        printf("Please re-enter key.\n");
        return 1; 
    }
    // get plaintext and turn plaintext string into integer
    {
        string plaintext = GetString();
        key = atoi(argv[1]);
    }
return 0;
}

QUESTION 1)

I saw in the response from another student that it is not necessary to ask the user for the key. If not, I do not understand how the problem would actually work as I understand that you would need to ask the user A) for they key and B) what they want to have encrypted (plain text).

Secondly, where does argc gets its number of arguements from? If the user is not asked for the key, what are the two argc arguments? (ie. the first i understand being the name of the program)

Your assistance is much appreciated.

Thanks

2 Answers 2

0

First things first, the statement printf("Please enter key"); return key; is completely incorrect.

A return statement terminates the function and returns the return value, here, the value of key. I think you meant to do something like printf("Please enter key"); char key = get_char();, where get_char() is a function inside the cs50 lib that asks for a character in the terminal.

But the catch is that the pset specifies that the user should specify the key as the second argument in the terminal i.e if the key is c, then you should write something like ./ceasar c then ask for the plaintext inside the program.

So you ask for plaintext with something like string plaintext = get_string().

This is what the pset specifies.

The value of argc depends on the number of arguments you type in the terminal. So in case of ./ceasar a the value of argc is 2 , likewise if you only type ./ceasar, then the value of argc will implicitly be set to 1. So basically the value of argc is the number of elements in argv[] array of strings.

0

Vikrant Pradhan is right, in caesar we must enter the key by the command line from there the statement:

int main (int argc, string argv[])

Instead of usual:

int main(void)

You will see later how the functions work, in this case the main function takes arguments, and returns an integer zero if it is successful, a number other than zero otherwise. argc corresponds to the number of arguments, in the case of caesar 2, ie we must make sure that argc == 2. argv [] is technically an array of pointers (well forget that at the moment), the important thing is that it stores the arguments that we write in the terminal, so if we write:

./caesar 13

we have:

argv[0] == ./caesar
argv[1] == 13 // the key as a string

and

argc == 2

I hope this help.

3
  • Makes sense - since the program is called caesar.c, I did not realize that we could actually run the program with ./caesar 13 or similar (13 being the key). If this is not correct, please let me know, otherwise I will work under this assumption. May 4, 2017 at 17:54
  • Your guess is correct
    – MARS
    May 4, 2017 at 18:46
  • I have moved on in this question thanks to your advice however am now getting an error that states: May 8, 2017 at 12:31

You must log in to answer this question.

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