Trouble with caesar Pset CS50

I kind of understand what I need to do for this pset however, I am not a computer and my thoughts do not translate into code lol. Any pointers? Have been stuck on this pset for quite some time now. Side note, L31 should I do output = input[I] + key; it will run but will not return all alphabetical. E.g. key = 2 input = ZZ output = \ (since its adding to ascii I understand that)

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

//indexing uppercase chars 0 - 25
char upperc[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

//indexing lowercase chars 0 - 25
char lowerc[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};


int main(int argc, string argv[])
{
    // checking if there is a command line argument

    if (argc != 2) {
        printf("USAGE: ./caesar KEY\n");
    }

    //also need to add a check to see if the CLA is a digit

    int key = 0; //initializing key to 0
    key = atoi(argv[1]); // converting char argv[1] to int


    string input = get_string("Plaintext: "); // input from user
    int input_length = strlen(input);


    printf("Ciphertext: "); // just so it prints neatly

    //TROUBLE BELOW

        for (int i = 0; i < input_length; i++) {    // looping through all the chars in input
            if (isalpha(input[i])) {        // Checking if char is alphabetical
                if (isupper(input[i])) {        // checking if char is uppercase
                    char output = ((upperc[input[i]] + key) % 26); // it makes sense in my head, basically what it is trying to do is
                    printf("%c", output);                          // find char input[i] then index that with my array upperc[] then doing function
                }                                                  // and returning the output char

                //still need to add lowerc here
            }
        }
            printf("\n");
}
'''
caesar.c:31:41: error: array subscript is of type 'char' [-Werror,-Wchar-subscripts]
                    char output = upperc[input[i]] + key;

Ended up watching some videos for help. Turns out I was making the problem much harder than I should have lol. instead of indexing the input[I] to a range of 0 - 25, I was just able to index it by the ascii value range of 0 - 25. a lot of bug fixing after that but we got there.

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


int main(int argc, string argv[])
{

    if (argc < 2 || argc > 2) {
        printf("USAGE: ./caesar KEY\n");
        return 1;
    }

    for (int i = 0; i < strlen(argv[1]); i ++) {
        if (!isdigit(argv[1][i])) {
            printf("USAGE: ./caesar KEY\n");
            return 1;
        }
    }

            int key = atoi(argv[1]);

            string input = get_string("Plaintext: ");

            int input_length = strlen(input);

            printf("Ciphertext: ");

            for (int j = 0; j < input_length; j++) {

                if (input[j] >= 'A' && input[j] <= 'Z') {
                    printf("%c", ((((input[j] - 65) + key) % 26) + 65));
                } else
                if (input[j] >= 'a' && input[j] <= 'z') {
                    printf("%c", (((input[j] - 97) + key) % 26) + 97);
                } else printf("%c", input[j]);
            }
            printf("\n");
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.