On 31 Dec 2005 16:13:51 -0800, "alternativa" <alternativa.4@wp.pl>
wrote in comp.lang.c:
[color=blue]
> Hi,
> I have problem with the following function - it was intended to ask a
> user for a 4-digits number:
>
> double ask_for_number (void)
> {
> char *notint;
> char s2[3];[/color]
This is not a large enough array to hold four digits of input. It has
three elements, s2[0], s2[1], and s2[2]. It can either hold three
arbitrary character values -- not a string -- or a string consisting
of two characters, with the '\0' that terminates the string being in
the third and final char.
[color=blue]
> double entered_number;
>
> do
> {
> printf("\tplease enter 4-digits number: ");
> scanf("%s", s2);[/color]
Using the scanf "%s" conversion specifier without a length parameter
is just plain suicidal. scanf() will attempt to pull characters from
the standard input stream and store them in successive memory
locations, then store a terminating '\0' at the end. If the input
contains four characters, the first three will wind up in your array,
then scanf() will try to write the fourth character and the
terminating '\0' into memory that does not belong to you, past the end
of the array. This is undefined behavior.
And what do you think happens if the user types forty or fifty
characters?
[color=blue]
> if ( strlen (s2) !=4 )[/color]
Now you are asking the strlen() function to access memory past the end
of your array.
[color=blue]
> printf ("Wrong input - input must consist of 4 integer
> numbers.\n");
> else
> entered_numberr = strtod(s2, ¬int);
> }
> while (*notint);
> printf ("you entered: %d", entered_number); /* ???? */
> return entered_number;
> };
>
> My question is: why in the line /* ???? */ we got something else that
> input number? I'd like to use entered_number in the other piece of
> program, so I have to be sure it works properly... I guess this is a
> matter of a strod function - could you give me a clue how it works or
> how to write some similar function without strod?
> best regards,
> a.[/color]
There is no requirement that anything at all be output by that line.
In fact, there is no requirement that the program even reach that
point. If the array overflows, there is undefined behavior and there
are no requirements at all any more, there is no right or wrong
behavior. But if there is no undefined behavior, strlen() on the
input array will return either 0, 1, or 2, so your program will never
reach the line.
And, as Keith pointed out, your are passing a double to printf() with
a "%d" conversion specifier, which is for int values. You need to use
"%f" to output a double.
But the bigger problem is your use of an array that's too small, your
program logic insisting that the input is not valid unless it
overflows the array, and using scanf("%n") without a length modifier.
To fix your program, you need to change the definition of s2 from [3]
to [5] elements. Then you need to use "%4s", as a conversion
specifier to scanf() so it will accept only four characters, and still
have room for the '\0'.
And finally you need to use "%f" in place of "%d" as the conversion
specifier to printf().
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://c-faq.com/
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html