Home > OS >  CS50 Pset2, Caesar, handles non-numeric key problem
CS50 Pset2, Caesar, handles non-numeric key problem

Time:10-22

When I use the function I created, only digits, and call it in main, it doesn't handle non-numeric keys. But when I type the body of the function only_digits right into main without calling it, it works just fine.

The specific issue is

:(( handles non-numeric key

running ./caesar 2x

cause: timed out while waiting for program to exit

Below is all the code, but instead of calling only digits in main(bool digits = only_digits(argv);), I just wrote the body of it,

for (int i = 0; i < strlen(argv[1]); i  )
        {
            if (isdigit(argv[1][i]) == false)
            {
                printf("Usage: ./caesar key\n");
                return 1;
            }
        }

in main to get all the checks to work for cs50.

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

bool only_digits(string argv[]);

int main(int argc, string argv[])
{
    if (argc == 2)
    {
        //run loop to check if each caracter in string, argv[1], is an integer 0-9, if it isn't then quite the program(return 1)
        for (int i = 0; i < strlen(argv[1]); i  )
        {
            if (isdigit(argv[1][i]) == false)
            {
                printf("Usage: ./caesar key\n");
                return 1;
            }
        }

        //convert the string key into integer key
        int k = atoi(argv[1]);
        //prompt user for plaintext to encrypt
        string plaintext = get_string("plaintext:  ");
        printf("ciphertext: ");

        //run loop to to the caesar calculation
        for (int i = 0, n = strlen(plaintext); i < n; i  )
        {
            //check if each character in plaintext the user inputted is between 'a'-'z', or 97-122
            if (plaintext[i] >= 'a' && plaintext[i] <= 'z')
            {
                printf("%c", (((plaintext[i] - 'a')   k) % 26)   'a');
            }
            //check if each character in plaintext the user inputted is between 'A'-'Z', or 65-90
            else if (plaintext[i] >= 'A' && plaintext[i] <= 'Z')
            {
                printf("%c", (((plaintext[i] - 'A')   k) % 26)   'A');
            }
            //If character isn't alphabet, just print what was inputted back to user
            else
            {
                printf("%c", plaintext[i]);
            }
        }
        printf("\n");
        return 0;

    }
    else
    {
        //Print error showing how to use the program and type a command-line argument
        printf("Usage: ./caesar key\n");
        return 1;
    }
}

bool only_digits(string argv[])
{
    for (int i = 0; i < strlen(argv[1]); i  )
    {
        if (isdigit(argv[1][i]) == false)
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }
    return argv;
}

The problem says I have to add a function called only_digits which is why I can't just submit it as is.

CodePudding user response:

This statement

return argv;

does not make sense. The compiler can issue a message for this statement (though it is not necessary). The return type of the function is bool but you are returning an expression of the type string * that never will be converted to false.

The function can be defined the following way

bool only_digits( string key )
{
    if ( *key == '\0' ) return false;

    while ( *key && isdigit( ( unsigned char )*key ) )   key;

    return *key == '\0';
}

The function return true if all characters represent digits otherwise false.

And the function can be called like

if ( !only_digits( argv[1] ) )
{
    puts( "Usage: ./caesar key" );
    return 1;
}

If you do not know yet what this expression *key means then the function can be defined the following way

bool only_digits( string key )
{
    size_t i = 0;

    while ( key[i] != '\0' && isdigit( ( unsigned char )key[i] ) )   i;

    return i != 0 && key[i] == '\0';
}
  • Related