views:

100

answers:

3

The user types a string, possibly separated by tabs, spaces and "enters" (CRs). I need to receive all of it; the problem is that gets() function stops the scan when the user presses the "Enter" key. Is there another way to do it? I cannot use any other function except for scanf and gets.

+1  A: 

First of all: gets() is really evil. DONT EVER USE IT.

It is not possible to write a correct program using gets().

Consider read(), getchar(), ...

Johannes Weiß
Calling things evil is unhelpful. DONT EVER DO IT. It would be more helpful if you explained the shortcomings of `gets()` (buffer overflows, stripping of newlines) so that the OP could take knowledge to her superiors to push for an alternative.
P Daddy
Calling gets() evil is accurate. The normal recommendation is fgets(), of course. An explanation of why would improve things, though.
Jonathan Leffler
The infamous Morris worm, exploited the gets function within the unix's finger program and overwrote the stack and that is how it spread like wildfire, the rest they say is history.
tommieb75
That gets is evil is well known and need not be explain yet again. A quick google search will easily answer this question.
Alex
Also, n1336 (http://www.open-std.org/jtc1/sc22/wg14/www/projects#9899) deprecates `gets`, so it might *finally* be removed from the C standard. Of course, support for C1X might take 10+ years, so who knows when it will be actually removed from implementations :-),
Alok
+1  A: 

Use a loop. Keep getcing (putting the results into some large-enough buffer), until you encounter EOF.

Anon.
Using loops is not allowed also.
Lady M
If you *have* to use `gets`, you're going to need to repeatedly call it until you've got all the data. How does your professor expect you to do that without a loop?
Anon.
Probably recursion, but I'm trying my hardest to avoid that. If there's no other solution, I guess I'm gonna have to use recursion.
Lady M
You could write your iterative version, than convert it to tail-call recursive easily enough. Keeps to the letter of "no loops" while completely violating the spirit of it.
Anon.
A: 

Why do you need the "Enter"? You know by the fact that gets returned that a carriage return (or end of file) was detected.

Trent