0

I cannot figure out why am I getting these results?

I'm currently only trying to validate the Numeric Key when typing 20x as a command-line argument get Usage: ./caesar Key

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

int main (int argc, string argv[])
{
    int Key = (int)argv[1];

    if (argc == 2)
    {
       for (int i = 0, n = strlen(argv[1]); i < n; i++)
       {
           if (argv[1][i] >= 48 && argv[1][i] <= 57)
           {
               printf("Success\n");
               printf("%s\n", argv[1]);
           }
           else
           {
               printf("Usage: ./caesar Key\n"); 
               return 1;
           }
       }

    }
    else 
    {
        printf("Usage: ./caesar Key\n");
    }
}

enter image description here

0

Maybe it's because you're trying to cast 20x into a integer, try doing these conditions, if argc != 2 then end the program, if argv[1] has a non numeric character then end the program. The arguments that your program should take should only be integers.

You must log in to answer this question.

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