0

Getting this segmentation error for inproper use of isdigit in my If statement, i just don't know how else to check argv[1] for numerical value as opposed to alphabetical.

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

if ((argc == 2 ) && (isdigit(argv[1]) == 0)) // if isdigit returns 0 then the key inputted is a digit
{
    int key = atoi(argv[1]); // converting a string to int and taking the array position 1 from the command line argument and making the value usable
    plaintext = get_string("plaintext: "); //input string stored in "plaintext"
    printf("ciphertext: ");

    for (int i = 0,  len = strlen(plaintext); i < len; i++) // assigning a position to the characters in the plaintext string

1 Answer 1

0
   isdigit(argv[1])

The isdigit function and all it's cousins take a single char as input. BUT, argv[1] is a string. So, when the code tries to shove a string down it's throat, isdigit chokes and spits out an error in the form of a seg fault.

The code needs to check each char in the string. You can't use isdigit to check an entire string at once. Maybe a for loop?

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

1
  • Thanks for a fast response homie! Oct 27, 2020 at 19:32

You must log in to answer this question.

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