Discussion:
Some C Problems and their Solutions
(too old to reply)
Barry Schwarz
2010-12-09 04:54:08 UTC
Permalink
On Tue, 7 Dec 2010 20:02:15 -0800 (PST), HumbleWorker
<***@gmail.com> wrote:

>Can anybody go through these solutions and give me a feedback. These
>compile on Visual C++ compiler. Some of them are in C language, so you
>should use .c extension. All these problems are on _cgets function.
>
>http://www.hoven.in/hoven.ashx?I=n90

In example 1:

Why does your function set cBuff[0] to the wrong value?
If the length of input is not 5, why does you your function
waste time examining the characters?
Why does your function try to remove "excess input" when the
specification for _cgets says it already did so?
Why, when attempting to do so does your function call _gets
with an incorrect value in cBuff[0].
What makes you think cReturn[0] will ever be 0?
Why does your main function return 0 when invalid input is
detected?

In example 2:

Why does you comment address digits when the requirement is to
enter a word?

In example 4:

Why is there a mismatch between the format specification and
the value passed to printf?

In example 5:

Where is the range check performed?

--
Remove del for email
HumbleWorker
2010-12-09 12:28:53 UTC
Permalink
Schwarz !

Thanks for the inputs. We can discuss the first example.

On Dec 9, 9:54 am, Barry Schwarz <***@dqel.com> wrote:
> Why does your function set cBuff[0] to the wrong value?
> Why does your function try to remove "excess input" when the
> specification for _cgets says it already did so?
Have you tried running this code in VC++ compiler with cBuff [0] =
MAXCHARS ? And by commenting off the code for "excess input" ? I think
we have to keep a provision for CR-LF also. The code doesnot run
correctly if cBuff [0] = MAXCHARS and we donot flush the excess input

> If the length of input is not 5, why does you your function
> waste time examining the characters?
It is a mistake. Thanks.

> What makes you think cReturn[0] will ever be 0?
cReturn [0] is NULL if CR-LF is read.

> Why does your main function return 0 when invalid input is
> detected?
Where is it returning 0 in example 1 ?
Barry Schwarz
2010-12-11 05:33:02 UTC
Permalink
On Thu, 9 Dec 2010 04:28:53 -0800 (PST), HumbleWorker
<***@gmail.com> wrote:

>
>Schwarz !
>
>Thanks for the inputs. We can discuss the first example.
>
>On Dec 9, 9:54 am, Barry Schwarz <***@dqel.com> wrote:
>> Why does your function set cBuff[0] to the wrong value?
>> Why does your function try to remove "excess input" when the
>> specification for _cgets says it already did so?
>Have you tried running this code in VC++ compiler with cBuff [0] =
>MAXCHARS ? And by commenting off the code for "excess input" ? I think
>we have to keep a provision for CR-LF also. The code doesnot run
>correctly if cBuff [0] = MAXCHARS and we donot flush the excess input

No, I didn't run your code. I took you at your word. So you knew the
function description was wrong along with the sample code but didn't
see the need to mention it.

>
>> If the length of input is not 5, why does you your function
>> waste time examining the characters?
>It is a mistake. Thanks.
>
>> What makes you think cReturn[0] will ever be 0?
>cReturn [0] is NULL if CR-LF is read.

NULL has a special meaning. You are probably correct that cReturn[0]
will be 0 if the only input is the ENTER key. There is no CR-LF; that
is an artifact of Microsoft's disk philosophy. The run-time library
will convert the ENTER key to '\n'.

>
>> Why does your main function return 0 when invalid input is
>> detected?
>Where is it returning 0 in example 1 ?

You do know that main is implicitly defined in your code to return an
int? Falling through the final }in main returns 0 to the operating
system.

--
Remove del for email
Barry Schwarz
2010-12-11 17:57:18 UTC
Permalink
On Dec 7, 8:02 pm, HumbleWorker <***@gmail.com> wrote:
> Can anybody go through these solutions and give me a feedback. These
> compile on Visual C++ compiler. Some of them are in C language, so you
> should use .c extension. All these problems are on _cgets function.
>
> http://www.hoven.in/hoven.ashx?I=n90

You may want to find a different site since this one does not seem to
know what the function does. Here is the description from the web
site:

<quote>
Return:
======

The function returns the address of the buffer where the console
input is stored after reading.

Comments:
========

The function reads the console till the enter key is pressed
and stores only the first 'n' characters in the
input buffer, and NULL terminates it.
<\quote>

And here is the description from VC help:

<quote>
Return Value

_cgets returns a pointer to the start of the string, at buffer[2].
There is no error return.

Parameter

buffer

Storage location for data

Remarks

The _cgets function reads a string of characters from the console and
stores the string and its length in the location pointed to by buffer.
The buffer parameter must be a pointer to a character array. The first
element of the array, buffer[0], must contain the maximum length (in
characters) of the string to be read. The array must contain enough
elements to hold the string, a terminating null character ('\0'), and
two additional bytes. The function reads characters until a carriage-
return – linefeed (CR-LF) combination or the specified number of
characters is read. The string is stored starting at buffer[2]. If the
function reads a CR-LF, it stores the null character ('\0'). _cgets
then stores the actual length of the string in the second array
element, buffer [1]. Because all editing keys are active when _cgets
is called, pressing F3 repeats the last entry.
<\quote>

Don't you think the differences are significant?

How about the sample from the help:

<quote>
/* CGETS.C: This program creates a buffer and initializes
* the first byte to the size of the buffer: 2. Next, the
* program accepts an input string using _cgets and displays
* the size and text of that string.
*/

#include <conio.h>
#include <stdio.h>

void main( void )
{
char buffer[82] = { 80 }; /* Maximum characters in 1st byte */
char *result;

printf( "Input line of text, followed by carriage return:\n");
result = _cgets( buffer ); /* Input a line of text */
printf( "\nLine length = %d\nText = %s\n", buffer[1], result );
}
<\quote>

Notice how the size buffer differs from the web page and your code.
Notice how the use of of buffer[0] and buffer[1] differ from your
code.

It is left as an exercise for the reader to determine which is more
accurate, the web page or the help.
HumbleWorker
2010-12-11 18:26:16 UTC
Permalink
On Dec 11, 10:57 pm, Barry Schwarz <***@yahoo.com> wrote:
> On Dec 7, 8:02 pm, HumbleWorker <***@gmail.com> wrote:
>
> > Can anybody go through these solutions and give me a feedback. These
> > compile on Visual C++ compiler. Some of them are in C language, so you
> > should use .c extension. All these problems are on _cgets function.
>
> It is left as an exercise for the reader to determine which is more
> accurate, the web page or the help.

Schwarz !

Can you help me with a code for the following problem -

Write a C-program based on _cgets that asks a user to enter a five
digit number. The program should return 0 only if the input is a
number with exactly five digits.

Thanks
Barry Schwarz
2010-12-12 06:10:49 UTC
Permalink
On Sat, 11 Dec 2010 10:26:16 -0800 (PST), HumbleWorker
<***@gmail.com> wrote:

>On Dec 11, 10:57 pm, Barry Schwarz <***@yahoo.com> wrote:
>> On Dec 7, 8:02 pm, HumbleWorker <***@gmail.com> wrote:
>>
>> > Can anybody go through these solutions and give me a feedback. These
>> > compile on Visual C++ compiler. Some of them are in C language, so you
>> > should use .c extension. All these problems are on _cgets function.
>>
>> It is left as an exercise for the reader to determine which is more
>> accurate, the web page or the help.
>
>Schwarz !
>
>Can you help me with a code for the following problem -
>
>Write a C-program based on _cgets that asks a user to enter a five
>digit number. The program should return 0 only if the input is a
>number with exactly five digits.

I doubt if anyone will write it for you. Post your attempt and
probably several will provide comments.

--
Remove del for email
HumbleWorker
2010-12-12 13:13:33 UTC
Permalink
On Dec 12, 11:10 am, Barry Schwarz <***@dqel.com> wrote:
> On Sat, 11 Dec 2010 10:26:16 -0800 (PST), HumbleWorker
>
>
>
>
>
> <***@gmail.com> wrote:
> >On Dec 11, 10:57 pm, Barry Schwarz <***@yahoo.com> wrote:
> >> On Dec 7, 8:02 pm, HumbleWorker <***@gmail.com> wrote:
>
> >> > Can anybody go through these solutions and give me a feedback. These
> >> > compile on Visual C++ compiler. Some of them are in C language, so you
> >> > should use .c extension. All these problems are on _cgets function.
>
> >> It is left as an exercise for the reader to determine which is more
> >> accurate, the web page or the help.
>
> >Schwarz !
>
> >Can you help me with a code for the following problem -
>
> >Write a C-program based on _cgets that asks a user to enter a five
> >digit number. The program should return 0 only if the input is a
> >number with exactly five digits.
>
> I doubt if anyone will write it for you.  Post your attempt and
> probably several will provide comments.
>
> --
> Remove del for email- Hide quoted text -
>
> - Show quoted text -

I just wanted to know whether the code provided in that website
[except for the why does you your function waste time examining the
characters?] can be improved upon, is that optimal ? That code works
fine as per what I have seen on my compiler VC++.
Barry Schwarz
2010-12-12 18:57:55 UTC
Permalink
On Sun, 12 Dec 2010 05:13:33 -0800 (PST), HumbleWorker
<***@gmail.com> wrote:

>On Dec 12, 11:10 am, Barry Schwarz <***@dqel.com> wrote:
>> On Sat, 11 Dec 2010 10:26:16 -0800 (PST), HumbleWorker
>>
>>
>>
>>
>>
>> <***@gmail.com> wrote:
>> >On Dec 11, 10:57 pm, Barry Schwarz <***@yahoo.com> wrote:
>> >> On Dec 7, 8:02 pm, HumbleWorker <***@gmail.com> wrote:
>>
>> >> > Can anybody go through these solutions and give me a feedback. These
>> >> > compile on Visual C++ compiler. Some of them are in C language, so you
>> >> > should use .c extension. All these problems are on _cgets function.
>>
>> >> It is left as an exercise for the reader to determine which is more
>> >> accurate, the web page or the help.
>>
>> >Schwarz !
>>
>> >Can you help me with a code for the following problem -
>>
>> >Write a C-program based on _cgets that asks a user to enter a five
>> >digit number. The program should return 0 only if the input is a
>> >number with exactly five digits.
>>
>> I doubt if anyone will write it for you.  Post your attempt and
>> probably several will provide comments.
>>
>> --
>> Remove del for email- Hide quoted text -
>>
>> - Show quoted text -
>
>I just wanted to know whether the code provided in that website
>[except for the why does you your function waste time examining the
>characters?] can be improved upon, is that optimal ? That code works
>fine as per what I have seen on my compiler VC++.

For some version of fine.

So you think it may be "optimal" for code to compile with diagnostics
and cause the user additional concerns.

So you think it is OK for the code to call printf without a prototype
(since the author forgot to include stdio.h)?

So you think it may be "optimal" to use quotation marks for standard
headers instead of angle brackets so the compiler has to search
additional directories?

So you think it may be "optimal" to call strlen and pay the price for
the function call as well as for the search for the '\0' instead of
using the value _cgets stored in cBuff[1] (about which the author to
have no knowledge)?

So you think it may be "optimal" to remove the unread stream
characters when length is less than 5 (and therefore there cannot be
any unread characters), forcing the user to press ENTER with no prompt
informing the user of why the system has hung?

There is no magic to this. These, and probably others, are issues you
can see for yourself if you read the code even casually, examine the
compiler output, and run the program under the debugger with a variety
of inputs.

--
Remove del for email
Loading...