I have two main questions for my code. Please excuse me for my tideous code and for not using existing functions in the library, i.e. isdigit and strlen, as I didn't want to become dependent on them from the get-go of learning how to programme. Also, I didn't read the instructions for the task, so you won't see any formulas or functions such as 'rotate' used.
Problem 1 :
My code compiles successfully when I go without the 'only_digit function' and place all its contents in the mains, but I decided to create a separate only_digit function to make the code more efficient so that's where the errors come from.
Error: incompatible pointer types passing 'string' (aka 'char *') to parameter of type 'string *' (aka 'char **'); take the address with & [-Werror,-Wincompatible-pointer-types] if (only_digit(argv[1]) == 1) ^~~~~~~ & note: passing argument to parameter 'argv' here bool only_digit(string argv[1]);
I don't understand why I must add '&' in front of argv[1] as the parameter. Even so, I get a segmentation error. From what I gathered, I think the problem lies with me using argv[1] as a parameter before main, and I don't understand why.
Problem 2 :
Also, I am unfamiliar with the concept of 'return'. The code works without adding return 1 in the mains, but when I run it through check50, the problem of exit code arises, so I was forced to add return 1 to make it work, but I don't understand why because it worked without it. Also why is it 'return 0' in my encryption function but 'return 1' in the main?
#include <cs50.h> #include <stdio.h> #include <stdlib.h> string encryption(int k); char alphabet[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; bool only_digit(string argv[1]); int main(int argc, string argv[]) { if (only_digit(argv[1]) == 1) { int k = atoi(argv[1]); encryption(k); printf("\n"); return 1; } else if (only_digit(argv[1]) == 0) { printf ("Usage: ./caesar key\n"); return 1; } } bool only_digit(string argv[1]) { int a = 0; while (argv[1][a] != '\0') { a++; } printf("%i\n", a); int b = atoi(argv[1]); printf("b: %i\n", b); int c = 0; while (b >= 0.1) { b = b /10; c++; } printf("b: %i c: %i\n", b, c); bool d = (a == c); return d; } string encryption(int k) { // 1. prompt the user for plaintext string plaintext = get_string("plaintext: "); printf("ciphertext: "); // 2. Count the number of characters within plaintext int n = 0; while (plaintext[n] != '\0') { n++; } // 3. Let the program know that the alphabets are counted from 0 to 26 for (int i = 0; i < n; i++) { // scan punctuations and numbers as it is if (plaintext[i] >= 32 && plaintext[i] <= 63) { printf("%c", plaintext[i]); } // add k to every typed character and deal with adding 13 for (int j = 0; j < 26; j++) { int z = j+k; if (plaintext[i] == alphabet[j]) { if (z > 25) { while (z > 25) { z = z - 26; } printf("%c", alphabet[z]); } else { printf("%c", alphabet[z]); } } //now deal with capitalising the alphabets if (plaintext[i] == alphabet[j] - 32) { if (z > 25) { while (z > 25) { z = z - 26; } printf("%c", alphabet[z]-32); } else { printf("%c", alphabet[j+k]-32); } } } } return 0; }