0

i have been trying to do this for a month and i am finaly down to my last error message when i run my check 50.the error message is :( handles non-numeric key timed out while waiting for program to exit.

does anyone know where i have gone wrong i am really struggling here?

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





int main(int argc, string argv[])
{


// make sure only 1 arguments and a number else reprompt
    if (argc == 2 && isdigit(argv[1]))

    {
        int k = atoi(argv[1]); // find the ceasar KEY value convert into integar
        string s = get_string("plaintext: "); // text
        printf("ciphertext: "); // print out cipher

// encrypt text
        for (int i = 0, n = strlen(s) ; i < n; i++)
        {
            // checking if it is lowercase
            if (s[i] >= 'a' && s[i] <= 'z')
            {
                printf("%c", (((s[i] - 'a') + k) % 26) + 'a'); // print out lowercase with key
            } // uppercase letters
            else if (s[i] >= 'A' && s[i] <= 'Z')
            {
                printf("%c", (((s[i] - 'A') + k) % 26) + 'A'); // print out uppercase with key
            }

            else

            {
                printf("%c", s[i]);
            }
        }

        printf("\n");
        return 0;
    }

    else
    {
        printf("Usage: ./caesar k\n");
        return 1;

    }

}
0

The code is not doing all the needed input parameter checks or is doing them wrong. It doesn't check that there is exactly 1 input parameter, the "key".

Next, it doesn't verify that the key, if entered, is all digits. The isdigit() function only checks one char, it cannot check an entire string. argv[1] is a string, not a char. Each char in argv[1] needs to be checked (think "for loop").

Finally, when something is found to be wrong - too few or too many parameters, or non-digit key - the program must exit with a return statement. There are no return statements in the code that checks input.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • hi cliff thank you for commenting, how do i get it to register the 1 input parameter for the key. sorry i have to ask but i feel i am so out of my debth here i dont now what i am missing i have wrote my code in so many different ways all with the same problem or a segmenation fault. Oct 5 '20 at 10:57

You must log in to answer this question.

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