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


int main(int argc, string argv[])
{
//are there two main line arguments? is k a postive integer? if not printf Usage: ./caesar key

int k = atoi(argv [1]); 

    if (argc == 2 && k >= 0)
    {
        string s = get_string ("plaintext: ");
    }
    else
    {
        printf ("Usage: ./caesar key\n");
        return 1;
    }

}
0
  1. check for two arguments:
if (argc != 2) {  // put this right after main
   printf("...");
   return 1;
}
  1. atoi will fail if its parameter is not a numerical string, you cannot use it this way. You need to examine each character of argv[1] and make sure all of them a char from '0' to '9'. You will have to use a loop and terminate the loop after you examine all characters.
1
  • 1
    Very close but not exactly true. If atoi encounters a non-numeric char, it will return 0. It can also return 0 if the number is actually 0. So, things can get dicey if 0 is a valid value to be returned. (Not a problem if 0 is rejected as input.) Also, a hint: isdigit() is your friend. – Cliff B May 28 '20 at 21:19

You must log in to answer this question.

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