简体   繁体   中英

Trouble understanding Functions in C

I am working through a programming assignment. I had the key validation working within main but decided to try to make it a separate function. I do not understand functions very well yet so I am unable to see where I am going wrong. Whenever I run the program, I always just get "Key is Valid" even when I know it's not. As I said, the program was running fine in main.

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

int validate (int c, string v[]); //prototpe validate function

int main (int argc, string argv[])
{
    int validate (int argc, string argv[]); //run validate for argc and argv

    printf("Key is valid\n"); //if valid, print message

}

int validate (int c, string v[])
{
    //Validate that only one Command Line Argument was entered
    if (c != 2) //Check the number if entered commands at execution
    {
        //If more than one, print error message
        printf("Key must be the only Command Line Argument\n");
        return 1; //Return false
    }

    //Validate that Key length is 26 characters
    if (strlen(v[1]) != 26) //Don't forget null 0
    {
        printf("Key must contain exactly 26 characters\n");
        return 1; //Return false
    }

    //Validate that all Key characters are alphabetic
    for (int i = 0, n = strlen(v[1]); i < n; i++)
    {
        //Check each element of the array
        if (isalpha(v[1][i]))
        {
            continue; //Continue if alphabetic
        }
        else
        {
            //if non-alphabetic, print error code
            printf("Key must contain only alphabetic characters\n");
            return 1; //Return false
        }
    }

    //Validate that each Key character is unique
    for (int x = 0, z = strlen(v[1]) - 1; x < z; x++)
    {
        //Create a second loop to compare every element to the first before incrementing the first
        for (int y = x + 1; y < z; y++)
        {
            //Cast array elements to int, check if each element equals next element in array
            if (v[1][x] == v[1][y])
            {
                printf("Key must contain exactly 26 unique characters\n");
                return 1;
            }
        }
    }

    return 0; //Key is valid, so return true
}

You are just declaring the function validate instead of running that and printing Key is valid unconditionally.

To run the function validate and print Key is valid only if it returns 0 , the main function should be like this:

int main (int argc, string argv[])
{
    if (validate (argc, argv) == 0) //run validate for argc and argv and check its response
    {
        // put printing inside if statement so that it runs only if the condition is true
        printf("Key is valid\n"); //if valid, print message
    }
}

Function declaration hints the compiler that I will return a particular data type and I will accept the given data types in the arguments section. For example,

int check(int a, int b); -> return type is int and the function will accept 2 integer parameters.
int mul(int a, float b); -> return type is int and the function will accept 1 integer parameter and 1 float.
void check(); -> returns nothing, accepts nothing

function calling is like your code is calling the function to execute.

int c = check(2, 3);
int b = mul(3, 0.12);
check();

Your function is returning some value. You have to get that value and execute rest of the code based on the value as below.

if (validate (argc, argv) == 0) {
    printf("key is valid");
} else {
    printf("key is not valid");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM