tags:

views:

291

answers:

3

input value 123 -- this value is integer, and valid

input value 1b23a -- this value is invalid

How do I detect which values are valid and not?

Here is my code:

#include <stdio.h>
#include <conio.h>
void main()
{
    char str1[5],str2[5];
    int num,num1,i;
    num=0;
    clrscr();
    printf("Enter the Number ");
    scanf("%s",str1);
    for(i=0;str1[i]!='\0';i++)
    {
        if(str1[i]>=48&&str1[i]<=56)
            num=num1*10+(str[i]-48);
        else
        {
            printf("The value is invalid ");
        }
    }
    printf("This Number is %d",num);
    getch();
}
A: 
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[5],str2[5];
int num,num1,i;
num=0;
clrscr();
printf("Enter the Number ");
scanf("%s",str1);
for(i=0;str1[i]!='\0';i++)
if(str1[i]>=48&&str1[i]<=56)
num=num1*10+(str[i]-48);
else
{
printf("The value is invalid ");
}
}
printf("This Number is %d",num);
getch();
}
bachchan
I inserted this code to your question. You can delete this answer.
mouviciel
… and don't post answers to your own questions, unless nobody else can answer and you can!
Potatoswatter
On the contrary, you can absolutely answer your own questions. And if you do answer your own question, you're NOT supposed to put it in your question. It's in the FAQ somewhere folks.
wds
+1  A: 

Please see this answer regarding use of strtol(). It is a safe way to convert arbitrary input that should be a string representation of an integer, while also saving 'garbage' bytes for additional analysis.

Using it, your code would look something like this:

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#ifdef LINUX_VERSION
#include <curses.h>
#else
#include <conio.h>
#endif

#define BUFF_SIZE 1024

int main(void)
{
    char str1[BUFF_SIZE], *garbage = NULL;
    long num = 0;

    printf("Enter the Number ");
    scanf("%s",str1);

    errno = 0;

    num = strtol(str1, &garbage, 0);
    if (errno) {
       printf("The number is invalid\n");
       return 1;
    }

    printf("You entered the number %ld\n", num);
    if (garbage != NULL) {
        printf("Additional garbage that was ignored is '%s'\n", garbage);
    }
    getch();
    return 0;
}

This doesn't fix everything that is questionable about what you posted, but it should help you get off to a better start.

Output is:

tpost@tpost-desktop:~$ ./t 
Enter the Number 1234abdc 
You entered the number 1234
Additional garbage that was ignored is 'abdc'

Compiled via:

gcc -Wall -DLINUX_VERSION -o t t.c -lcurses

I'm not sure what platform you are using, so additional fixes to the code may be needed.

Tim Post
A: 

One way is to use sscanf and check that there are no characters following the number. This is done most easily by adding a %c on the end and testing the return code, like this:

const char *yourString = ...;
int theValue, dummy;
if (sscanf(yourString, "%d%c", &theValue, &dummy) == 1) {
    // Was a pure number, parsed into 'theValue'
} else {
    // Either no number or had junk after it
}
Donal Fellows
I do seriously recommend against using sscanf for anything. Really. Search for "sscanf crash" and look at all the pages returned. "The GNU C Programming Tutorial" (http://crasseux.com/books/ctutorial/index.html) says: "The sscanf function is just like the deprecated parent scanf function, .... The scanf function is considered dangerous for a number of reasons.". Please do not use it.
hlovdal
@hlovdal: You're incompetent. I didn't recommend `scanf`, but rather `sscanf`, and the case it's dealing with (no string parsing, correctly paired format string and variables) is safe. Indeed, if you'd *bothered* to read that link you posted you'd see that it recommends the use of `sscanf`. (It's trivial to prevent buffer overflows – you can bound the size of strings – and letting people specify the format directly was always daft.)
Donal Fellows
I cite http://crasseux.com/books/ctutorial/Formatted-string-input.html from that tutorial as proof of what I said.
Donal Fellows