2

I need to check if a user input value is not an int value. I've tried different combinations of what I know but I either get nothing or random errors

If the user inputs any char it'll raise a warning message

this is what ive written

#include <stdio.h> 

//C program to perform addition, subtraction, multiplication, division + - * / 

int main()   
{    
    int num1,num2;
    char alpha[30]

    printf("enter numbers:\n\n");

    printf("number 1: ");
    scanf("%d",&num1);

    printf("number 2: ");
    scanf("%d",&num2);

    // write a funcntion that when a char is entered to display an error 

    if (num1//and num2 == alpha)
        printf("error");


        else {

    printf("Rezultat: \n");

    printf("sborut im e: %d\n",num1+num2);
    printf("ralikata im e: %d\n",num1-num2);
    printf("proizvedenieto im e: %d\n",num1*num2);
    printf("ralikata im e: %d\n",num1/num2);

            }
    return 0;
}
6
  • you must use try catch to handle the error Commented Jan 7, 2017 at 21:44
  • Select the code. Click the code button {} to format the code correctly. Commented Jan 7, 2017 at 21:46
  • 5
    @Mohsen_Fatemi - no "try catch" in "C" Commented Jan 7, 2017 at 21:46
  • 2
    scanf returns the number of successful conversions. You request one conversion, so the return value should be 1. Commented Jan 7, 2017 at 21:47
  • 2
    Possible duplicate: stackoverflow.com/questions/4072190/… Commented Jan 7, 2017 at 21:49

2 Answers 2

3

Scanf has a return value for a reason.

1-3) Number of receiving arguments successfully assigned (which may be zero in case a matching failure occurred before the first receiving argument was assigned), or EOF if input failure occurs before the first receiving argument was assigned.

4-6) Same as (1-3), except that EOF is also returned if there is a runtime constraint violation.

Here is an example program using that information:

#include <stdio.h> 
int main(int argc, char **argv) {
    int inputInteger;
    printf("Please provide some input.\n");
    if(scanf("%d", &inputInteger) == 1) {
        printf("You inputted an integer\n");
    } else {
        printf("You did not enter an integer\n");
    }
    return 0;
}

Output:

./a.out
1[Enter]
You inputted an integer

./a.out
hello[Enter]
You did not enter an integer.

Note: I feel obliged to inform you that scanf() is not the best way to get input. See this answer for more details.

EDIT: I changed if(scanf("%d", &inputInteger)) to if(scanf("%d", &inputInteger) == 1) so that EOF will not output that an integer was found (pointed out by chux in the comments).

Sign up to request clarification or add additional context in comments.

5 Comments

okay but how to check for two scanf like my case if (scanf("%d",&num1)( what stays in between them) scanf("%d",&num2){ printf("stuff...") }
@Toni03m Simply use the && operator inside your if statement. If you don't know how to do that, I would suggest reading a basic book on C.
Note the when stdin is closed (end-of file), scanf("%d", &inputInteger) returns EOF and will then printf("You inputted an integer\n");. Better to use if(scanf("%d", &inputInteger) == 1).
@chux Edited. Thank you, I didn't catch that.
I inputted 4.5 and it still worked. It just set the value to 4.
0

a try/catch approach works, with casting to int the test is caught by the compiler

std::string input;
std::getline(std::cin,input);
int input_value;
try {
  input_value=boost::lexical_cast<int>(input));
} catch(boost::bad_lexical_cast &) {
  // process bad input here
}

1 Comment

we are talking for C language

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.