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.
9 9902
scan input as string then check if exists any non-numeric character
if exists then reject, otherwise accept
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?
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.
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.
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.
Banfa 9,065
Expert Mod 8TB
OK here are my comments -
do {
-
printf("Input a positive integer: ");
-
fgets(line, MAXLINE, stdin);
-
- /* all the scanf family of functions are best avoided if possible
-
they just cause problems */
-
error = sscanf(line, "%d", &n) != 1 || n <= 0; [b]
-
-
if (error)
-
printf("\nERROR: Do it again.\n");
-
- /* you are attempting to use an int (n) as a pointer this will not work
-
you need to call isdigit on the characters of the string (line) */
-
for (i = 0; *n != '\0'; n++)}
- /* Statmement makes no sense because error; does nothing (but is compilable)
-
In this kind of test you need to assume it passes at the start and set
-
an error flag if it fails but your if statement tests for the success condition */
-
if (isdigit(*n)){
-
error;}
-
} while (error);
-
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.
OK here are my comments -
do {
-
printf("Input a positive integer: ");
-
fgets(line, MAXLINE, stdin);
-
- /* all the scanf family of functions are best avoided if possible
-
they just cause problems */
-
error = sscanf(line, "%d", &n) != 1 || n <= 0; [b]
-
-
if (error)
-
printf("\nERROR: Do it again.\n");
-
- /* you are attempting to use an int (n) as a pointer this will not work
-
you need to call isdigit on the characters of the string (line) */
-
for (i = 0; *n != '\0'; n++)}
- /* Statmement makes no sense because error; does nothing (but is compilable)
-
In this kind of test you need to assume it passes at the start and set
-
an error flag if it fails but your if statement tests for the success condition */
-
if (isdigit(*n)){
-
error;}
-
} while (error);
-
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.
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 -
char *pEnd;
-
long l;
-
char test[] = "1234";
-
-
l = strtol(test, &pEnd, 10);
-
-
if (*pEnd != '\0')
-
{
-
// error
-
}
-
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 -
char *pEnd;
-
long l;
-
char test[] = "1234";
-
-
l = strtol(test, &pEnd, 10);
-
-
if (*pEnd != '\0')
-
{
-
// error
-
}
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
by: JKop |
last post by:
Take a class like the following:
class Finger
{
public:
double length;
}
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |