简体   繁体   English

C语言-通过功能验证用户输入

[英]c language - Validating User Input via function

I need to validate user input using a separate function. 我需要使用单独的功能来验证用户输入。 For example, programming is asking for input in functionA and validation code should be in FunctionB ... I know all the ifs and while statement for validation but I cant figure out how to use two separate function for this... Here is the sample run.. 例如,编程要求在functionA中输入,而验证代码应在FunctionB中。我知道所有ifs和while语句以进行验证,但是我不知道如何为此使用两个独立的函数。这是示例跑..

#include <stdio.h>

void get_input (int * inp);
double valid_input (void);

main ()
{
    get_input (&inp);
    valid_input ();
}

void get_input (int *inp)
{
    printf("enter something");
    scanf("%d", &inp);
}

double valid_input ()
{
    // what to put here ?
}

In this case, you would want to keep it in one function, since the value returned by scanf determines whether the user input was valid or not. 在这种情况下,您希望将其保留在一个函数中,因为scanf返回的值确定用户输入是否有效。

Also, you should not be passing the address of the parameter to scanf, it's already a pointer to an int. 另外,您不应该将参数的地址传递给scanf,它已经是一个指向int的指针。

Consider rewriting your function like this: 考虑像这样重写您的函数:

int get_input (int *inp);

// main function is here

// returns 1 if input was valid
// see documentation for scanf for possible return values
// http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
int get_input (int *inp)
{
    printf("enter something: ");
    return scanf("%d", inp); 
}

You can then use the return value of the function to determine whether or not it succeeded, Like this: 然后,您可以使用函数的返回值来确定它是否成功,如下所示:

int value;
if (get_input(&value) == 1)
{
    // input was valid
}
else
{
    // function returned an error state
}

I'm not entirely sure exactly what validation that you're looking for. 我不能完全确定您要寻找的验证。 If you're looking simply for validation that the character types you were looking for were entered, Wug's answer is close. 如果您只是想验证输入的字符类型,Wug的答案很接近。

If you're looking for another function that does some validation, this could provide a starting point for you: 如果您正在寻找另一个进行验证的功能,则可以为您提供一个起点:

#include <stdio.h>

int get_input (int *integerINput, char *characterInput);
void valid_input (int inp);

main()
{
    int integerInput;
    char charInput[2];

    // man scanf reports that scanf returns the # of items
    //      successfully mapped and assigned.
    //      gcc 4.1.2 treats it this way.
    if (get_input (&integerInput) < 2)
    {
        printf ("Not enough characters entered.\n");
        return;
    }

    valid_input (integerInput);
}

int get_input (int *integerInput, char *characterInput)
{
    int inputCharsFound = 0;

    printf ("Enter an integer: ");

    inputCharsFound += scanf ("%d", inp);


    printf ("Enter a character: ");

    // The first scanf leaves the newline in the input buffer
    //    and it has to be accounted for here.
    inputCharsFound += scanf ("\n%c", characterInput);

    printf ("Number of characters found = %d\n", inputCharsFound);

    return inputCharsFound;
}

void valid_input (int inp)
{
    if (inp > 5)
        printf ("You entered a value greater than 5\n");
    else
        printf ("You entered a value less than 5\n");
}

EDIT HasanZ asked for more details on how to handle more than one variable in the comments below. 编辑 HasanZ在下面的评论中要求提供有关如何处理多个变量的更多详细信息。 I've updated the code to read in another input character. 我已经更新了代码以读取另一个输入字符。

I'll leave it to you to determine how to best accept the appropriate input and validate that input since you've asked in generic terms how to validate in a separate function. 由于您已经用通用术语询问了如何在单独的函数中进行验证,因此我将由您决定如何最好地接受适当的输入并验证该输入。

I would also take a look here for more information on C programming. 我还将在这里查看有关C编程的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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