|
From: Jan M. <jan...@vo...> - 2007-07-17 14:01:42
|
Ok, I write a program which behaves according to the value it
get through getchar (kind of a menu).
So it can follow several branches, some of them without
gets().
Of course I know the str array in this case can be a reason of potential
memory leak but I have sent this code only for explainig
what I mean or need.
I have written all of it should be in a program
I am suppose to do and this is just a part of console front-end for
testing purposes and just for me. The production front-ends versions
will be in a Windows API and Symbian API. So I don't care about some
checking in console front-end and
there will be no getchar() or gets() in production versions.
But thank you for advices.
Jan Mura
----- Original Message -----
From: "Adam Jones" <aj...@i-...>
To: <dev...@li...>
Sent: Monday, July 16, 2007 6:17 PM
Subject: Re: [Dev-C++] Problems with input
> >>Hello,
>
> >>I have got a problem I don't understand very much.
>
> >>I have in my code:
>
> >>#include<stdio.h>
> >>int main()
> >>{
> >>char str[10];
>
> >>getchar ();
> >>gets (str);
> >>printf ("%s\n", str);
> >>return 0;
> >>}
>
> It would be helpful to all of us if you could explain why you feel it
> necessary to use getchar() the gets() the way you do. What are you trying
to
> accomplish by doing it this way.
> getchar() waits for any keypress to continue, and gets will wait until it
> encounters the newline or end of file before continuing.
>
> You also have a potential to corrupt memory here. What happens if the
person
> enters a string longer than 10 Char. You also assume which is not very
good
> that the system will allocate a char str[10] without checking to see if it
> can do it with a malloc first. On memory allocations you need to check
> first, and if it fails exit your program gracefully.
>
> >>In this case it seems the program is ignoring the "gets ()" command.
After
> I press some key and >CR>It stops and doesn't wait for line input. If I
do
> not have there the "getchar () I >>can input a line.
>
> If you hit any key and the CR then the single key press goes to getchar()
> the <CR> goes to the gets() which terminates that call. You should just
hit
> any key, then type in your string and then hit CR when you are done. The
> getchar() does not require you to hit CR in order for it to accept the key
> press. This should work.
>
> -Adam Jones
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
> __________ Informace od NOD32 2399 (20070714) __________
>
> Tato zprava byla proverena antivirovym systemem NOD32.
> http://www.nod32.cz
>
>
|