[ back to toc ]

do...while problem

Date: 2002/03/25 13:06

Q:
Dear Sir,

Below is my program. I want to know why the program can't restrict the
user inputting the letters if the equal sign removed in the while
statement as below:

while((num<0 || num >255));

However it is work if use while((num<=0 || num >=255));, but it make that
the user can't input 0. Can I have some hint of the solution.

Best Regards,

*NAME-DELETED*

#include <stdio.h>
#include <stdlib.h>

main () {
int bits[8];
int num,k;

do{
printf("Give a number(0-256):");
scanf("%d", &num);
if ((num<=0 || num >=256))
printf("Wrong number! Input again.\n");
}while((num<=0 || num >=256));

for(k=0;k<8;k++){
bits[k] = num %2;
num = num /2;
};
for(k=7;k>=0;k--){
printf("%d",bits[k]);
};
printf("\t");
}

A:
If the user enters letters and not digits then the function 'scanf'
results zero in the variable 'num'. How doy ou want to handle that? Your
code this way requires the user to type some number between 0 and 256
including the limits (why 256 by the way, didn't you mean 255?).

The function scanf is difficult to handle because it may not read the
terminatin new line. I recommend that you allocate a buffer, like

char buffer[80];

read a line using the function 'gets' and then use the function 'sscanf'
to get the number from the string.

regards,
Peter
Q:
Dear Peter,

Thanks for your help, but the code I gave you yesterday is not the
original one. The correct one is as below, sorry for that.

Actually, I found that gets() is not help in this situation, would you
mind helping me again?

Thank you very much!

Best regards,

*NAME-DELETED*

#include <stdio.h>
#include <stdlib.h>

main () {
int bits[8];
int num,k;
char input[1000];

do{
printf("\nGive a number(0-255):");
gets(input);
num=atoi(input);
if (num<=0 || num >=255)
printf("\nWrong number! Input again.");
}while(num<=0 || num >=255);

for(k=0;k<8;k++){
bits[k] = num %2;
num = num /2;
};
for(k=7;k>=0;k--){
printf("%d",bits[k]);
};
printf("\t");
}

A:
Insert a line

do{
printf("\nGive a number(0-255):");
gets(input);
num=atoi(input);

printf("%s %d\n",input,num);// inserted line

if (num<=0 || num >=255)
printf("\nWrong number! Input again.");
}while(num<=0 || num >=255);

and see what the program does.

Also I do not get the point why you exclude the zero or 255 as values. Why
do not you write

}while(num<0 || num >255);

Maybe I am missing the problem you have.

Regards,
Peter
Q:
Dear Peter,

Thanks for your help first, but there is still a problem. When the code is
compiled and you can input 0-255, but also letter. I want to have the
result that the "Wrong number! Input again." will prompt if the user
inputting letter.

Could I have solution? Or that is the problem in C?

Best Regards

*NAME-DELETED*
A:
Now I see what you want to achive.

The issue is that both scanf and atoi returns zero if the number is not
correct and none of them shows by any means that the number is incorrect.

You should use a bit more complicated function called strtol. Here is the
manual page:

regards,
Peter
STRTOL(3) Linux Programmer's Manual STRTOL(3)

NAME
strtol - convert a string to a long integer.

SYNOPSIS
#include <stdlib.h>

long int strtol(const char *nptr, char **endptr, int base);

DESCRIPTION
The strtol() function converts the string in nptr to a
long integer value according to the given base, which must
be between 2 and 36 inclusive, or be the special value 0.

The string must begin with an arbitrary amount of white
space (as determined by isspace(3)) followed by a single
optional `+' or `-' sign. If base is zero or 16, the
string may then include a `0x' prefix, and the number will
be read in base 16; otherwise, a zero base is taken as 10
(decimal) unless the next character is `0', in which case
it is taken as 8 (octal).

The remainder of the string is converted to a long int
value in the obvious manner, stopping at the first charac­
ter which is not a valid digit in the given base. (In
bases above 10, the letter `A' in either upper or lower
case represents 10, `B' represents 11, and so forth, with
`Z' representing 35.)

If endptr is not NULL, strtol() stores the address of the
first invalid character in *endptr. If there were no dig­
its at all, strtol() stores the original value of nptr in
*endptr. (Thus, if *nptr is not `\0' but **endptr is `\0'
on return, the entire string is valid.)

RETURN VALUE
The strtol() function returns the result of the conver­
sion, unless the value would underflow or overflow. If an
underflow occurs, strtol() returns LONG_MIN. If an over­
flow occurs, strtol() returns LONG_MAX. In both cases,
errno is set to ERANGE.

ERRORS
ERANGE The given string was out of range; the value con­
verted has been clamped.

CONFORMING TO
SVID 3, BSD 4.3, ISO 9899

SEE ALSO
atof(3), atoi(3), atol(3), strtod(3), strtoul(3)

BUGS
Ignores the current locale.

GNU 10 June 1995 1

[ back to toc ]