0

I am getting non numeric timed out error when I check50 my code. I have added another check to argv to see if it is a digit or not. It still doesn't work. Can you help?

#include <cs50.h>
#include <stdio.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;
    }

    int k = atoi(argv[1]); // store key value in K as an int
    int i;
    int key;
    for (key=0; key < strlen(argv[1]); key++)
    {
        if (isdigit(argv[1][key] == 0))
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
        else
        {
            continue;
        }
    }
    string plaintext = get_string("Type your text: ");  // get text input
    printf("ciphertext: ");      // print title to get cyphered text

    for (i = 0; i < strlen(plaintext); i++)
    {
        if (isalpha(plaintext[i]))
        {
            if (islower(plaintext[i]))
            {
                printf("%c", ((((plaintext[i] + k) - 97) % 26) + 97)); // formula to convert to alphab and then back to ascii
            }
            else if (isupper(plaintext[i]))
            {
                printf("%c", ((((plaintext[i] + k) - 65) % 26) + 65)); // formula to convert to alphab and then back to ascii
            }
        }
        else
        {
            printf("%c", plaintext[i]); // print as is
        }
    }

    printf("\n"); //exit
    return 0;

}

1
  • anyway you already asked this somewhere else ...
    – MARS
    May 21 '20 at 20:05
0

In your program it is never verified if argv [] is a digit or not, carefully observe your code:

if (isdigit(argv[1][key] == 0))

and look at the correct code:

if (isdigit(argv[1][key]) == 0)

A more intuitive way to write your code can be:

if (!isdigit(argv[1][key]))

Be careful with those little things, in a very large program it could be very difficult to detect, especially without check50 in real life. We should test our code thoroughly before sending to check50.

You must log in to answer this question.

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