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;
}
user3386109
  • 34,287
  • 7
  • 49
  • 68
Toni03m
  • 31
  • 1
  • 3

2 Answers2

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

Community
  • 1
  • 1
iRove
  • 562
  • 1
  • 9
  • 19
  • 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 Jan 07 '17 at 22:12
  • @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](http://stackoverflow.com/a/562377/4520911). – iRove Jan 07 '17 at 22:53
  • 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 - Reinstate Monica Jan 07 '17 at 23:30
  • @chux Edited. Thank you, I didn't catch that. – iRove Jan 07 '17 at 23:50
  • I inputted `4.5` and it still worked. It just set the value to `4`. – Flaming_Dorito Oct 01 '20 at 08:03
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
}
user1213320
  • 650
  • 1
  • 6
  • 13