0

I've read all the other answers I can find about people having a similar problem, but mine doesn't seem to be the same issue. Everyone says to use isdigit first, then use atoi, which is what I've done. It works when I execute it myself, but check50 fails this test every time. I've tried everything I can think of. Any help would be appreciated!

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


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

    if (argc ==2 && isdigit(argv[1][0]))             //if argc = 2 AND argv[1] is digits
    {
        int k = atoi(argv[1]);                      //initialize k as the conversion of argv[1] to an integer

        string plaintext = get_string("plaintext: ");   //get user input for plaintext
        printf("ciphertext: ");

        for (int i = 0, len = strlen(plaintext); i < len; i++)    //i is the current character its looking at
        {
            if (plaintext[i] >= 'a' && plaintext[i] <= 'z')     //if a or higher, or z or lower
            {
                printf("%c", (((plaintext[i] - 'a') + k) % 26) + 'a');  //then print letter + key
            }
            else if (plaintext[i] >= 'A' && plaintext[i] <= 'Z')     //if A or higher, or Z or lower
            {
                printf("%c", (((plaintext[i] - 'A') + k) % 26) + 'A');  //then print letter + key
            }
            else
            {
                printf("%c", plaintext[i]);                     //otherwise print whatever character is there
            }
        }

        printf("\n");                                           //print new line after the ciphertext
        return 0;                                               //program completed successfully

    }
    else
        printf("Usage: ./caesar key\n");                        //print error message
        return 1;                                               //return error, terminate program
}

2 Answers 2

0

When it checks isdigit, it's only checking the first char in argv[1]. What happens when you test the program with a key of 1a? It should fail.

The code needs to cycle through argv[1], checking each and every char in that string using isdigit. Try a for loop.

2
  • I'm not sure how to go about doing that. I've tried everything I can think of, I'm sure I'm missing something that'll make sense once I see it. But how would I iterate through each char in this string to check that it's a digit? May 16, 2020 at 1:47
  • @BrandonZemel essentially a string is a array of chars right? Why not think of each char as say string[i] and forming a loop which checks each string[i]? I would recommend thinking again from here Oct 13, 2020 at 9:34
0

This step I kept skipping and working on other parts of my code because I was having the same issue. I’d get it to not print a non digit but it wouldn’t return the error. Finally, I had to split it into 2 if statements. Sorry I’m on my phone but I’ll type out my first part. { bool keyvalid = true; string s = argv[1];

 if (argc != 2)
 {
      printf(“Usage: ./caesar key\n”);
      return 1;
  }
 else
  }
        for (int i = 0; i < strnlen(s); i++)
        {
              if (isdigit(argv[1][i]) == false)
             {
                  keyvalid = false;
                  printf(“Usage: ./caesar key\n”);
                  return 1;
               }
         }
   }

Then I went on with the rest of the code. After days of trying it was the only thing that worked. Everything others said they used still allowed non digits in the key when I tried. Check50 worked perfect!

You must log in to answer this question.

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