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.
14 12801
Can you post the libraries (includes) you are using?
Also, you can try referring to this.
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>
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. -
-
#include <stdio.h>
-
-
#include <ctype.h>
-
-
#include <string.h>
-
-
-
-
void main(void)
-
-
{
-
-
-
char name[20];
-
int a,b;
-
-
-
printf("Enter your name ");/*get users name*/
-
-
scanf ("%s", name );
-
-
{
-
printf("Enter a the first number \n"); /*get first number*/
-
-
scanf("%d", &a);
-
-
printf("Enter the second number \n"); /*get second number*/
-
-
scanf("%d", &b);
-
-
if ( !isdigit ( a ) ) /* if a is not a digit */ /********this doesn't seem to work*********/
-
-
{
-
printf("this is not a digit");
-
}
-
-
else
-
-
{
-
-
if (a > b) /*if first number is greater than the second number*/
-
-
{
-
printf("\nHey %s %d", name, b );
-
printf("\nis smaller than %d\n", a );
-
-
}
-
-
else /*if second number is greater than the first number*/
-
-
{
-
printf("\nHey %s %d", name, a );
-
printf("\nis smaller than %d\n", b );
-
}
-
-
}
-
-
}
-
}
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().
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?
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. - #include <stdio.h>
-
-
#include <ctype.h>
-
-
#include <string.h>
-
-
#define ENTER 13
-
-
void main(void)
-
-
{
-
-
char a,b;
-
char name[20];
-
-
-
-
printf("Enter your name ");/*get users name*/
-
-
scanf ("%s", name );
-
-
-
printf("Enter a the first number \n"); /*get first number*/
-
-
((a = getchar()) !=ENTER);
-
-
-
-
if ( !(isdigit(a)) ) /* if a is not a digit */
-
-
{
-
printf("this is not a valid number ");
-
}
-
-
-
else
-
-
{
-
printf("Enter the second number \n"); /*get second number*/
-
-
((b = getchar()));
-
-
-
-
-
{
-
-
if (a > b) /*if first number is greater than the second number*/
-
-
{
-
-
printf("\nHey %s %d", name, b );
-
printf("\nis smaller than %d\n", a );
-
-
-
}
-
-
else /*if second number is greater than the first number*/
-
-
{
-
-
printf("\nHey %s %d", name, a );
-
printf("\nis smaller than %d\n", b );
-
}
-
}
-
-
-
-
}
-
}
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?
Is this correct to assume?
It's rarely ever correct to assume. Why not actually test it? (Possibly by printing out b?)
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?
That's because you are using get char(). 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. -
int arg;
-
arg = scanf("%d", & arg);
-
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]
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.
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: - if ((ch = getchar()) != EOF)
-
{
-
if (isdigit(ch))
-
{
-
}
-
}
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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...
|
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?
|
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.
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |