Re: User Input issue
- From: santosh <santosh.k83@xxxxxxxxx>
- Date: Tue, 02 Oct 2007 23:36:59 +0530
n3o wrote:
Hello Comp.Lang.C Members,
I have an issue with user input that I have been trying to figure out
for the longest. For instance, let's say you have something like this:
void foo()
{
int num;
printf("Please enter a number: ");
Unless you terminate the string with a newline the output might not appear
immediately. Alternatively you can also call fflush on the stream.
scanf("%d", &num); // yes I know this function may throw a
warning with some compilers (MSVC++ 8.0)
The diagnostic is not required by the Standard, so it a QoI issue. Also as
you can see C++ style comments tend to break when posted to Usenet. Also
some compilers under certain switches cannot compile code containing them.
...
}
Stylistically I prefer a return statement to falling of the end.
now we know that the int data type uses 4 bytes of memory.
No. This is not specified by the Standard. An int has to be at least of 16
bits but could be anything more, 24 bit, 32 bit, 64 bit and other sizes are
common.
Let's say
the user enters a number that exceeds the minimum/maximum size the int
data type can hold. This would be considered a bug and would cause the
program to function in an innappropriate manner.
Presumably.
To be more specific,
let's say that the programmer wants a real number meaning that they do
not want floating point numbers.
Huh?
If that is the case, we know that by
just changing the data type of 'num' to double would not solve the
issue for a specific reason which I will explain in a minute. Also, by
changing the data type of, 'num,' to long int would not solve the
issue either. Why? long int uses 4 bytes just like int, and double,
which is not what the programmer wants in this instance, uses 8 bytes.
None of your assumptions regarding the size of types are mandated by the
Standard. A long has to be at least 32 bits while a double could be
anywhere from 32 to 96 or more bits depending on the implementation.
Therefore, if someone could guide me as to how I can fix this minor
problem, I would really appreciate it greatly. But, now that I think
of it, if I make 'num' a double, is it possible to type cast 'num' to
an int data type so that the 'num' will output as a whole number with
no decimal number?
In C the term is cast and you don't need to do that, since casting a
floating type to an integer type might involve undefined behaviour if the
integer type is not big enough to hold the double's value. Just round the
double to the nearest whole number and print it out.
Also, just as a sidenote, I know that I can use
either a C-style string or C++ string
No. If you use anything from C++, then your code becomes C++. Consult on
comp.lang.c++. Interaction between C and C++ is subtle and involved.
and then use the stdlib header
to convert the string to an integer which would solve this little
problem that I have been having with user input, however, I am not
allowed to use such a technique because my Computer Science
programming instructor told me not to use strings since we have not
covered strings yet (I'm a college freshman in a Programming I/Novice
Programming course, although I should be in a higher level CS course
but that's irrelavent at this moment in time.) Therefore, I would like
some guidance as to what functions/implementations I could use that
DOES NOT use strings, that would control the use of oversizing a data-
type; exceeding the maximum/minimum size that a data type can hold.
The scanf family of functions are your only choice if you cannot use
strings. If the value read by scanf is too big for the object in question
then the behaviour is undefined. Usually you'll get garbage values.
A more robust solution is to read in the value as a string with fgets and
convert it with strtol or strtod. These functions indicate when they are
unable to convert the value. Unfortunately you are apparently prohibited
from using them.
My advice is to use either one of long, unsigned long, long long, unsigned
long long, double or long double, depending on your exact needs and
availability and inform the user of the maximum possible value when
prompting for input. For non-interactive input there is no way, given your
restrictions, against overflow or wrap-around. The ranges of these types
are found in limits.h and float.h
.
- Follow-Ups:
- Re: User Input issue
- From: Peter J. Holzer
- Re: User Input issue
- From: n3o
- Re: User Input issue
- References:
- User Input issue
- From: n3o
- User Input issue
- Prev by Date: Re: C (functional programming) VS C++ (object oriented programming)
- Next by Date: Re: C (functional programming) VS C++ (object oriented programming)
- Previous by thread: User Input issue
- Next by thread: Re: User Input issue
- Index(es):
Relevant Pages
|