-2

I'm trying to write a function that will take an input and return it when it is valid

The function is constantly printing the error message even if the input is correct.

How can I pass the argument through to verify without it being evaluated as boolean beforehand?

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

char verify(char argument[256]){            //function to check an input is valid
    char input;
    while(1){
        input = getchar();
        while(getchar()!='\n');             //clear buffer
        if(argument){
            break;
        }
        printf("That is not a valid input. Try again: ");       //error message
    }
    return input;
}

int main(void){

    char input;

    printf("Enter an alphabetical input: ");
    input = verify(input>64&&input<91||input>96&&input<123);   //checks to see if the ASCII value of the input corresponds to a alphabetical char

    printf("Input is: %c",input);

    return 0;
}

1 Answers1

0

Instead of if (argument) you need to actually put your boolean conditional there: if (input>64&&...&&input<123) When you do that, the verify function no longer needs to take an argument.

C doesn't support what you're doing trying to pass the conditional as an argument. It just so happens that the type of that expression is compatible with the char[] type verify takes; which is why your code is compiling at all. C does in no way knows you are wanting to pass a conditional expression for verify to evaluate. It just doesn't work like that.

Michael Albers
  • 3,721
  • 3
  • 21
  • 32