+ 3

Why function gets() doesn't work in a playground code? It's about C.

I've just tried to write some code on C in playground. But compilier said that he didn't know such function (gets()). studio.h and string.h was included.

22nd Apr 2020, 8:54 PM
Денис Филь
Денис Филь - avatar
9 Answers
+ 3
You should use fgets() Read this to know why not use use gets(): http://www.cplusplus.com/reference/cstdio/gets/ and here, http://www.cplusplus.com/reference/cstdio/fgets/ on how to use fgets(). edit: ahhhh.....Link does not show how to use the function for user input..so char name[20] fgets(name, 20, stdin);
22nd Apr 2020, 9:07 PM
rodwynnejones
rodwynnejones - avatar
+ 1
Can we see the code? It's works but not recommended to use. Since it is couse misleading results when overflows.. So better to use fgets() instead of gets().
22nd Apr 2020, 9:08 PM
Jayakrishna 🇮🇳
22nd Apr 2020, 9:17 PM
Денис Филь
Денис Филь - avatar
+ 1
Here the code. I think gets more convenient in this case.
22nd Apr 2020, 9:18 PM
Денис Филь
Денис Филь - avatar
+ 1
Yes I now about disadvantages of using gets () vs fgets (). But sometimes gets more convenient.
22nd Apr 2020, 9:23 PM
Денис Филь
Денис Филь - avatar
+ 1
In the code...when your multiplying, you've got what look like an 'x' instead of a '*'...also..... for (i=0;i<=strln; i++){ blah blah } should be: for (int i=0;i<=strln; i++){ blah blah } and the gets() Apart from that....it works.
22nd Apr 2020, 9:28 PM
rodwynnejones
rodwynnejones - avatar
+ 1
And There are other errors.. for (i=0;i<=strln; i++){ //here i is undefined.. So Add int i instead of i only.. Line 24, 25 : unnecessary extra white-spaces, I deleted, And you used x instead * for multiplication.. I edited all these errors.. It's working... char output[strln+1+(2*z)]; output[strln+1+(2*z)]='\0';
22nd Apr 2020, 9:28 PM
Jayakrishna 🇮🇳
+ 1
Oh yeah. Thanx a lot. It works! 👍
22nd Apr 2020, 10:14 PM
Денис Филь
Денис Филь - avatar
+ 1
Never use gets(). You’re asking for bugs that take weeks to track down, that are so easily avoidable.
24th Apr 2020, 1:31 PM
s.f