-1

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();
}
Douwe Maan
  • 6,888
  • 2
  • 34
  • 35
bachchan
  • 313
  • 3
  • 5
  • 14
  • I think you mean: How should I validate an input field to only accepct integers. ie:123 is valid, 1b23a is not valid – Luis Apr 29 '10 at 07:11
  • 1
    Objective C is just a programming language - I suspect you're probably talking about Cocoa development on the Mac or CocoaTouch development on iPhone/iPad ? – Paul R Apr 29 '10 at 07:30
  • If doing this via a GUI choose the correct field in interface builder to only allow numbers – mmmmmm Apr 29 '10 at 10:51
  • 1
    This is clearly not Objective-C / Cocoa code. `conio.h` suggests MS-DOS platform. – mouviciel Apr 29 '10 at 12:41
  • This is also an illegal definition of `main`. There are various possible definitions, but they are all required to have a return type of `int`. – David Thornley Apr 29 '10 at 14:58
  • I have no idea what platform you are using (or compiler), I did my best to show you a portable example. For all I know (due to information given), you could be using Turbo C on DOS. – Tim Post May 04 '10 at 09:12

3 Answers3

1

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.

Community
  • 1
  • 1
Tim Post
  • 33,371
  • 15
  • 110
  • 174
0
#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();
}
Joe
  • 46,419
  • 33
  • 155
  • 245
bachchan
  • 313
  • 3
  • 5
  • 14
  • 2
    I inserted this code to your question. You can delete this answer. – mouviciel Apr 29 '10 at 12:40
  • 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 May 04 '10 at 09:28
-1

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
  • 133,037
  • 18
  • 149
  • 215
  • 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 May 04 '10 at 06:38
  • @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 May 04 '10 at 08:18
  • I cite http://crasseux.com/books/ctutorial/Formatted-string-input.html from that tutorial as proof of what I said. – Donal Fellows May 04 '10 at 08:18