Hello,

I have a problem in my program

I want to check the input if it's a number or not

the number Component of N boxes
for example if N=4
the input 1234 is valid
the input 123a is invalid

i try with this code

printf ("Please enter guess no.%d : ",i+1);
		scanf("%d", &num);


		if (isdigit(num))
		{
			printf("valid input!\n");
		}
		else printf("Wrong input!\n");

and I try used the scanf() function to check, but it's not succeded !!

How can I do this ?!!

Thanks

Try checking each character that is entered like below:

char ch;

while ((ch = fgetc(stdin)) != '\n')
{
//check if ch is a digit
}

but the input is number (integer), how I will check it ?

but the input is number (integer), how I will check it ?

Try replacing your scanf function with the loop and print each character out:

char ch;

while ((ch = fgetc(stdin)) != '\n')
{
fputc(ch, stdout);
}

Note - When you enter the 'number' in its a string of characters

thank u
but How i will convert it to integer after this ?

thank u
but How i will convert it to integer after this ?

I would get a table of the ascii values and do some arithmetic.

oh, jesus. it's one thing to send the poor guy on a wild goose chase, but another to give him shitty instructions.

look, dude, here you go:

#include <string.h>

char input[32];     // arbitrary size string
char * chk_ptr;     // tests string conversion
int number;         // holds converted number
int done = 0;       // loop control

do {
    printf("enter decimal number: ");           // lack of newline char requires fflush
    fflush(stdout);                             // this ensures the prompt is output to screen
    fgets(input, sizeof(input), stdin);         // get string input
    number = strtol(input, &chk_ptr, 10);       // force decimal conversion

    if (chk_ptr == input)          // non numeric chars at beginning, can't convert at all
        printf("   invalid entry:  not a number\n");
    else if (**chk_ptr != '\0');   // conv. ok, but non numerics at end -- typ. not an "error"
        printf("   invalid entry:  contains non-numeric characters\n");
    else
        done = 1;

} while (!done);

printf("valid numeric entry: %d\n", number);

yeah, that's a nice tutorial... explains things pretty well.

still, i like my code better :P

I might like yours if you'd fix the errors. ;)

[edit]BTW/FYI, one in the links of the tutorial was to a snippet that uses strtol too.

I might like yours if you'd fix the errors. ;)

what?? there is not!

... is there?

O_o

oh, jesus. it's one thing to send the poor guy on a wild goose chase, but another to give him shitty instructions.

So what's wrong with producing this

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>

int main(int argc, char**argv)
{

	char ch;
	const int MULTI = 10;
	int x = 0;

	while (true)
	{
		fputs("enter a number->", stdout);
		while ((ch = fgetc(stdin)) != '\n')
		{
			if (isdigit(ch))
			{
				x = (x * MULTI) + (ch - 48);
			}
			else
			{
				/*do something if it fails*/
				fprintf(stdout, "ch->%c is not a digit\n", ch);
			}
		}
		fprintf(stdout, "ans->%d\n", x);
		x = 0;
	}
	exit(EXIT_SUCCESS);
}

what?? there is not!

... is there?

O_o

You're getting input from fgets , right? If you don't exceed the buffer size, what will the last character be? Are you checking for it? Got any misplaced semicolons? Is that particular comparison even correct syntactically? You might also want to add an #include or two.

And really, would adding the main() wrapper have been that difficult? :P

So what's wrong with producing this

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>

int main(int argc, char**argv)
{

	char ch;
	const int MULTI = 10;
	int x = 0;

	while (true)
	{
		fputs("enter a number->", stdout);
		while ((ch = fgetc(stdin)) != '\n')
		{
			if (isdigit(ch))
			{
				x = (x * MULTI) + (ch - 48);
			}
			else
			{
				/*do something if it fails*/
				fprintf(stdout, "ch->%c is not a digit\n", ch);
			}
		}
		fprintf(stdout, "ans->%d\n", x);
		x = 0;
	}
	exit(EXIT_SUCCESS);
}

ch should be an int .
Prefer '0' to 48 .
It doesn't handle negative values (or those beginning with +).
It has overflow issues.
I prefer C90 to C99 for more generic examples.

You're getting input from fgets , right? If you don't exceed the buffer size, what will the last character be? Are you checking for it? Got any misplaced semicolons? Is that particular comparison even correct syntactically? You might also want to add an #include or two.

And really, would adding the main() wrapper have been that difficult? :P

oops. that misplaced semicolon would be a real bitch for a noob to find. as to the 'main' and the 'stdio' i figured he already had that part.

okay, how's this.

#include <string.h>
#include <stdio.h>
int main(void)
{
    char input[32];     // arbitrary size string
    char * chk_ptr;     // tests string conversion
    int number;         // holds converted number
    int done = 0;       // loop control

    do {
        printf("enter decimal number: ");           // lack of newline char requires fflush
        fflush(stdout);                             // this ensures the prompt is output to screen
        fgets(input, sizeof(input), stdin);         // get string input
        if (input[strlen(input)-1] == '\n')
            input[strlen(input)-1] = '\0';          // get rid of /newline
        number = strtol(input, &chk_ptr, 10);       // force decimal conversion

        if (chk_ptr == input)          // non numeric chars at beginning, can't convert at all
            printf("   invalid entry:  not a number\n");
        else if (**chk_ptr != '\0')   // conv. ok, but non numerics at end -- typ. not an "error"
            printf("   invalid entry:  contains non-numeric characters\n");
        else
            done = 1;

    } while (!done);

    printf("valid numeric entry: %d\n", number);

    return 0;
}

i do believe the comparison is synactically correct. if (**chk_ptr != '\0') is looking to make sure the entire string was converted. if not, then it's pointing to the first non-numeric remaining.

although, i dont normally do this particular check for non-numerics (or newlines) at the end. because normally there is non numerics afterwards and i handle them later, or else i don't care if there is or not. i usually only do the first pointer equality comparison to make sure that something was converted at all.

.

main.c:16: warning: implicit declaration of function `strtol'
main.c:20: error: invalid type argument of `unary *'

I tend to avoid the double-strlen call myself.

commented: thanks :) +1

oh, jeebus.

i forgot stdlib.h, not string.h, is the required library for 'strtol' ... and i just noticed my ** in the strtol return pointer, that doesnt work too well, does it?

okay, make me compile and run my own code; I should have done it in the first place. lesson learned. again. :P

anyhow, now it works. and i think its reasonably strong.

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    char input[32];     // arbitrary size string
    size_t input_len;   // for dave
    char * chk_ptr;     // tests string conversion
    int number;         // holds converted number
    int done = 0;       // loop control

    do {
        printf("enter decimal number: ");           // lack of newline char requires fflush
        fflush(stdout);                             // this ensures the prompt is output to screen
        fgets(input, sizeof(input), stdin);         // get string input
        input_len = strlen(input);
        if (input[input_len-1] == '\n')             // get rid of /newline character
            input[input_len-1] = '\0';             
        number = strtol(input, &chk_ptr, 10);       // convert to int, force decimal 

        if (chk_ptr == input)          // non numeric chars at beginning, can't convert at all
            printf("   invalid entry:  not a number\n");
        else if (*chk_ptr != '\0')   // conv. ok, but non numerics at end -- typ. not an "error"
            printf("   invalid entry:  contains non-numeric characters\n");
        else
            done = 1;

    } while (!done);

    printf("valid numeric entry: %d\n", number);

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.