473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

number validation

Hi all. I'm writing a program that asks a user to enter two numbers and then outputs which number is larger than the other. The program itself was easy enough however, I also have to validate whether or not it was a number that was entered. Is there a simple way to verify that the numbers entered were actually numbers?

I attempted using this in a if statment with no luck.

if ( !isdigit ( x ) ) /* if x is not a digit */

Also, this is my first post so please accept my apologies if this is posted incorrectly or I was supposed to try something else before posting this.
Nov 13 '07 #1
14 12801
sicarie
4,677 Expert Mod 4TB
Can you post the libraries (includes) you are using?

Also, you can try referring to this.
Nov 13 '07 #2
Can you post the libraries (includes) you are using?

Also, you can try referring to this.



I actually looked at the link that you sent me prior to submitting my post. I seem unable to use either statements in if/else format.

if ( !isdigit ( x ) )

printf("That is not a number \n");

Else

(run the rest of the program)

Below are the libraries I've included.

#include <stdio.h>

#include <ctype.h>

#include <string.h>
Nov 13 '07 #3
I checked out the other link you sent, but it seems more for character validation. Below is my code. I thought there would be a simple way to validate whether or not data entered was a number? If not I can try something else if you have any suggestions.


BTW thanks so much for your help.




Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3.  
  4. #include <ctype.h>
  5.  
  6. #include <string.h>
  7.  
  8.  
  9.  
  10. void main(void)
  11.  
  12. {
  13.  
  14.  
  15. char name[20];
  16. int a,b;
  17.  
  18.  
  19. printf("Enter your name ");/*get users name*/
  20.  
  21. scanf ("%s", name );
  22.  
  23. {
  24.     printf("Enter a the first number \n"); /*get first number*/
  25.  
  26.     scanf("%d", &a);
  27.  
  28.     printf("Enter the second number \n"); /*get second number*/
  29.  
  30.     scanf("%d", &b);
  31.  
  32. if ( !isdigit ( a ) ) /* if a is not a digit */  /********this doesn't seem to work*********/
  33.  
  34. {
  35. printf("this is not a digit");
  36. }
  37.  
  38. else
  39.  
  40.     {
  41.  
  42.     if (a > b) /*if first number is greater than the second number*/
  43.  
  44.     {
  45.         printf("\nHey %s %d", name, b ); 
  46.         printf("\nis smaller than %d\n", a );
  47.  
  48.     } 
  49.  
  50.     else /*if second number is greater than the first number*/
  51.  
  52.     {
  53.         printf("\nHey %s %d", name, a ); 
  54.         printf("\nis smaller than %d\n", b );
  55.     }
  56.  
  57.     }
  58.  
  59. }
  60. }
Nov 13 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
You are using scanf() with an int variable. You must enter an integer otherwise scanf() fails (you never check for success). The contents of a and would be indeterminate.

Change your program to use getchar() into a char and then test the char with isdigit().
Nov 13 '07 #5
I've made some changes but it's still not working. I would like to post my code but I'm unsure how to correctly post it. Can you assist?
Nov 13 '07 #6
sicarie
4,677 Expert Mod 4TB
And we have just the thing to help : http://www.thescripts.com/forum/faq....ask_a_question
Nov 13 '07 #7
When the program is run now I'm able to enter my name then the program bypasses the first question and ends. Not sure why.





Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. #include <ctype.h>
  4.  
  5. #include <string.h>
  6.  
  7. #define ENTER 13
  8.  
  9. void main(void)
  10.  
  11. {
  12.  
  13. char a,b;
  14. char name[20];
  15.  
  16.  
  17.  
  18. printf("Enter your name ");/*get users name*/
  19.  
  20. scanf ("%s", name );
  21.  
  22.  
  23.     printf("Enter a the first number \n"); /*get first number*/
  24.  
  25.     ((a = getchar()) !=ENTER);
  26.  
  27.  
  28.  
  29. if ( !(isdigit(a)) ) /* if a is not a digit */  
  30.  
  31. {
  32. printf("this is not a valid number ");
  33. }
  34.  
  35.  
  36. else
  37.  
  38. {
  39.     printf("Enter the second number \n"); /*get second number*/
  40.  
  41.     ((b = getchar()));
  42.  
  43.  
  44.  
  45.  
  46.     {
  47.  
  48.     if (a > b) /*if first number is greater than the second number*/
  49.  
  50.     {
  51.  
  52.         printf("\nHey %s %d", name, b ); 
  53.         printf("\nis smaller than %d\n", a );
  54.  
  55.  
  56.     } 
  57.  
  58.     else /*if second number is greater than the first number*/
  59.  
  60.     {
  61.  
  62.         printf("\nHey %s %d", name, a ); 
  63.         printf("\nis smaller than %d\n", b );
  64.     }
  65.     }
  66.  
  67.  
  68.  
  69. }
  70. }
Nov 13 '07 #8
Ok, I actually go the data validation to work! But now after the first number is inputted prints and skips the 'enter the second number and prints 50 is smaller that 51.
I think my problem now lies within this code

[PHP] printf("Enter the second number \n"); /*get second number*/

((b = getchar()));[/PHP]

It does not seem to be storing the value entered as the actual number entered.
Is this correct to assume?
Nov 14 '07 #9
sicarie
4,677 Expert Mod 4TB
Is this correct to assume?
It's rarely ever correct to assume. Why not actually test it? (Possibly by printing out b?)
Nov 14 '07 #10
Okay if i enter '1' for 'a' it turns it into '49'. '2' becomes '50'. '3' becomes 51. I'm seeing a pattern here. Which if I'm not mistaken is ascii for all three numbers. Why is [PHP] ((b = getchar()));[/PHP] storing these numbers as ascii? Can this statement store this as the actual number entered?
Nov 14 '07 #11
weaknessforcats
9,208 Expert Mod 8TB
That's because you are using getchar(). This function assumes that when you type a 1 you want printf() with a %c format specifier to print 1. That requires an ASCII character, so your 1 is converted to an ASCII value.

Use scanf() with a %d specifer to keep the 1 as a 1.
Expand|Select|Wrap|Line Numbers
  1. int arg;
  2. arg = scanf("%d", & arg);
  3.  
Nov 15 '07 #12
This does not seem to work with the data validation code.

[PHP]char a,b;
char name[20];
int arg;


printf("Enter your name ");/*get users name*/

gets(name);

printf("Enter a the first number \n"); /*get first number*/

arg = scanf("%d", & arg);

if ( !(isdigit(arg)) ) /* if a is not a digit */

{
printf("this is not a valid number\n");
}

else

{
printf("Enter the second number %d \n",a); /*get second number*/

((b = getchar()));[/PHP]
Nov 15 '07 #13
weaknessforcats
9,208 Expert Mod 8TB
What doesn't work? isdigit() will check to see if arg is 48 through 57.

That is, isdigit() tells you if the value in the int (or char) represents an ASCII digit 0 through 9. That is, a 48 through a 57.
Nov 16 '07 #14
oler1s
671 Expert 512MB
You’re making this too hard, snowman. Moreover, you are making fundamental syntactical mistakes in your more complex attempts. For example, do not use gets. Ever. You never use gets. Let me say it once again, in case you missed it. Never use gets. It’s completely unsafe, as there is no way to stop a buffer overflow.

The correct function in place of gets is fgets.

You’re misunderstanding the use of isdigit. Start with functions like getchar and so on. getchar surprisingly returns an int. Here’s why. Functions don’t necessarily work, because something can go wrong. If you try to ask for user input, it may not work for whatever reason. That’s an error, and the function will tell you that with the return value. However, the error return value has to be something other than a char, right? That means the return type is actually int, making it big enough for a char as well as an extra error value.

You are guaranteed the return value is either a char or that error value. If you look at the documentation, you find that the return value on error is EOF. So first you should check if you actually got an EOF, meaning you have a problem. Then if you have an actual character, go ahead and use isdigit on it. The code then should look something like:

Expand|Select|Wrap|Line Numbers
  1. if ((ch = getchar()) != EOF)
  2. {
  3.     if (isdigit(ch))
  4.     {
  5.     }
  6. }
I wouldn’t go about it this way though. I would use fgets for all three: the name, and the two numbers. When you use fgets, you have a C string. To convert a string into a number, use the function strtol.

EDIT: One more thing, what code editor are you using? Whatever it is, it’s terrible.
Nov 16 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Laphan | last post by:
Hi All I'm after a Tax/VAT number validation script that I can tag onto the onClick event of my submit button before submission. I've seen a lot of these as server side, but I'm trying to keep...
6
by: simina | last post by:
Hi... I have a form with 4 number fields: phone area, phone number, cell area, cell number. I did a function that checks the "number" issue for all 4 fields in the same time (because the code is...
3
by: tdmailbox | last post by:
What is the proper way to set up a validation rule to only allow numbers? I want to allow both 1024 and 1024.25
2
by: Andy Eshtry | last post by:
I need to validate a textbox control using the validation control so that user can enter only string (character) type not numbers I used comparevalidator and set the "Operator" property to...
6
by: JIM.H. | last post by:
How can I grantee that user enter only number in the textbox. If possible accept ","and "." in the number. Can I do this?
9
by: campbellwarren | last post by:
Does anyone know how I could limit the number of rows allowed in a MS Access table... want to limit it to 1.
2
by: Jeff | last post by:
hey asp.net 2.0 I have a textbox on my webpage. I want to validate that the user has entered a number and not text. So I added a RegularExpressionValidator to the form, and set its...
2
by: voroojak | last post by:
Hi i just want the user can enter number not any thing else in form. i tried >like "*#*"< in validation validation rule area and it seems that it is working and put just >accept number< in...
19
Frinavale
by: Frinavale | last post by:
Filtering user input is extremely important for web programming. If input is left unfiltered users can input malicious code that can cripple your website. This article will explain how to make...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.