0

I really don't want to ask for this since this's like asking you guys to do my homework for me but what did I do wrong here?

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>

string encrypt(string s);

int main(int argc, string argv[])
{
    int key = atoi(argv[1]);
    int i = 0;                                                          //make 26 char array
    int eng_char[26] = {0};
    for(char c = 'A'; c <= 'Z'; c++)
    {
        eng_char[i] = c;
        i++;
    }

    string input = "HELLO";
    printf("ciphertext: %string\n", encrypt(input));                    //encrypt things
}

string encrypt(string s)                                            
{
    int n1 = strlen(s);
    for(int i1; i1 < n1; i1++)
    {
        int i2 = 0;
        while(s[i1] != eng_char[i2])                                    //loop until get same char in array
        {
            i2++;
        }

        i2 = (i2 + key) % 26;
        s[i1] = eng_char[i2];
    }
    return s;
}

I have a whole bunch of unesed variable error so I suspect the problem lies with inserting input into the funcion?

1

Start with this short on Variables and Scope. You can only use variables in the scope in which they have been declared. For instance, key is declared in main, so you cannot access it, it does not exist, in encrypt.

More generally, variables can only be used in the code block, that is the code between {}, in which they have been declared. Maybe help50 could give you more clues, as in help50 make caesar.


The following is totally incorrect, left in to give context to the comments.

NB, the edX 2017 version of caesar does not take command line arguments.

9
  • actually, caesar does take command line arguments. Per the spec: "Your program must accept a single command-line argument, a non-negative integer. Let’s call it k for the sake of discussion." – SuperNovaCoder Apr 29 '17 at 15:46
  • So if I understand it right then there is no way for a function to access command line arguments right? – Huynq Apr 29 '17 at 16:12
  • @Huynq argc and argv are local to main (just like all arguments to a function). If you want to access them in another function, they can be passed as arguments to that function. – DinoCoderSaurus Apr 29 '17 at 17:06
  • @SuperNovaCoder that is from the 2016 version of the spec. I clarified the answer. – DinoCoderSaurus Apr 29 '17 at 17:32
  • @DinoCoderSaurus Visit docs.cs50.net/problems/caesar/caesar.html#specification. – SuperNovaCoder Apr 29 '17 at 18:16

You must log in to answer this question.

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