check50 results in all the tests passing but also produces a repetitive error, Invalid key. Please provide a positive integer. Cause Timed out while waiting for program to exit. Not sure which line is the culprit. Thank you

#include <ctype.h>
#include <stdio.h>

// Function to encrypt a character using Caesar's cipher
char caesarEncrypt(char ch, int key)
    if (isalpha(ch))
        char base = islower(ch) ? 'a' : 'A';
        return (ch - base + key) % 26 + base;
    return ch; // Preserve non-alphabet characters

int main()
    int key;

    // Prompt the user for the encryption key
    printf("Enter the encryption key (a positive integer): ");
    if (scanf("%d", &key) != 1 || key <= 0)
        printf("Invalid key. Please provide a positive integer.\n");
        return 1;

    // Consume the newline character left in the input buffer

    // Input message from the user
    printf("Enter a message to encrypt: ");
    char message[1000];
    fgets(message, sizeof(message), stdin);

    // Encrypt the message
    printf("Encrypted message: ");
    for (int i = 0; message[i] != '\0'; i++)
        printf("%c", caesarEncrypt(message[i], key));

    return 0;

2 Answers 2


Thank you UpandAdam, much appreciated

  • You are trying to ask the user for input when the assignment states that the key k will be given to you as a command line argument:
Your program must accept a single command-line argument, a non-negative integer. Let’s call it 'k' for the sake of discussion.
If your program is executed without any command-line arguments or with more than one command-line argument, your program should print an error message of your choice (with printf) and return from main a value of 1 (which tends to signify an error) immediately.
If any of the characters of the command-line argument is not a decimal digit, your program should print the message Usage: ./caesar key and return from main a value of 1.
Do not assume that k will be less than or equal to 26. Your program should work for all non-negative integral values of k less than 2^31 - 26. In other words, you don’t need to worry if your program eventually breaks if the user chooses a value for 
 that’s too big or almost too big to fit in an int. (Recall that an int can overflow.) But, even if k is greater than 26, alphabetical characters in your program’s input should remain alphabetical characters in your program’s output. For instance, if k is 27, A should not become \ even though \ is 27 positions away from A in ASCII, per asciitable.com; A should become B, since B is 27 positions away from A, provided you wrap around from Z to A.
Your program must output plaintext: (with two spaces but without a newline) and then prompt the user for a string of plaintext (using get_string).
Your program must output ciphertext: (with one space but without a newline) followed by the plaintext’s corresponding ciphertext, with each alphabetical character in the plaintext “rotated” by k positions; non-alphabetical characters should be outputted unchanged.

So you are prompting the check50 run for input that it is never going to give.

  • You should be prompting the user for plaintext with the prompt plaintext: You are giving them a different prompt.

  • You should be returning the output to the user with the prompt ciphertext: You are using a different prompt.

You must log in to answer this question.

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