samedi 27 juin 2020

why is my code not giving me the correct output (i only want to receive integers as key)?

int main(int argc, string argv[])                           //command line argument
{

  for ( int i = 0; i< strlen(argv[1]); i++)
  {
  if (argc != 2)                                            //correct argument count
  {
    printf ("usage: ./caesar key\n");
    return 1;
  }
  else if(isdigit(argv[1][i]))
  {
     printf("success\n");
  }
  else if(isalpha(argv[1][i]))
  {
    printf ("usage: ./caesar key\n");
    return 1;
  }
  }  

in this part of code what i'm trying to say to the computer is to 'only accept ints as key, otherwise reject the key'.

however, when i tried different inputs to test the accuracy of my code this is what happened:

~/caesar/ $ ./caesar 4r       //<--mix of outputs.i don't want 'success' here bc input has a letter
success
usage: ./caesar key
~/caesar/ $ ./caesar g5       //correct output
usage: ./caesar key
~/caesar/ $ ./caesar r        //correct output
usage: ./caesar key

as you can see, when i input a letter and then an int, it works fine, but when i do the opposite (int and then letter), it would give me a mix of outputs, both rejecting and accepting the output.

Aucun commentaire:

Enregistrer un commentaire