0

Here's my code. I get lot's of errors from the compiler. Can you simply fix explicit errors that I made? So that I am on the right track and then I can keep fixing.

enter image description here

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

int main(int argc, string argv[])
{
    // make sure the user provided two arguments only
    if (argc != 2)
    {
        printf("Provide only two arguments");
        return 1;
    } else
    {
        // prompts the user to enter plaintext
        printf("plaintext:");
        string input = get_string();

        // keep the length or argv in l
        int l = strlen(*argv);

        // convert user-provided argument to lowercase
        for (int i = 0; i < l; i++)
        {
            tolower(argv[i]);
        }

        // make sure the user-provided input does not equal to NULL
        if (input != NULL)
        {
            // iterate through the user-provided input
            for(int i = 0, int n = strlen(input); i < n; i++)
            {
                // do this only if i'th character in user-provided input is alphabetical
                if (isalpha(input[i]))
                {
                    // do this only for lowercase alphabetical characters of the user's input
                    if(islower(input[i]))
                    {
                        // do this to lower-case letters
                        printf("%c", ( (input[i] - 97) + (key[(l + i ) % l] - 97 )) % 26 + 97);
                    } else
                    {
                        // do this to upper-case letters
                        printf("%c", ( (input[i] - 65) + (key[(l + i ) % l] - 65 )) % 26 + 65);
                    }
                } else 
                {
                    // do this only for non-alphabetical characters of the user's input
                    printf("%c", input[i]);
                }
            }
            printf("\n");
        }
    }
}
2
1

Don't be scared by the number of errors, fixing them one by one quickly reduces the number. If you are unsure about how to use a feature, search it in the CS50 reference or on the internet. It's really annoying to see people like you not doing their search first.

First error is about tolower not accepting strings. When you see something like that, look it up on the reference. That will tell you that tolower works on individual characters, not strings, and it returns a lowercased character, you'll have to use its return value.

Second error is about for (int i = 0, int n = strlen(input); i < n; i++), you could write for (int i = 0, n = strlen(input); i < n; i++).

If those variables were of different types, I'd prefer declaring all the variables, or all but one, above the for loop (there are some hacky solutions for this, but harder to read).

The other errors on that screen will probably disappear once the first ones are fixed.

BTW, there are some logic errors. For example, strlen(*argv) is same as strlen(argv[0]) (argv[0] contains the programme name). Use the debugger and/or add debug outputs to see where values are not what you expect them to be. You really should learn how to use a debugger and a search engine, it's more efficient than asking others each time, and will help you learn coding.

You must log in to answer this question.

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