0

I am trying to complete the caesar problem, but my program is not working properly. I know I need to add some code at the beginning to ensure the 2nd argument is legitimate, but I'm just wondering why my program is not processing the for loop once I enter plain text:

#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]);
        }
    }
}

My program is not bringing up any errors when I 'make' it, but when I enter 'Hello' as the plain text the program does not return anything. Could anyone help by telling me why?

Much appreciated, Dan

2
  • In your for loop you're checking if i > n. i always starts being 0, so it will never be greater than the plaintext lenght. You should do the opposite --> "if i < n"
    – belublem
    Commented Feb 25, 2018 at 13:09
  • @belublem you are correct. Please delete your comment and add it as an answer so that it can be accepted and the question can be closed. thanks.
    – Cliff B
    Commented Feb 25, 2018 at 20:28

1 Answer 1

0

In your for loop you're checking "if i > n".

i always starts being 0, so it will never be greater than the plaintext lenght.

You should do the opposite "if i < n"

You must log in to answer this question.

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