0

I keep getting the above error:

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

int main (int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./caesar key");
    }

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

    }       

}


1 Answer 1

0

I cannot reproduce your error, but your program is highly inefficient and would produce a segmentation error if the number of arguments is 1, the correct form would be:

if (argc != 2)
{
    printf("Usage: ./caesar key");
    return 1;
}

As for the other part of the code it just doesn't work, look at the output it has when we mix digits and characters:

~/ $ ./crack 3e
Success
3e

All your code can be summarized as follows:

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

printf("Success\n");
printf("%s\n", argv[1]);
return 0;
1
  • Also, the for loop as written will only run once. It hits an if/else statement with return statements in both clauses. As soon as it hits a return statement, the program terminates. The code will never get to the end of the loop.
    – Cliff B
    Commented Aug 21, 2019 at 1:44

You must log in to answer this question.

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