0

I have made my caesar program, and when compiled it doesn't generate any errors. However, once I run the program with a valid command argument, and then input text, nothing is printed after. Here is my program so far:

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

int main(int argc, string argv[])

{   //making sure argc is equal to 2 else program does not run
    if (argc != 2)
    printf("Error\n");


    //converting second command line argument from string to int
    int k = atoi(argv[1]);

    //getting plain text from user
    string p = get_string("Plain text: ");


    for(int i = 0, n = strlen(p); i < n; i++)
    {
        if(isalpha(p[i]))
        {
            if (islower(p[i]))
            {
                printf("%c", p[i] - (((97)+k)%26)+97);
            }
            else
            {
                printf("%c", p[i] - (((65)+k)%26)+65);
            }
        }
        else
        {
            printf("%c", p[i]);
        }
    }
}

For example, I'll run the program with "./caesar 4" and then get the prompt saying "plain text: ". However, when I return a string back to the program, the program does not print out the encoded version of the string back. Could anyone help by telling me why? I've gone over the code many times but cannot figure it out.

I'm also very new to programming so it may be obvious, but any help is much appreciated.

Thanks, Dan

1 Answer 1

0

the problem is with Addition and Subtraction formula, take a look on the steps that the formula do and which steps should it follow, you do all the calculation without involving the p[i] in it, you will have to rearrange the parentheses to get p[i] into the formula, then you shall get the desired result.

You must log in to answer this question.

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