Caesar: Checking the key

54 Views Asked by At

I am trying to check the key of the cipher text to see if every character is an integer. The code When I compile the program I get the following error code compiling error

1

There are 1 best solutions below

0
binhgreat On

You can do as bellow:

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

bool only_digits(string key);
void print_usage() {
  printf("Usage: ./caesar key\n");
}

int main(int argc, string argv[]) {
  if (argc != 2) {
    print_usage();
    return 1;
  }

  if (!only_digits(argv[1])) {
    print_usage();
    return 1;
  }
  return 0;
}

bool only_digits(string key) {
  int key_length = strlen(key);
  for (int i = 0; i < key_length; i++) {
    if (isdigit(key[i]) == 0) {
      return false;
    }
  }

  return true;
}

Your problem is:

  1. Line 6: only_digits should receive argument type string, not string[1] (array of string)
  2. Line 26, 33: argv[1] is the way you access to the 2nd element in argv array. While argv[1] in function prototype bool only_digits(string argv[1]) is declaring an argument with array type (with 1 element)