0

I have gotten my code to work for the caesar problem in problem set 2. However, when i check through check50 I run into this error: :( handles non-numeric key timed out while waiting for program to exit. Everything else works fine. I thought that using the isdigit() function handled non-numeric entries, but maybe I am misplacing or misusing the function. Here is my code so far:

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

int main (int argc, string argv[])
{
    if (argc !=2)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
    else
    {
        for (int i = 0, n = strlen(argv[1]); i < n; i++)
        {
            if (isdigit(argv[1][i]))
            {
                int key = atoi(argv[1]);
                int letter;
                int cipheredletter[30];
                string plaintext = get_string("plaintext: ");
                printf("ciphertext: ");
                    for (int j = 0, m = strlen(plaintext) ; j < m ; j++)
                    {
                        if (isalpha(plaintext[j]))
                        {
                            if (isupper(plaintext[j]))
                            {
                             letter = plaintext[j]-65;
                             cipheredletter[j] = (letter + key)%26;
                             cipheredletter[j] = cipheredletter[j]+65;
                             printf("%c",cipheredletter[j]);
                            }
                            else
                            {
                             letter = plaintext[j]-97;
                             cipheredletter[j] = (letter + key)%26;
                             cipheredletter[j] = cipheredletter[j]+97;
                             printf("%c",cipheredletter[j]);
                            }

                        }
                        else
                        {
                          cipheredletter[j] = plaintext[j];
                          printf("%c",cipheredletter[j]);
                        }
                    }
                    printf("\n");
                    return 0;
            }
            else
            {
            printf("Usage: ./caesar key\n");
            return 1;
            }
        }
    }


}

1 Answer 1

0

Your program does not meet all specifications, so chek50 fails, you must ensure that the input provided by the user is a positive integer, and not anything else. That sounds like a new IF condition, the isdigit () function can help, we must exit the program with a value of 1 if it is not met:

"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."

1
  • ahhhh makes sense. Got it going now. Thank you!
    – janecrager
    Commented Apr 13, 2020 at 17:53

You must log in to answer this question.

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