0

The log is telling me: "Segmentation fault", and after looking into it, I figured out the problem was in the "isdigit", but i don't know how to fix it, i'm here for about 2 hours and i can't figure it out.

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

int main(int argc, string argv[])
{
    for(int i = 0, n = (strlen(argv[1])); i < n; i++)
    {
    if (argc != 2 || !isdigit(argv[1][i]))
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
    if (argv[1] < 0)
    {
        printf("You can't encrypt it\n");
        return 0;
    }
    }```
1

The actual problem isn't in the isdigit statement. It's in the for statement.

for(int i = 0, n = (strlen(argv[1])); i < n; i++)

If there is no parameter, then argv[1] doesn't exist. So, strlen throws a seg fault.

The code needs to check argc before it does anything with argv.

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

1
  • Oooh man! I'm so dumb, thank you so much Dec 28 '20 at 15:58

You must log in to answer this question.

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