0

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..

#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 ?
}
Wug
  • 12,956
  • 4
  • 34
  • 54
Hassan Z
  • 446
  • 2
  • 7
  • 14
  • Where is `inp` defined? What is the name of the last function in the file? How does this even compile? – Kevin Vermeer Jul 13 '12 at 20:54
  • Kevin: it's pseudocode. I don't think it needs to compile for us to identify what he's trying to do. Let's assume that inp is an int declared somewhere globally outside of his snippet. – Wug Jul 13 '12 at 20:58

2 Answers2

4

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.

Also, you should not be passing the address of the parameter to scanf, it's already a pointer to an 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
}
Wug
  • 12,956
  • 4
  • 34
  • 54
  • man scanf reports that scanf reports the # of items read & assigned. Using gcc 4.1.2 treats is as such. Testing the return as done here doesn't work as I think you intended. This may be compiler specific. – Taylor Price Jul 13 '12 at 21:06
  • Aha! You're right. I made the dumb assumption that it would work like every other c function that exists, and return 0 for success. However, you can still use its return value for validity checking. Since it will return the number of items successfully read, it will return 1 if it worked correctly, all other values indicate an error or invalid input. I'll edit my answer. – Wug Jul 13 '12 at 21:10
  • Sorry, I didnt get it. All I wanna know If I have scanf in a function other thn main how Can I validate that input using another function. I have validation in C before but both my scanf and validation codes were always together (in same function) – Hassan Z Jul 13 '12 at 21:10
  • The answer I posted that covers this: you can't. Not without something yucky like a global variable, or something contrived and overcomplicated. Just stick with this. – Wug Jul 13 '12 at 21:13
  • @Wug Yeah .. I understood what you meant. My answer does the same thing. - HassanZ - The answer he provided is in C. – Taylor Price Jul 13 '12 at 21:14
  • If you entered, say, "cat" instead of a number, get_input would return 0. – Wug Jul 13 '12 at 21:16
1

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.

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. 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.

Community
  • 1
  • 1
Taylor Price
  • 622
  • 1
  • 8
  • 21
  • i think this is the closest.. Also can you tell me how I deal with multiple variables ? you can edit your code and add one or two more variables in it. So I can have better understanding. Thank You – Hassan Z Jul 13 '12 at 21:53
  • hey @taylor-price I m still confused......let me make my question easier. in main i m only calling function. – Hassan Z Jul 14 '12 at 23:44
  • @HassanZ - I'm not entirely sure what you're looking for. What do you mean by "in main i'm only calling function"? Does that mean you're only calling one function? What are you trying to do? – Taylor Price Jul 15 '12 at 21:42