473,395 Members | 1,726 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,395 software developers and data experts.

rejecting an non numberic input in C

17
I was given the following code

char line[MAXLINE];
int error, n;

do {
printf("Input a positive integer: ");
fgets(line, MAXLINE, stdin);
error = sscanf(line, "%d", &n) != 1 || n <= 0;
if (error)
printf("\nERROR: Do it again.\n");
} while (error);


The preceding code works well, but if you input a letter vice a number say for example 22e instead of 223, it should should reject 22e.

I have tried assert, but am not sure that is the correct way to go.

I have even tried int isalpha(int c);

but the way I used it only checked the first character in the string.

Please if anyone could suggest what type of programming to use it would be helpful.
Thank you in advance.
Sep 29 '06 #1
9 10043
ltgbau
41
scan input as string then check if exists any non-numeric character
if exists then reject, otherwise accept
Sep 30 '06 #2
rwise5
17
scan input as string then check if exists any non-numeric character
if exists then reject, otherwise accept
What do I scan a string with to determine if it is acceptable or not?
Oct 1 '06 #3
Banfa
9,065 Expert Mod 8TB
The function

int isdigit(int c);

will return non-zero if the character passed to it is a digit otherwise it returns 0

Loop down your string and check each character of the string individually.
Oct 1 '06 #4
rwise5
17
The function

int isdigit(int c);

will return non-zero if the character passed to it is a digit otherwise it returns 0

Loop down your string and check each character of the string individually.
Thank you I will try what you suggested, I am hoping that I can get it to work without throwing my computer through a wall.
Thank you again.
Oct 1 '06 #5
rwise5
17
Here is what I am thinking about trying....

char line[MAXLINE];
int error, n;

do {
printf("Input a positive integer: ");
fgets(line, MAXLINE, stdin);
error = sscanf(line, "%d", &n) != 1 || n <= 0;
if (error)
printf("\nERROR: Do it again.\n");
for (i = 0; *n != '\0'; n++)}
if (isdigit(*n))
error;}
} while (error);

Does this look like I am thinking correctly.
Thank you.
Oct 2 '06 #6
Banfa
9,065 Expert Mod 8TB
OK here are my comments

Expand|Select|Wrap|Line Numbers
  1. do {
  2.   printf("Input a positive integer: ");
  3.   fgets(line, MAXLINE, stdin);
  4.  
  5. /* all the scanf family of functions are best avoided if possible
  6.    they just cause problems */
  7.   error = sscanf(line, "%d", &n) != 1 || n <= 0; [b]
  8.  
  9.   if (error)
  10.     printf("\nERROR: Do it again.\n");
  11.  
  12. /* you are attempting to use an int (n) as a pointer this will not work
  13.    you need to call isdigit on the characters of the string (line) */
  14.   for (i = 0; *n != '\0'; n++)}
  15. /* Statmement makes no sense because error; does nothing (but is compilable)
  16.    In this kind of test you need to assume it passes at the start and set 
  17.    an error flag if it fails but your if statement tests for the success condition */
  18.     if (isdigit(*n)){
  19.     error;}
  20. } while (error);
  21.  
You are along the right lines but not quite there yet. You will need a variable of type char * to point at line in order to call isdigit with. Once you have done this rather than call sscanf I would call atoi.

Of course now I think about it during daytime instead of the middle of the night I wouldn't bother with sscanf, isdigit and a loop I would just use strtol which will convert the string to an integer and test for errors at the same time.
Oct 2 '06 #7
rwise5
17
OK here are my comments

Expand|Select|Wrap|Line Numbers
  1. do {
  2.   printf("Input a positive integer: ");
  3.   fgets(line, MAXLINE, stdin);
  4.  
  5. /* all the scanf family of functions are best avoided if possible
  6.    they just cause problems */
  7.   error = sscanf(line, "%d", &n) != 1 || n <= 0; [b]
  8.  
  9.   if (error)
  10.     printf("\nERROR: Do it again.\n");
  11.  
  12. /* you are attempting to use an int (n) as a pointer this will not work
  13.    you need to call isdigit on the characters of the string (line) */
  14.   for (i = 0; *n != '\0'; n++)}
  15. /* Statmement makes no sense because error; does nothing (but is compilable)
  16.    In this kind of test you need to assume it passes at the start and set 
  17.    an error flag if it fails but your if statement tests for the success condition */
  18.     if (isdigit(*n)){
  19.     error;}
  20. } while (error);
  21.  
You are along the right lines but not quite there yet. You will need a variable of type char * to point at line in order to call isdigit with. Once you have done this rather than call sscanf I would call atoi.

Of course now I think about it during daytime instead of the middle of the night I wouldn't bother with sscanf, isdigit and a loop I would just use strtol which will convert the string to an integer and test for errors at the same time.
I have researched strtol function and I see no way to check to see if the string is digital or alpha.

I have been thinking maybe I need to be using pointers. ??

do {
printf("Input a positive integer: ");
fgets(line, MAXLINE, stdin);

for (p = line; *p != '\0'; ++p){
if (!isalpha(*p))
error;}
if (error)
printf("\nERROR: Do it again.\n");
} while (error);


I need to work out the Ivalue s and such, tell me what you think.
Thank you.
Oct 4 '06 #8
Banfa
9,065 Expert Mod 8TB
I have researched strtol function and I see no way to check to see if the string is digital or alpha.
Use strtol to convert the string to an integer, the character that EndPtr ends up pointing to should be the string terminator '\0'.

If it isn't then there is an error

Alternitively if it isn't but the rest of the string is white space then there is not an error

Expand|Select|Wrap|Line Numbers
  1. char *pEnd;
  2. long l;
  3. char test[] = "1234";
  4.  
  5. l = strtol(test, &pEnd, 10);
  6.  
  7. if (*pEnd != '\0')
  8. {
  9.   // error
  10. }
  11.  
Oct 4 '06 #9
rwise5
17
Use strtol to convert the string to an integer, the character that EndPtr ends up pointing to should be the string terminator '\0'.

If it isn't then there is an error

Alternitively if it isn't but the rest of the string is white space then there is not an error

Expand|Select|Wrap|Line Numbers
  1. char *pEnd;
  2. long l;
  3. char test[] = "1234";
  4.  
  5. l = strtol(test, &pEnd, 10);
  6.  
  7. if (*pEnd != '\0')
  8. {
  9.   // error
  10. }
  11.  
I couldn't get your suggestion to work. I went back to the pointer function and I figure out what was wrong with the I value.

char *p = line; // This was what I had to get correct or I was stuck

do {
printf("Input a positive integer: ");
fgets(line, MAXLINE, stdin);

for (p = line; *p != '\0'; ++p){
if (!isalpha(*p))
error;}
if (error)
printf("\nERROR: Do it again.\n");
} while (error);

I would like to thank everyone who help me think this out.
Thanks.
Oct 6 '06 #10

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

Similar topics

0
by: gotcha | last post by:
I have a little code to add multiple items to a shopping cart based page. This code works perfect, but it adds all of the info to the same input fields every time it loops. I need it to change...
3
by: david | last post by:
HI! Im trying to make "HTML form" into automatic. 1. If I get 18 numbers like: A B C D E F . . . . 2. How can I put those 18 numbers automatically into 6 numbers format like: A B C D E F
16
by: JKop | last post by:
Take a class like the following: class Finger { public: double length; }
2
by: SophistiCat | last post by:
Hi, I am working on a computational program that has to read a number of parameters (~50) from an input file. The program contains a single class hierarchy with about a dozen member-classes or...
2
by: Cranky | last post by:
Ok, here is my scenario: I need to input numbers using my handheld IPAQ. I figured out how to create an online numeric keypad for inputting numbers into an input field, what I need to know is how...
3
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table...
3
by: cbradio | last post by:
Hi, I am having trouble developing a form in a restricted environment. My sample code is found below my message (sorry I don't have a URL). Basically, without a doctype, the form displays properly...
18
by: Diogenes | last post by:
Hi All; I, like others, have been frustrated with designing forms that look and flow the same in both IE and Firefox. They simply did not scale the same. I have discovered, to my chagrin,...
16
by: raids51 | last post by:
i created a program in VB.NET that connects with tcp to a server... the problem i have is my server keeps rejecting the connections... i have no skill in networkin and i need some help, its probably...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.