N
n3o
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: ");
scanf("%d", &num); // yes I know this function may throw a
warning with some compilers (MSVC++ 8.0)
...
}
now we know that the int data type uses 4 bytes of memory. 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. To be more specific,
let's say that the programmer wants a real number meaning that they do
not want floating point numbers. 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.
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? Also, just as a sidenote, I know that I can use
either a C-style string or C++ string 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.
n3o
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: ");
scanf("%d", &num); // yes I know this function may throw a
warning with some compilers (MSVC++ 8.0)
...
}
now we know that the int data type uses 4 bytes of memory. 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. To be more specific,
let's say that the programmer wants a real number meaning that they do
not want floating point numbers. 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.
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? Also, just as a sidenote, I know that I can use
either a C-style string or C++ string 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.
n3o