468,504 Members | 1,986 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

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 8744
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,055 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,055 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,055 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

Post your reply

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

Similar topics

16 posts views Thread by JKop | last post: by
2 posts views Thread by SophistiCat | last post: by
2 posts views Thread by Cranky | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.