0

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;
}

1 Answer 1

0
  1. for only digit function signature it should just be bool only_digit(string word)
    putting argv[1] in there changes it to being a string pointer

  2. in your is digit function you are comparing b an integer to 0.1 a float, this isn't a good idea, you want to compare to > 0 instead while writing this out is all well and good, a simpler way if you are going low level is to get string length, and then not use atoi but to use the ascii code range for the digits.

  3. I would suggest you use strlen function for calculating string length.

  4. return is what your function should return, main should return 0 on success non zero for error. other functions should return what their signature says that they should return

If you don't understand these things you are doing yourself a disservice by not watching the video and not reading the slides. the goal of course isn't to just solve the problems it is to gain understanding of what you are doing, why, and how to go about solving problems and the C language for this section. It is also absolutely criticial that you read the instructions for the task I would put that i all caps but it would be rude. again read the instructions if you don't read the instructions you will not pass. Coding is all about producing what you are asked to produce in the instructions. if they ask you to make toast and you make a tasty cake you have failed.

anecdotally almost half of the errors on this site seem to come from folks who did not read and follow instructions.

You must log in to answer this question.

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