|
From: Jonathan W. <jon...@gm...> - 2007-07-17 09:04:51
|
Hi, Multiple things are happening here: As Adam said, the getchar() will only take one character, the <CR> then gets eaten by the gets(), terminating the string. Also, the gets() will not (and cannot) check the length of the string the user enters against the length of your buffer, therefore mangling your data if the user enters a too-long string. A solution is to use fgets(). See here for an explanation http://www.gidnetwork.com/b-56.html On top of that, you will probably notice with some debugging prints that the letter you type before <CR> is only given to getchar() when you type <CR>. This is because windows (and most OS) only flushes the buffer when lots of data has been sent, or <CR> is pressed. There must be a way to change this behavior, but I'm not familiar with it (try looking for unbuffered stdin) This modification shows what happens with your program: try typing "azerty<CR>" and see what happens #include<stdio.h> int main() { char str[10]; char c; puts("press a key"); c = getchar(); printf("you pressed %c\n",c); puts("enter a string"); gets (str); printf ("you typed %s\n", str); return 0; } However, Adam you are wrong about the allocation of char str[10]: this data will be allocated on the stack, along with other local variables. You can't check whether it failed or not: if there's anough room, ok, if not, the program will have already crashed (stack overflow) before you can do anything (this is why we use malloc() for large amounts of memory, which indeed needs checking) Cheers, Jonathan On 7/16/07, Adam Jones < aj...@i-...> wrote: > > >>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 > -- <Morpheus> linux, c'est une question de VI ou de MORE |