-1

how to strict the user to just enter a number when the programme ask about the input or just how to apply numeric input on a entry can anyone help me out.

as a exmpale:

#include "stdio.h"
#include "conio.h"
  void main()
     {
        int x;
        printf("Enter a numeric value:");
        scanf("%d",&x);

        if(..........)    // here i supose to write the if statement  
}

6 Answers 6

2

scanf returns the number of parameters successfully processed, so you need to store its return value and test that.

Therefore, you can do if (scanf("%d", &x)) { ... } else {... } instead, since scanf will either return 0 (no integer was matched) or 1 (if an integer was matched)

EDIT

if (scanf("%d", &x)) {
    /* an integer was read into x, so what do you want to do here? */
} else {
    /* what the user typed was not an integer, so normally you want to write some error message or something */
}
Sign up to request clarification or add additional context in comments.

2 Comments

can u plz written the whole statement i did not understant what to write in if {body} or else {body}
scanf("%d",...) has no over/underflow range check, try an input like "999999999999999999999999999999999999999", scanf gives OK but isn't
0

You should receive the whole line which inputed by user, then use isdigit()(defined in ctype.h) to check each charactor to see if it is a number.

4 Comments

but i can't use isdigit or ctype.h i don't know about this function or header file
This is unnecessarily slow and painful. Many implementations have an actual nontrivial function for isdigit despite the C standard requiring it to return nonzero if and only if ((unsigned)c-'0'<10).
@R If the function is trivial it's probably inlined by the compiler. There's no point in premature optimization. Also, even if that "conversion" works, it's cryptical. There's a reason why the function "isdigit()" exists.
isdigit fails on heading sign in inputstream, like +3 or -3
0

I advise you to check here: http://www.tek-tips.com/viewthread.cfm?qid=1024751&page=10

Interestingly enough, that is also the first result on Google.

Also, stop using void main(). It's restricting.

Comments

0

Also, consider taking input in a infinite while loop, and break out of it when the user enters the proper input.

Comments

0

Comments

0

If you want the user's input to satisfy a certain condition, you can do something like this:

#include <stdio.h>

int main()
{
    int x;
    printf("Enter a numeric value:");

    scanf("%d", &x);

    //suppose you want the user to enter values b/w 0 and 999
    while ((x < 0) || (x >= 1000)) {
        scanf("%d", &x);
    }
}

Remember, void main and conio.h are not standard compliant.

Comments

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.