0

Let us say I want to create a separate function for lines 11-25 (check comment for reference) in my code as a separate "argument checker" function. Something similar to how I have my decrypt function setup, I am trying to obviously reduce the clutter in main and just have it simply calling functions as it should be. The problem is I am afraid I can't seem to work around that. Is it possible? From what I am aware, command-line arguments can only be passed through the main function.

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

void decrypt(string p, int k_int, char c);

int main(int argc, string argv[])
{
    if ((argc == 1) || (argc > 2)) //checks if there's two command line arguments or not
    {
        printf("Error: Please enter in one positive integer after the program execution command.\n");
        return 1;
    }

    for (int i = 0; i < strlen(argv[1]); i++)
    {
        string k = argv[1]; //implementation to pass the arg to a variable for ez iteration
        if ((isdigit(k[i])) == 0)
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    } //I want to put the above code in a separate function

    int k_int = atoi(argv[1]) % 26; //counters if k is greater than 26 (only 26 letters in alphabet)
    string p = get_string("plaintext: "); //plaintext
    char c = '\0'; //ciphertext

    printf("ciphertext: ");
    decrypt(p, k_int, c);

    return 0;
}

void decrypt(string p, int k_int, char c)
{
    for (int i = 0; i < strlen(p); i++)
    {
        if (isalpha(p[i]) != 0)
        {
            if (islower(p[i]) != 0) //checks if lowercase character since those ASCII values are different than uppercase
            {
                c = ((p[i] - 'a' + k_int) % 26) + 'a'; //subtract by lowercase a
            }
            else
            {
                c = ((p[i] - 'A' + k_int) % 26) + 'A'; //subtract by uppercase A
            }
        }
        else
        {
            c = p[i]; //keeps punctuation as is
        }
        printf("%c", c);
    }
    printf("\n");
}
0

Pass the argv[1] variable into your other function. The other function declaration could look like

bool is_a_number(string caesar)

and where you call it could look like

if (!is_a_number(argv[1])) {
    // return your error and exit the program
}
// keep going

You can't exit the program from the other function, so that part has to stay in main, but you can pass that command line argument into your other function.

(You can also pass all the command line arguments into the function. The declaration would look like bool is_a_number(string arguments[]). You only need the one argument, though, so you don't have to do that.)

Also, a note: You probably remember what p, k_int, and c are in void decrypt(string p, int k_int, char c), but if you're working with someone else, or if you come back to this code in 6 months, it'll take extra work to figure out what those mean. It's generally a good idea to give your variables descriptive names, so you don't have to check other parts of the program to figure out what variables are.

0

It is possible, but not recommended. Why put code to do this in a separate function, and endure the needless overhead of a function call when it's only going to execute once???

Having said that, if you really want to, or just want to know how, check out exit() function. Here's a link:

https://www.tutorialspoint.com/c_standard_library/c_function_exit.htm

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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