What is going on with 'gets(stdin)' on the site coderbyte? The Next CEO of Stack OverflowThe Definitive C++ Book Guide and ListWhy does gets(stdin) return an integer? And other errorsWhat is the difference between #include <filename> and #include “filename”?What are the differences between a pointer variable and a reference variable in C++?What does the explicit keyword mean?What is the “-->” operator in C++?What are move semantics?What is the copy-and-swap idiom?What is The Rule of Three?What are the basic rules and idioms for operator overloading?What is a lambda expression in C++11?Why is reading lines from stdin much slower in C++ than Python?

Prodigo = pro + ago?

My ex-girlfriend uses my Apple ID to login to her iPad, do I have to give her my Apple ID password to reset it?

How to pronounce fünf in 45

Is it OK to decorate a log book cover?

Masking layers by a vector polygon layer in QGIS

Free fall ellipse or parabola?

Read/write a pipe-delimited file line by line with some simple text manipulation

What does this strange code stamp on my passport mean?

Gauss' Posthumous Publications?

Ising model simulation

pgfplots: How to draw a tangent graph below two others?

How to find if SQL server backup is encrypted with TDE without restoring the backup

Why doesn't Shulchan Aruch include the laws of destroying fruit trees?

Find a path from s to t using as few red nodes as possible

Could you use a laser beam as a modulated carrier wave for radio signal?

Can you teleport closer to a creature you are Frightened of?

Is the offspring between a demon and a celestial possible? If so what is it called and is it in a book somewhere?

Shortening a title without changing its meaning

Are British MPs missing the point, with these 'Indicative Votes'?

Does the Idaho Potato Commission associate potato skins with healthy eating?

Find the majority element, which appears more than half the time

Why did the Drakh emissary look so blurred in S04:E11 "Lines of Communication"?

What did the word "leisure" mean in late 18th Century usage?

Why did early computer designers eschew integers?



What is going on with 'gets(stdin)' on the site coderbyte?



The Next CEO of Stack OverflowThe Definitive C++ Book Guide and ListWhy does gets(stdin) return an integer? And other errorsWhat is the difference between #include <filename> and #include “filename”?What are the differences between a pointer variable and a reference variable in C++?What does the explicit keyword mean?What is the “-->” operator in C++?What are move semantics?What is the copy-and-swap idiom?What is The Rule of Three?What are the basic rules and idioms for operator overloading?What is a lambda expression in C++11?Why is reading lines from stdin much slower in C++ than Python?










117















Coderbyte is an online coding challenge site (I found it just 2 minutes ago).



The first C++ challenge you are greeted with has a C++ skeleton you need to modify:




#include <iostream>
#include <string>
using namespace std;

int FirstFactorial(int num)

// Code goes here
return num;



int main()

// Keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;





If you are little familiar with C++ the first thing* that pops in your eyes is:



int FirstFactorial(int num);
cout << FirstFactorial(gets(stdin));


So, ok, the code calls gets which is deprecated since C++11 and removed since C++14 which is bad in itself.



But then I realize: gets is of type char*(char*). So it shouldn't accept a FILE* parameter and the result shouldn't be usable in the place of an int parameter, but ... not only it compiles without any warnings or errors, but it runs and actually passes the correct input value to FirstFactorial.



Outside of this particular site, the code doesn't compile (as expected), so what is going on here?




*Actually the first one is using namespace std but that is irrelevant to my issue here.










share|improve this question
























  • Note that stdin in the standard library is a FILE*, and a pointer to any type converts to char*, which is the type of the argument of gets(). However, you should never, ever, ever write that kind of code outside an obfuscated C contest. If your compiler even accepts it, add more warning flags, and if you’re trying to fix a codebase that has that construct in it, turn warnings into errors.

    – Davislor
    Mar 21 at 3:42












  • @Davislor no it doesn't "candidate function not viable: no known conversion from 'struct _IO_FILE *' to 'char *' for 1st argument"

    – bolov
    Mar 21 at 10:14






  • 2





    @Davislor huh, that might be true for ancient C, but definitely not for C++.

    – Quentin
    Mar 21 at 12:49











  • @Quentin Yeah. That shouldn’t compile. The intended challenge might’ve been, “Take this broken code, read my mind about what it’s supposed to do, and fix it,” but in that case there should be a real specification. With test cases.

    – Davislor
    Mar 21 at 14:35






  • 5





    I’m surprised no-one tried this, but gets(stdin ) (with an extra space) produces the expected C++ error.

    – Roman Odaisky
    Mar 21 at 14:58















117















Coderbyte is an online coding challenge site (I found it just 2 minutes ago).



The first C++ challenge you are greeted with has a C++ skeleton you need to modify:




#include <iostream>
#include <string>
using namespace std;

int FirstFactorial(int num)

// Code goes here
return num;



int main()

// Keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;





If you are little familiar with C++ the first thing* that pops in your eyes is:



int FirstFactorial(int num);
cout << FirstFactorial(gets(stdin));


So, ok, the code calls gets which is deprecated since C++11 and removed since C++14 which is bad in itself.



But then I realize: gets is of type char*(char*). So it shouldn't accept a FILE* parameter and the result shouldn't be usable in the place of an int parameter, but ... not only it compiles without any warnings or errors, but it runs and actually passes the correct input value to FirstFactorial.



Outside of this particular site, the code doesn't compile (as expected), so what is going on here?




*Actually the first one is using namespace std but that is irrelevant to my issue here.










share|improve this question
























  • Note that stdin in the standard library is a FILE*, and a pointer to any type converts to char*, which is the type of the argument of gets(). However, you should never, ever, ever write that kind of code outside an obfuscated C contest. If your compiler even accepts it, add more warning flags, and if you’re trying to fix a codebase that has that construct in it, turn warnings into errors.

    – Davislor
    Mar 21 at 3:42












  • @Davislor no it doesn't "candidate function not viable: no known conversion from 'struct _IO_FILE *' to 'char *' for 1st argument"

    – bolov
    Mar 21 at 10:14






  • 2





    @Davislor huh, that might be true for ancient C, but definitely not for C++.

    – Quentin
    Mar 21 at 12:49











  • @Quentin Yeah. That shouldn’t compile. The intended challenge might’ve been, “Take this broken code, read my mind about what it’s supposed to do, and fix it,” but in that case there should be a real specification. With test cases.

    – Davislor
    Mar 21 at 14:35






  • 5





    I’m surprised no-one tried this, but gets(stdin ) (with an extra space) produces the expected C++ error.

    – Roman Odaisky
    Mar 21 at 14:58













117












117








117


13






Coderbyte is an online coding challenge site (I found it just 2 minutes ago).



The first C++ challenge you are greeted with has a C++ skeleton you need to modify:




#include <iostream>
#include <string>
using namespace std;

int FirstFactorial(int num)

// Code goes here
return num;



int main()

// Keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;





If you are little familiar with C++ the first thing* that pops in your eyes is:



int FirstFactorial(int num);
cout << FirstFactorial(gets(stdin));


So, ok, the code calls gets which is deprecated since C++11 and removed since C++14 which is bad in itself.



But then I realize: gets is of type char*(char*). So it shouldn't accept a FILE* parameter and the result shouldn't be usable in the place of an int parameter, but ... not only it compiles without any warnings or errors, but it runs and actually passes the correct input value to FirstFactorial.



Outside of this particular site, the code doesn't compile (as expected), so what is going on here?




*Actually the first one is using namespace std but that is irrelevant to my issue here.










share|improve this question
















Coderbyte is an online coding challenge site (I found it just 2 minutes ago).



The first C++ challenge you are greeted with has a C++ skeleton you need to modify:




#include <iostream>
#include <string>
using namespace std;

int FirstFactorial(int num)

// Code goes here
return num;



int main()

// Keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;





If you are little familiar with C++ the first thing* that pops in your eyes is:



int FirstFactorial(int num);
cout << FirstFactorial(gets(stdin));


So, ok, the code calls gets which is deprecated since C++11 and removed since C++14 which is bad in itself.



But then I realize: gets is of type char*(char*). So it shouldn't accept a FILE* parameter and the result shouldn't be usable in the place of an int parameter, but ... not only it compiles without any warnings or errors, but it runs and actually passes the correct input value to FirstFactorial.



Outside of this particular site, the code doesn't compile (as expected), so what is going on here?




*Actually the first one is using namespace std but that is irrelevant to my issue here.







c++ input gets standards-compliance






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 13:51









Peter Mortensen

13.8k1987113




13.8k1987113










asked Mar 20 at 20:00









bolovbolov

33.4k877141




33.4k877141












  • Note that stdin in the standard library is a FILE*, and a pointer to any type converts to char*, which is the type of the argument of gets(). However, you should never, ever, ever write that kind of code outside an obfuscated C contest. If your compiler even accepts it, add more warning flags, and if you’re trying to fix a codebase that has that construct in it, turn warnings into errors.

    – Davislor
    Mar 21 at 3:42












  • @Davislor no it doesn't "candidate function not viable: no known conversion from 'struct _IO_FILE *' to 'char *' for 1st argument"

    – bolov
    Mar 21 at 10:14






  • 2





    @Davislor huh, that might be true for ancient C, but definitely not for C++.

    – Quentin
    Mar 21 at 12:49











  • @Quentin Yeah. That shouldn’t compile. The intended challenge might’ve been, “Take this broken code, read my mind about what it’s supposed to do, and fix it,” but in that case there should be a real specification. With test cases.

    – Davislor
    Mar 21 at 14:35






  • 5





    I’m surprised no-one tried this, but gets(stdin ) (with an extra space) produces the expected C++ error.

    – Roman Odaisky
    Mar 21 at 14:58

















  • Note that stdin in the standard library is a FILE*, and a pointer to any type converts to char*, which is the type of the argument of gets(). However, you should never, ever, ever write that kind of code outside an obfuscated C contest. If your compiler even accepts it, add more warning flags, and if you’re trying to fix a codebase that has that construct in it, turn warnings into errors.

    – Davislor
    Mar 21 at 3:42












  • @Davislor no it doesn't "candidate function not viable: no known conversion from 'struct _IO_FILE *' to 'char *' for 1st argument"

    – bolov
    Mar 21 at 10:14






  • 2





    @Davislor huh, that might be true for ancient C, but definitely not for C++.

    – Quentin
    Mar 21 at 12:49











  • @Quentin Yeah. That shouldn’t compile. The intended challenge might’ve been, “Take this broken code, read my mind about what it’s supposed to do, and fix it,” but in that case there should be a real specification. With test cases.

    – Davislor
    Mar 21 at 14:35






  • 5





    I’m surprised no-one tried this, but gets(stdin ) (with an extra space) produces the expected C++ error.

    – Roman Odaisky
    Mar 21 at 14:58
















Note that stdin in the standard library is a FILE*, and a pointer to any type converts to char*, which is the type of the argument of gets(). However, you should never, ever, ever write that kind of code outside an obfuscated C contest. If your compiler even accepts it, add more warning flags, and if you’re trying to fix a codebase that has that construct in it, turn warnings into errors.

– Davislor
Mar 21 at 3:42






Note that stdin in the standard library is a FILE*, and a pointer to any type converts to char*, which is the type of the argument of gets(). However, you should never, ever, ever write that kind of code outside an obfuscated C contest. If your compiler even accepts it, add more warning flags, and if you’re trying to fix a codebase that has that construct in it, turn warnings into errors.

– Davislor
Mar 21 at 3:42














@Davislor no it doesn't "candidate function not viable: no known conversion from 'struct _IO_FILE *' to 'char *' for 1st argument"

– bolov
Mar 21 at 10:14





@Davislor no it doesn't "candidate function not viable: no known conversion from 'struct _IO_FILE *' to 'char *' for 1st argument"

– bolov
Mar 21 at 10:14




2




2





@Davislor huh, that might be true for ancient C, but definitely not for C++.

– Quentin
Mar 21 at 12:49





@Davislor huh, that might be true for ancient C, but definitely not for C++.

– Quentin
Mar 21 at 12:49













@Quentin Yeah. That shouldn’t compile. The intended challenge might’ve been, “Take this broken code, read my mind about what it’s supposed to do, and fix it,” but in that case there should be a real specification. With test cases.

– Davislor
Mar 21 at 14:35





@Quentin Yeah. That shouldn’t compile. The intended challenge might’ve been, “Take this broken code, read my mind about what it’s supposed to do, and fix it,” but in that case there should be a real specification. With test cases.

– Davislor
Mar 21 at 14:35




5




5





I’m surprised no-one tried this, but gets(stdin ) (with an extra space) produces the expected C++ error.

– Roman Odaisky
Mar 21 at 14:58





I’m surprised no-one tried this, but gets(stdin ) (with an extra space) produces the expected C++ error.

– Roman Odaisky
Mar 21 at 14:58












3 Answers
3






active

oldest

votes


















150














I'm the founder of Coderbyte and also the guy who created this gets(stdin) hack.



The comments on this post are correct that it is a form of find-and-replace, so let me explain why I did this really quickly.



Back in the day when I first created the site (around 2012), it only supported JavaScript. There was no way to "read in input" in JavaScript running in the browser, and so there would be a function foo(input) and I used the readline() function from Node.js to call it like foo(readline()). Except I was a kid and didn't know better, so I literally just replaced readline() with the input at run-time. So foo(readline()) became foo(2) or foo("hello") which worked fine for JavaScript.



Around 2013/2014 I added more languages and used third-party service to evaluate code online, but it was very difficult to do stdin/stdout with the services I was using, so I stuck with the same silly find-and-replace for languages like Python, Ruby, and eventually C++, C#, etc.



Fast forward to today, I run the code in my own containers, but never updated the way stdin/stdout works because people have gotten used to the weird hack (some people have even posted in forums explaining how to get around it).



I know it is not best practice and it isn't helpful for someone learning a new language to see hacks like this, but the idea was for new programmers to not worry about reading input at all and just focus on writing the algorithm to solve the problem. One common complaint about coding challenge sites years ago was that new programmers would spend a lot of time just figuring out how to read from stdin or read lines from a file, so I wanted new coders to avoid this problem on Coderbyte.



I'll be updating the entire editor page soon along with the default code and stdin reading for languages. Hopefully then C++ programmers will enjoy using Coderbyte more :)






share|improve this answer




















  • 15





    "[B]ut the idea was for new programmers to not worry about reading input at all and just focus on writing the algorithm to solve the problem" - and it didn't occur to you to, instead of writing something that resembles "real" code, just put a made up function name or an obvious placeholder in that spot? Genuinely curious.

    – Ruther Rendommeleigh
    Mar 21 at 8:48






  • 23





    I genuinely didn't expect I was going to choose an answer other than my own when I posted this. Thank you for proving me wrong in such a great way. It's really a pleasure to see your answer.

    – bolov
    Mar 21 at 10:24






  • 4





    Very interesting! I would recommend, if you want to keep this hack, that you replace the function call with something like TAKE_INPUT, then use your find-replace to insert #define TAKE_INPUT whatever_here at the top.

    – Draconis
    Mar 21 at 22:24






  • 11





    We need more answers starting with "I'm the founder of x and also the guy who created this".

    – pipe
    Mar 22 at 8:36






  • 1





    @iheanyi No one asked for it to be perfect. In fact, I'm convinced that almost any placeholder would have been better than something that looks like valid code to any newbie but doesn't actually compile.

    – Ruther Rendommeleigh
    Mar 25 at 8:24


















105














I am intrigued. So, time to put the investigation goggles on and since I don't have access to the compiler or compilation flags I need to get inventive. Also because nothing about this code makes sense it's not a bad idea question every assumption.



First let's check the actual type of gets. I have a little trick for that:



template <class> struct Name;

int main()

Name<decltype(gets)> n;

// keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;




And that looks ... normal:




/tmp/613814454/Main.cpp:16:19: warning: 'gets' is deprecated [-Wdeprecated-declarations]
Name<decltype(gets)> n;
^
/usr/include/stdio.h:638:37: note: 'gets' has been explicitly marked deprecated here
extern char *gets (char *__s) __wur __attribute_deprecated__;
^
/usr/include/x86_64-linux-gnu/sys/cdefs.h:254:51: note: expanded from macro '__attribute_deprecated__'
# define __attribute_deprecated__ __attribute__ ((__deprecated__))
^
/tmp/613814454/Main.cpp:16:26: error: implicit instantiation of undefined template 'Name<char *(char *)>'
Name<decltype(gets)> n;
^
/tmp/613814454/Main.cpp:12:25: note: template is declared here
template <class> struct Name;
^
1 warning and 1 error generated.



gets is marked as deprecated and has the signature char *(char *). But then how is FirstFactorial(gets(stdin)); compiling?



Let's try something else:



int main() 
Name<decltype(gets(stdin))> n;

// keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;




Which gives us:




/tmp/286775780/Main.cpp:15:21: error: implicit instantiation of undefined template 'Name<int>'
Name<decltype(8)> n;
^



Finally we are getting something: decltype(8). So the entire gets(stdin) was textually replaced with the input (8).



And the things get weirder. The compiler error continues:




/tmp/596773533/Main.cpp:18:26: error: no matching function for call to 'gets'
cout << FirstFactorial(gets(stdin));
^~~~
/usr/include/stdio.h:638:14: note: candidate function not viable: no known conversion from 'struct _IO_FILE *' to 'char *' for 1st argument
extern char *gets (char *__s) __wur __attribute_deprecated__;



So now we get the expected error for cout << FirstFactorial(gets(stdin));



I checked for a macro and since #undef gets seems to do nothing it looks like it isn't a macro.



But



std::integral_constant<int, gets(stdin)> n;


It compiles.



But



std::integral_constant<int, gets(stdin)> n; // OK
std::integral_constant<int, gets(stdin)> n2; // ERROR wtf??


Doesn't with the expected error at the n2 line.



And again, almost any modification to main makes the line cout << FirstFactorial(gets(stdin)); spit out the expected error.



Moreover the stdin actually seems to be empty.



So I can only conclude and speculate they have a little program that parses the source and tries (poorly) to replace gets(stdin) with the test case input value before actually feeding it into the compiler. If anybody has a better theory or actually knows what they are doing please share!



This is obviously a very bad practice. While researching this I found there is at least a question here (example) about this and because people have no idea that there is a site out there who does this their answer is "don't use gets use ... instead" which is indeed a good advice but only confuses the OP more since any attempt at a valid read from stdin will fail on this site.




TLDR



gets(stdin) is invalid C++. It's a gimmick this particular site uses (for what reasons I cannot figure out). If you want to continue to submit on the site (I am neither endorsing it neither not endorsing it) you have to use this construct that otherwise would not make sense, but be aware that it is brittle. Almost any modifications to main will spit out an error. Outside of this site use normal input reading methods.






share|improve this answer




















  • 26





    I'm genuinely amazed. Maybe this Q/A can be a canonical post on why not to learn from coding challenge sites.

    – alter igel
    Mar 20 at 20:07






  • 1





    The "8" comes from the left box at the bottom of the screen. Try typing a text string in there (I tried "maplemaple") and see the result...

    – Stobor
    Mar 20 at 20:09






  • 27





    Something really evil is happening, and I think it's at the level of text replacement in the source code outside of the compiler. Try this: std::cout << "gets(stdin)"; and the output is 8 (or whatever you type into the 'input' field. This is a disgraceful abuse of the language.

    – alter igel
    Mar 20 at 20:13







  • 13





    @Stobor note the quotes around "gets(stdin)". That's a string literal that even the preprocessor wouldn't touch

    – alter igel
    Mar 20 at 20:15






  • 2





    To quote James Kirk: "This is damn peculiar."

    – ApproachingDarknessFish
    Mar 21 at 19:49


















60














I tried the following addition to main in the Coderbyte editor:



std::cout << "gets(stdin)";


Where the mysterious and enigmatic snippet gets(stdin) appears inside a string literal. This shouldn't possibly be transformed by anything, not even the preprocessor, and any C++ programmer should expect this code to print the exact string gets(stdin) to the standard output. And yet we see the following output, when compiled and run on coderbyte:



8


Where the value 8 is taken straight from the convenient 'input' field under the editor.



Magic code



From this, it's clear that this online editor is performing blind find-and-replace operations on the source code, substitution appearances of gets(stdin) with the user's 'input'. I would personally call this a misuse of the language that's worse than careless preprocessor macros.



In the context of an online coding challenge website, I'm worried by this because it teaches unconventional, non-standard, meaningless, and at least unsafe practices like gets(stdin), and in a manner that can't be repeated on other platforms.



I'm sure it can't be this hard to just use std::cin and just stream input to a program.






share|improve this answer

























  • and it's not even a blind "find and replace" because sometimes it replaces it sometimes it does not.

    – bolov
    Mar 20 at 20:31






  • 4





    @bolov could it be just the first occurrence of gets(stdin) that is replaced? I meant 'blind' in the sense that it appears to be unaware of the language's syntax or grammar.

    – alter igel
    Mar 20 at 20:39











  • yes, you are right. It replaces the first occurence. I tried putting one before main and that's what I got indeed.

    – bolov
    Mar 20 at 20:54






  • 1





    Further research suggests that that site does it for all languages, not just C++ - python/ruby it uses the function call ("raw_input()" or "STDIN.gets") which would typically return a string from stdin, but ends up doing a string substitution of that string instead. I guess finding a regex match for the getline function was too hard, so they went with gets(stdin) for C/C++.

    – Stobor
    Mar 20 at 23:39






  • 3





    @Stobor dang, you're right. I can confirm this happens for Java too, the line System.out.print(FirstFactorial(s.nextLine()9)); prints 89 even when s is undefined.

    – alter igel
    Mar 20 at 23:41












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55269252%2fwhat-is-going-on-with-getsstdin-on-the-site-coderbyte%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









150














I'm the founder of Coderbyte and also the guy who created this gets(stdin) hack.



The comments on this post are correct that it is a form of find-and-replace, so let me explain why I did this really quickly.



Back in the day when I first created the site (around 2012), it only supported JavaScript. There was no way to "read in input" in JavaScript running in the browser, and so there would be a function foo(input) and I used the readline() function from Node.js to call it like foo(readline()). Except I was a kid and didn't know better, so I literally just replaced readline() with the input at run-time. So foo(readline()) became foo(2) or foo("hello") which worked fine for JavaScript.



Around 2013/2014 I added more languages and used third-party service to evaluate code online, but it was very difficult to do stdin/stdout with the services I was using, so I stuck with the same silly find-and-replace for languages like Python, Ruby, and eventually C++, C#, etc.



Fast forward to today, I run the code in my own containers, but never updated the way stdin/stdout works because people have gotten used to the weird hack (some people have even posted in forums explaining how to get around it).



I know it is not best practice and it isn't helpful for someone learning a new language to see hacks like this, but the idea was for new programmers to not worry about reading input at all and just focus on writing the algorithm to solve the problem. One common complaint about coding challenge sites years ago was that new programmers would spend a lot of time just figuring out how to read from stdin or read lines from a file, so I wanted new coders to avoid this problem on Coderbyte.



I'll be updating the entire editor page soon along with the default code and stdin reading for languages. Hopefully then C++ programmers will enjoy using Coderbyte more :)






share|improve this answer




















  • 15





    "[B]ut the idea was for new programmers to not worry about reading input at all and just focus on writing the algorithm to solve the problem" - and it didn't occur to you to, instead of writing something that resembles "real" code, just put a made up function name or an obvious placeholder in that spot? Genuinely curious.

    – Ruther Rendommeleigh
    Mar 21 at 8:48






  • 23





    I genuinely didn't expect I was going to choose an answer other than my own when I posted this. Thank you for proving me wrong in such a great way. It's really a pleasure to see your answer.

    – bolov
    Mar 21 at 10:24






  • 4





    Very interesting! I would recommend, if you want to keep this hack, that you replace the function call with something like TAKE_INPUT, then use your find-replace to insert #define TAKE_INPUT whatever_here at the top.

    – Draconis
    Mar 21 at 22:24






  • 11





    We need more answers starting with "I'm the founder of x and also the guy who created this".

    – pipe
    Mar 22 at 8:36






  • 1





    @iheanyi No one asked for it to be perfect. In fact, I'm convinced that almost any placeholder would have been better than something that looks like valid code to any newbie but doesn't actually compile.

    – Ruther Rendommeleigh
    Mar 25 at 8:24















150














I'm the founder of Coderbyte and also the guy who created this gets(stdin) hack.



The comments on this post are correct that it is a form of find-and-replace, so let me explain why I did this really quickly.



Back in the day when I first created the site (around 2012), it only supported JavaScript. There was no way to "read in input" in JavaScript running in the browser, and so there would be a function foo(input) and I used the readline() function from Node.js to call it like foo(readline()). Except I was a kid and didn't know better, so I literally just replaced readline() with the input at run-time. So foo(readline()) became foo(2) or foo("hello") which worked fine for JavaScript.



Around 2013/2014 I added more languages and used third-party service to evaluate code online, but it was very difficult to do stdin/stdout with the services I was using, so I stuck with the same silly find-and-replace for languages like Python, Ruby, and eventually C++, C#, etc.



Fast forward to today, I run the code in my own containers, but never updated the way stdin/stdout works because people have gotten used to the weird hack (some people have even posted in forums explaining how to get around it).



I know it is not best practice and it isn't helpful for someone learning a new language to see hacks like this, but the idea was for new programmers to not worry about reading input at all and just focus on writing the algorithm to solve the problem. One common complaint about coding challenge sites years ago was that new programmers would spend a lot of time just figuring out how to read from stdin or read lines from a file, so I wanted new coders to avoid this problem on Coderbyte.



I'll be updating the entire editor page soon along with the default code and stdin reading for languages. Hopefully then C++ programmers will enjoy using Coderbyte more :)






share|improve this answer




















  • 15





    "[B]ut the idea was for new programmers to not worry about reading input at all and just focus on writing the algorithm to solve the problem" - and it didn't occur to you to, instead of writing something that resembles "real" code, just put a made up function name or an obvious placeholder in that spot? Genuinely curious.

    – Ruther Rendommeleigh
    Mar 21 at 8:48






  • 23





    I genuinely didn't expect I was going to choose an answer other than my own when I posted this. Thank you for proving me wrong in such a great way. It's really a pleasure to see your answer.

    – bolov
    Mar 21 at 10:24






  • 4





    Very interesting! I would recommend, if you want to keep this hack, that you replace the function call with something like TAKE_INPUT, then use your find-replace to insert #define TAKE_INPUT whatever_here at the top.

    – Draconis
    Mar 21 at 22:24






  • 11





    We need more answers starting with "I'm the founder of x and also the guy who created this".

    – pipe
    Mar 22 at 8:36






  • 1





    @iheanyi No one asked for it to be perfect. In fact, I'm convinced that almost any placeholder would have been better than something that looks like valid code to any newbie but doesn't actually compile.

    – Ruther Rendommeleigh
    Mar 25 at 8:24













150












150








150







I'm the founder of Coderbyte and also the guy who created this gets(stdin) hack.



The comments on this post are correct that it is a form of find-and-replace, so let me explain why I did this really quickly.



Back in the day when I first created the site (around 2012), it only supported JavaScript. There was no way to "read in input" in JavaScript running in the browser, and so there would be a function foo(input) and I used the readline() function from Node.js to call it like foo(readline()). Except I was a kid and didn't know better, so I literally just replaced readline() with the input at run-time. So foo(readline()) became foo(2) or foo("hello") which worked fine for JavaScript.



Around 2013/2014 I added more languages and used third-party service to evaluate code online, but it was very difficult to do stdin/stdout with the services I was using, so I stuck with the same silly find-and-replace for languages like Python, Ruby, and eventually C++, C#, etc.



Fast forward to today, I run the code in my own containers, but never updated the way stdin/stdout works because people have gotten used to the weird hack (some people have even posted in forums explaining how to get around it).



I know it is not best practice and it isn't helpful for someone learning a new language to see hacks like this, but the idea was for new programmers to not worry about reading input at all and just focus on writing the algorithm to solve the problem. One common complaint about coding challenge sites years ago was that new programmers would spend a lot of time just figuring out how to read from stdin or read lines from a file, so I wanted new coders to avoid this problem on Coderbyte.



I'll be updating the entire editor page soon along with the default code and stdin reading for languages. Hopefully then C++ programmers will enjoy using Coderbyte more :)






share|improve this answer















I'm the founder of Coderbyte and also the guy who created this gets(stdin) hack.



The comments on this post are correct that it is a form of find-and-replace, so let me explain why I did this really quickly.



Back in the day when I first created the site (around 2012), it only supported JavaScript. There was no way to "read in input" in JavaScript running in the browser, and so there would be a function foo(input) and I used the readline() function from Node.js to call it like foo(readline()). Except I was a kid and didn't know better, so I literally just replaced readline() with the input at run-time. So foo(readline()) became foo(2) or foo("hello") which worked fine for JavaScript.



Around 2013/2014 I added more languages and used third-party service to evaluate code online, but it was very difficult to do stdin/stdout with the services I was using, so I stuck with the same silly find-and-replace for languages like Python, Ruby, and eventually C++, C#, etc.



Fast forward to today, I run the code in my own containers, but never updated the way stdin/stdout works because people have gotten used to the weird hack (some people have even posted in forums explaining how to get around it).



I know it is not best practice and it isn't helpful for someone learning a new language to see hacks like this, but the idea was for new programmers to not worry about reading input at all and just focus on writing the algorithm to solve the problem. One common complaint about coding challenge sites years ago was that new programmers would spend a lot of time just figuring out how to read from stdin or read lines from a file, so I wanted new coders to avoid this problem on Coderbyte.



I'll be updating the entire editor page soon along with the default code and stdin reading for languages. Hopefully then C++ programmers will enjoy using Coderbyte more :)







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 27 at 21:57









yash

71111123




71111123










answered Mar 21 at 3:56









Daniel BorowskiDaniel Borowski

704123




704123







  • 15





    "[B]ut the idea was for new programmers to not worry about reading input at all and just focus on writing the algorithm to solve the problem" - and it didn't occur to you to, instead of writing something that resembles "real" code, just put a made up function name or an obvious placeholder in that spot? Genuinely curious.

    – Ruther Rendommeleigh
    Mar 21 at 8:48






  • 23





    I genuinely didn't expect I was going to choose an answer other than my own when I posted this. Thank you for proving me wrong in such a great way. It's really a pleasure to see your answer.

    – bolov
    Mar 21 at 10:24






  • 4





    Very interesting! I would recommend, if you want to keep this hack, that you replace the function call with something like TAKE_INPUT, then use your find-replace to insert #define TAKE_INPUT whatever_here at the top.

    – Draconis
    Mar 21 at 22:24






  • 11





    We need more answers starting with "I'm the founder of x and also the guy who created this".

    – pipe
    Mar 22 at 8:36






  • 1





    @iheanyi No one asked for it to be perfect. In fact, I'm convinced that almost any placeholder would have been better than something that looks like valid code to any newbie but doesn't actually compile.

    – Ruther Rendommeleigh
    Mar 25 at 8:24












  • 15





    "[B]ut the idea was for new programmers to not worry about reading input at all and just focus on writing the algorithm to solve the problem" - and it didn't occur to you to, instead of writing something that resembles "real" code, just put a made up function name or an obvious placeholder in that spot? Genuinely curious.

    – Ruther Rendommeleigh
    Mar 21 at 8:48






  • 23





    I genuinely didn't expect I was going to choose an answer other than my own when I posted this. Thank you for proving me wrong in such a great way. It's really a pleasure to see your answer.

    – bolov
    Mar 21 at 10:24






  • 4





    Very interesting! I would recommend, if you want to keep this hack, that you replace the function call with something like TAKE_INPUT, then use your find-replace to insert #define TAKE_INPUT whatever_here at the top.

    – Draconis
    Mar 21 at 22:24






  • 11





    We need more answers starting with "I'm the founder of x and also the guy who created this".

    – pipe
    Mar 22 at 8:36






  • 1





    @iheanyi No one asked for it to be perfect. In fact, I'm convinced that almost any placeholder would have been better than something that looks like valid code to any newbie but doesn't actually compile.

    – Ruther Rendommeleigh
    Mar 25 at 8:24







15




15





"[B]ut the idea was for new programmers to not worry about reading input at all and just focus on writing the algorithm to solve the problem" - and it didn't occur to you to, instead of writing something that resembles "real" code, just put a made up function name or an obvious placeholder in that spot? Genuinely curious.

– Ruther Rendommeleigh
Mar 21 at 8:48





"[B]ut the idea was for new programmers to not worry about reading input at all and just focus on writing the algorithm to solve the problem" - and it didn't occur to you to, instead of writing something that resembles "real" code, just put a made up function name or an obvious placeholder in that spot? Genuinely curious.

– Ruther Rendommeleigh
Mar 21 at 8:48




23




23





I genuinely didn't expect I was going to choose an answer other than my own when I posted this. Thank you for proving me wrong in such a great way. It's really a pleasure to see your answer.

– bolov
Mar 21 at 10:24





I genuinely didn't expect I was going to choose an answer other than my own when I posted this. Thank you for proving me wrong in such a great way. It's really a pleasure to see your answer.

– bolov
Mar 21 at 10:24




4




4





Very interesting! I would recommend, if you want to keep this hack, that you replace the function call with something like TAKE_INPUT, then use your find-replace to insert #define TAKE_INPUT whatever_here at the top.

– Draconis
Mar 21 at 22:24





Very interesting! I would recommend, if you want to keep this hack, that you replace the function call with something like TAKE_INPUT, then use your find-replace to insert #define TAKE_INPUT whatever_here at the top.

– Draconis
Mar 21 at 22:24




11




11





We need more answers starting with "I'm the founder of x and also the guy who created this".

– pipe
Mar 22 at 8:36





We need more answers starting with "I'm the founder of x and also the guy who created this".

– pipe
Mar 22 at 8:36




1




1





@iheanyi No one asked for it to be perfect. In fact, I'm convinced that almost any placeholder would have been better than something that looks like valid code to any newbie but doesn't actually compile.

– Ruther Rendommeleigh
Mar 25 at 8:24





@iheanyi No one asked for it to be perfect. In fact, I'm convinced that almost any placeholder would have been better than something that looks like valid code to any newbie but doesn't actually compile.

– Ruther Rendommeleigh
Mar 25 at 8:24













105














I am intrigued. So, time to put the investigation goggles on and since I don't have access to the compiler or compilation flags I need to get inventive. Also because nothing about this code makes sense it's not a bad idea question every assumption.



First let's check the actual type of gets. I have a little trick for that:



template <class> struct Name;

int main()

Name<decltype(gets)> n;

// keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;




And that looks ... normal:




/tmp/613814454/Main.cpp:16:19: warning: 'gets' is deprecated [-Wdeprecated-declarations]
Name<decltype(gets)> n;
^
/usr/include/stdio.h:638:37: note: 'gets' has been explicitly marked deprecated here
extern char *gets (char *__s) __wur __attribute_deprecated__;
^
/usr/include/x86_64-linux-gnu/sys/cdefs.h:254:51: note: expanded from macro '__attribute_deprecated__'
# define __attribute_deprecated__ __attribute__ ((__deprecated__))
^
/tmp/613814454/Main.cpp:16:26: error: implicit instantiation of undefined template 'Name<char *(char *)>'
Name<decltype(gets)> n;
^
/tmp/613814454/Main.cpp:12:25: note: template is declared here
template <class> struct Name;
^
1 warning and 1 error generated.



gets is marked as deprecated and has the signature char *(char *). But then how is FirstFactorial(gets(stdin)); compiling?



Let's try something else:



int main() 
Name<decltype(gets(stdin))> n;

// keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;




Which gives us:




/tmp/286775780/Main.cpp:15:21: error: implicit instantiation of undefined template 'Name<int>'
Name<decltype(8)> n;
^



Finally we are getting something: decltype(8). So the entire gets(stdin) was textually replaced with the input (8).



And the things get weirder. The compiler error continues:




/tmp/596773533/Main.cpp:18:26: error: no matching function for call to 'gets'
cout << FirstFactorial(gets(stdin));
^~~~
/usr/include/stdio.h:638:14: note: candidate function not viable: no known conversion from 'struct _IO_FILE *' to 'char *' for 1st argument
extern char *gets (char *__s) __wur __attribute_deprecated__;



So now we get the expected error for cout << FirstFactorial(gets(stdin));



I checked for a macro and since #undef gets seems to do nothing it looks like it isn't a macro.



But



std::integral_constant<int, gets(stdin)> n;


It compiles.



But



std::integral_constant<int, gets(stdin)> n; // OK
std::integral_constant<int, gets(stdin)> n2; // ERROR wtf??


Doesn't with the expected error at the n2 line.



And again, almost any modification to main makes the line cout << FirstFactorial(gets(stdin)); spit out the expected error.



Moreover the stdin actually seems to be empty.



So I can only conclude and speculate they have a little program that parses the source and tries (poorly) to replace gets(stdin) with the test case input value before actually feeding it into the compiler. If anybody has a better theory or actually knows what they are doing please share!



This is obviously a very bad practice. While researching this I found there is at least a question here (example) about this and because people have no idea that there is a site out there who does this their answer is "don't use gets use ... instead" which is indeed a good advice but only confuses the OP more since any attempt at a valid read from stdin will fail on this site.




TLDR



gets(stdin) is invalid C++. It's a gimmick this particular site uses (for what reasons I cannot figure out). If you want to continue to submit on the site (I am neither endorsing it neither not endorsing it) you have to use this construct that otherwise would not make sense, but be aware that it is brittle. Almost any modifications to main will spit out an error. Outside of this site use normal input reading methods.






share|improve this answer




















  • 26





    I'm genuinely amazed. Maybe this Q/A can be a canonical post on why not to learn from coding challenge sites.

    – alter igel
    Mar 20 at 20:07






  • 1





    The "8" comes from the left box at the bottom of the screen. Try typing a text string in there (I tried "maplemaple") and see the result...

    – Stobor
    Mar 20 at 20:09






  • 27





    Something really evil is happening, and I think it's at the level of text replacement in the source code outside of the compiler. Try this: std::cout << "gets(stdin)"; and the output is 8 (or whatever you type into the 'input' field. This is a disgraceful abuse of the language.

    – alter igel
    Mar 20 at 20:13







  • 13





    @Stobor note the quotes around "gets(stdin)". That's a string literal that even the preprocessor wouldn't touch

    – alter igel
    Mar 20 at 20:15






  • 2





    To quote James Kirk: "This is damn peculiar."

    – ApproachingDarknessFish
    Mar 21 at 19:49















105














I am intrigued. So, time to put the investigation goggles on and since I don't have access to the compiler or compilation flags I need to get inventive. Also because nothing about this code makes sense it's not a bad idea question every assumption.



First let's check the actual type of gets. I have a little trick for that:



template <class> struct Name;

int main()

Name<decltype(gets)> n;

// keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;




And that looks ... normal:




/tmp/613814454/Main.cpp:16:19: warning: 'gets' is deprecated [-Wdeprecated-declarations]
Name<decltype(gets)> n;
^
/usr/include/stdio.h:638:37: note: 'gets' has been explicitly marked deprecated here
extern char *gets (char *__s) __wur __attribute_deprecated__;
^
/usr/include/x86_64-linux-gnu/sys/cdefs.h:254:51: note: expanded from macro '__attribute_deprecated__'
# define __attribute_deprecated__ __attribute__ ((__deprecated__))
^
/tmp/613814454/Main.cpp:16:26: error: implicit instantiation of undefined template 'Name<char *(char *)>'
Name<decltype(gets)> n;
^
/tmp/613814454/Main.cpp:12:25: note: template is declared here
template <class> struct Name;
^
1 warning and 1 error generated.



gets is marked as deprecated and has the signature char *(char *). But then how is FirstFactorial(gets(stdin)); compiling?



Let's try something else:



int main() 
Name<decltype(gets(stdin))> n;

// keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;




Which gives us:




/tmp/286775780/Main.cpp:15:21: error: implicit instantiation of undefined template 'Name<int>'
Name<decltype(8)> n;
^



Finally we are getting something: decltype(8). So the entire gets(stdin) was textually replaced with the input (8).



And the things get weirder. The compiler error continues:




/tmp/596773533/Main.cpp:18:26: error: no matching function for call to 'gets'
cout << FirstFactorial(gets(stdin));
^~~~
/usr/include/stdio.h:638:14: note: candidate function not viable: no known conversion from 'struct _IO_FILE *' to 'char *' for 1st argument
extern char *gets (char *__s) __wur __attribute_deprecated__;



So now we get the expected error for cout << FirstFactorial(gets(stdin));



I checked for a macro and since #undef gets seems to do nothing it looks like it isn't a macro.



But



std::integral_constant<int, gets(stdin)> n;


It compiles.



But



std::integral_constant<int, gets(stdin)> n; // OK
std::integral_constant<int, gets(stdin)> n2; // ERROR wtf??


Doesn't with the expected error at the n2 line.



And again, almost any modification to main makes the line cout << FirstFactorial(gets(stdin)); spit out the expected error.



Moreover the stdin actually seems to be empty.



So I can only conclude and speculate they have a little program that parses the source and tries (poorly) to replace gets(stdin) with the test case input value before actually feeding it into the compiler. If anybody has a better theory or actually knows what they are doing please share!



This is obviously a very bad practice. While researching this I found there is at least a question here (example) about this and because people have no idea that there is a site out there who does this their answer is "don't use gets use ... instead" which is indeed a good advice but only confuses the OP more since any attempt at a valid read from stdin will fail on this site.




TLDR



gets(stdin) is invalid C++. It's a gimmick this particular site uses (for what reasons I cannot figure out). If you want to continue to submit on the site (I am neither endorsing it neither not endorsing it) you have to use this construct that otherwise would not make sense, but be aware that it is brittle. Almost any modifications to main will spit out an error. Outside of this site use normal input reading methods.






share|improve this answer




















  • 26





    I'm genuinely amazed. Maybe this Q/A can be a canonical post on why not to learn from coding challenge sites.

    – alter igel
    Mar 20 at 20:07






  • 1





    The "8" comes from the left box at the bottom of the screen. Try typing a text string in there (I tried "maplemaple") and see the result...

    – Stobor
    Mar 20 at 20:09






  • 27





    Something really evil is happening, and I think it's at the level of text replacement in the source code outside of the compiler. Try this: std::cout << "gets(stdin)"; and the output is 8 (or whatever you type into the 'input' field. This is a disgraceful abuse of the language.

    – alter igel
    Mar 20 at 20:13







  • 13





    @Stobor note the quotes around "gets(stdin)". That's a string literal that even the preprocessor wouldn't touch

    – alter igel
    Mar 20 at 20:15






  • 2





    To quote James Kirk: "This is damn peculiar."

    – ApproachingDarknessFish
    Mar 21 at 19:49













105












105








105







I am intrigued. So, time to put the investigation goggles on and since I don't have access to the compiler or compilation flags I need to get inventive. Also because nothing about this code makes sense it's not a bad idea question every assumption.



First let's check the actual type of gets. I have a little trick for that:



template <class> struct Name;

int main()

Name<decltype(gets)> n;

// keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;




And that looks ... normal:




/tmp/613814454/Main.cpp:16:19: warning: 'gets' is deprecated [-Wdeprecated-declarations]
Name<decltype(gets)> n;
^
/usr/include/stdio.h:638:37: note: 'gets' has been explicitly marked deprecated here
extern char *gets (char *__s) __wur __attribute_deprecated__;
^
/usr/include/x86_64-linux-gnu/sys/cdefs.h:254:51: note: expanded from macro '__attribute_deprecated__'
# define __attribute_deprecated__ __attribute__ ((__deprecated__))
^
/tmp/613814454/Main.cpp:16:26: error: implicit instantiation of undefined template 'Name<char *(char *)>'
Name<decltype(gets)> n;
^
/tmp/613814454/Main.cpp:12:25: note: template is declared here
template <class> struct Name;
^
1 warning and 1 error generated.



gets is marked as deprecated and has the signature char *(char *). But then how is FirstFactorial(gets(stdin)); compiling?



Let's try something else:



int main() 
Name<decltype(gets(stdin))> n;

// keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;




Which gives us:




/tmp/286775780/Main.cpp:15:21: error: implicit instantiation of undefined template 'Name<int>'
Name<decltype(8)> n;
^



Finally we are getting something: decltype(8). So the entire gets(stdin) was textually replaced with the input (8).



And the things get weirder. The compiler error continues:




/tmp/596773533/Main.cpp:18:26: error: no matching function for call to 'gets'
cout << FirstFactorial(gets(stdin));
^~~~
/usr/include/stdio.h:638:14: note: candidate function not viable: no known conversion from 'struct _IO_FILE *' to 'char *' for 1st argument
extern char *gets (char *__s) __wur __attribute_deprecated__;



So now we get the expected error for cout << FirstFactorial(gets(stdin));



I checked for a macro and since #undef gets seems to do nothing it looks like it isn't a macro.



But



std::integral_constant<int, gets(stdin)> n;


It compiles.



But



std::integral_constant<int, gets(stdin)> n; // OK
std::integral_constant<int, gets(stdin)> n2; // ERROR wtf??


Doesn't with the expected error at the n2 line.



And again, almost any modification to main makes the line cout << FirstFactorial(gets(stdin)); spit out the expected error.



Moreover the stdin actually seems to be empty.



So I can only conclude and speculate they have a little program that parses the source and tries (poorly) to replace gets(stdin) with the test case input value before actually feeding it into the compiler. If anybody has a better theory or actually knows what they are doing please share!



This is obviously a very bad practice. While researching this I found there is at least a question here (example) about this and because people have no idea that there is a site out there who does this their answer is "don't use gets use ... instead" which is indeed a good advice but only confuses the OP more since any attempt at a valid read from stdin will fail on this site.




TLDR



gets(stdin) is invalid C++. It's a gimmick this particular site uses (for what reasons I cannot figure out). If you want to continue to submit on the site (I am neither endorsing it neither not endorsing it) you have to use this construct that otherwise would not make sense, but be aware that it is brittle. Almost any modifications to main will spit out an error. Outside of this site use normal input reading methods.






share|improve this answer















I am intrigued. So, time to put the investigation goggles on and since I don't have access to the compiler or compilation flags I need to get inventive. Also because nothing about this code makes sense it's not a bad idea question every assumption.



First let's check the actual type of gets. I have a little trick for that:



template <class> struct Name;

int main()

Name<decltype(gets)> n;

// keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;




And that looks ... normal:




/tmp/613814454/Main.cpp:16:19: warning: 'gets' is deprecated [-Wdeprecated-declarations]
Name<decltype(gets)> n;
^
/usr/include/stdio.h:638:37: note: 'gets' has been explicitly marked deprecated here
extern char *gets (char *__s) __wur __attribute_deprecated__;
^
/usr/include/x86_64-linux-gnu/sys/cdefs.h:254:51: note: expanded from macro '__attribute_deprecated__'
# define __attribute_deprecated__ __attribute__ ((__deprecated__))
^
/tmp/613814454/Main.cpp:16:26: error: implicit instantiation of undefined template 'Name<char *(char *)>'
Name<decltype(gets)> n;
^
/tmp/613814454/Main.cpp:12:25: note: template is declared here
template <class> struct Name;
^
1 warning and 1 error generated.



gets is marked as deprecated and has the signature char *(char *). But then how is FirstFactorial(gets(stdin)); compiling?



Let's try something else:



int main() 
Name<decltype(gets(stdin))> n;

// keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;




Which gives us:




/tmp/286775780/Main.cpp:15:21: error: implicit instantiation of undefined template 'Name<int>'
Name<decltype(8)> n;
^



Finally we are getting something: decltype(8). So the entire gets(stdin) was textually replaced with the input (8).



And the things get weirder. The compiler error continues:




/tmp/596773533/Main.cpp:18:26: error: no matching function for call to 'gets'
cout << FirstFactorial(gets(stdin));
^~~~
/usr/include/stdio.h:638:14: note: candidate function not viable: no known conversion from 'struct _IO_FILE *' to 'char *' for 1st argument
extern char *gets (char *__s) __wur __attribute_deprecated__;



So now we get the expected error for cout << FirstFactorial(gets(stdin));



I checked for a macro and since #undef gets seems to do nothing it looks like it isn't a macro.



But



std::integral_constant<int, gets(stdin)> n;


It compiles.



But



std::integral_constant<int, gets(stdin)> n; // OK
std::integral_constant<int, gets(stdin)> n2; // ERROR wtf??


Doesn't with the expected error at the n2 line.



And again, almost any modification to main makes the line cout << FirstFactorial(gets(stdin)); spit out the expected error.



Moreover the stdin actually seems to be empty.



So I can only conclude and speculate they have a little program that parses the source and tries (poorly) to replace gets(stdin) with the test case input value before actually feeding it into the compiler. If anybody has a better theory or actually knows what they are doing please share!



This is obviously a very bad practice. While researching this I found there is at least a question here (example) about this and because people have no idea that there is a site out there who does this their answer is "don't use gets use ... instead" which is indeed a good advice but only confuses the OP more since any attempt at a valid read from stdin will fail on this site.




TLDR



gets(stdin) is invalid C++. It's a gimmick this particular site uses (for what reasons I cannot figure out). If you want to continue to submit on the site (I am neither endorsing it neither not endorsing it) you have to use this construct that otherwise would not make sense, but be aware that it is brittle. Almost any modifications to main will spit out an error. Outside of this site use normal input reading methods.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 20 at 21:40









scohe001

8,16212442




8,16212442










answered Mar 20 at 20:00









bolovbolov

33.4k877141




33.4k877141







  • 26





    I'm genuinely amazed. Maybe this Q/A can be a canonical post on why not to learn from coding challenge sites.

    – alter igel
    Mar 20 at 20:07






  • 1





    The "8" comes from the left box at the bottom of the screen. Try typing a text string in there (I tried "maplemaple") and see the result...

    – Stobor
    Mar 20 at 20:09






  • 27





    Something really evil is happening, and I think it's at the level of text replacement in the source code outside of the compiler. Try this: std::cout << "gets(stdin)"; and the output is 8 (or whatever you type into the 'input' field. This is a disgraceful abuse of the language.

    – alter igel
    Mar 20 at 20:13







  • 13





    @Stobor note the quotes around "gets(stdin)". That's a string literal that even the preprocessor wouldn't touch

    – alter igel
    Mar 20 at 20:15






  • 2





    To quote James Kirk: "This is damn peculiar."

    – ApproachingDarknessFish
    Mar 21 at 19:49












  • 26





    I'm genuinely amazed. Maybe this Q/A can be a canonical post on why not to learn from coding challenge sites.

    – alter igel
    Mar 20 at 20:07






  • 1





    The "8" comes from the left box at the bottom of the screen. Try typing a text string in there (I tried "maplemaple") and see the result...

    – Stobor
    Mar 20 at 20:09






  • 27





    Something really evil is happening, and I think it's at the level of text replacement in the source code outside of the compiler. Try this: std::cout << "gets(stdin)"; and the output is 8 (or whatever you type into the 'input' field. This is a disgraceful abuse of the language.

    – alter igel
    Mar 20 at 20:13







  • 13





    @Stobor note the quotes around "gets(stdin)". That's a string literal that even the preprocessor wouldn't touch

    – alter igel
    Mar 20 at 20:15






  • 2





    To quote James Kirk: "This is damn peculiar."

    – ApproachingDarknessFish
    Mar 21 at 19:49







26




26





I'm genuinely amazed. Maybe this Q/A can be a canonical post on why not to learn from coding challenge sites.

– alter igel
Mar 20 at 20:07





I'm genuinely amazed. Maybe this Q/A can be a canonical post on why not to learn from coding challenge sites.

– alter igel
Mar 20 at 20:07




1




1





The "8" comes from the left box at the bottom of the screen. Try typing a text string in there (I tried "maplemaple") and see the result...

– Stobor
Mar 20 at 20:09





The "8" comes from the left box at the bottom of the screen. Try typing a text string in there (I tried "maplemaple") and see the result...

– Stobor
Mar 20 at 20:09




27




27





Something really evil is happening, and I think it's at the level of text replacement in the source code outside of the compiler. Try this: std::cout << "gets(stdin)"; and the output is 8 (or whatever you type into the 'input' field. This is a disgraceful abuse of the language.

– alter igel
Mar 20 at 20:13






Something really evil is happening, and I think it's at the level of text replacement in the source code outside of the compiler. Try this: std::cout << "gets(stdin)"; and the output is 8 (or whatever you type into the 'input' field. This is a disgraceful abuse of the language.

– alter igel
Mar 20 at 20:13





13




13





@Stobor note the quotes around "gets(stdin)". That's a string literal that even the preprocessor wouldn't touch

– alter igel
Mar 20 at 20:15





@Stobor note the quotes around "gets(stdin)". That's a string literal that even the preprocessor wouldn't touch

– alter igel
Mar 20 at 20:15




2




2





To quote James Kirk: "This is damn peculiar."

– ApproachingDarknessFish
Mar 21 at 19:49





To quote James Kirk: "This is damn peculiar."

– ApproachingDarknessFish
Mar 21 at 19:49











60














I tried the following addition to main in the Coderbyte editor:



std::cout << "gets(stdin)";


Where the mysterious and enigmatic snippet gets(stdin) appears inside a string literal. This shouldn't possibly be transformed by anything, not even the preprocessor, and any C++ programmer should expect this code to print the exact string gets(stdin) to the standard output. And yet we see the following output, when compiled and run on coderbyte:



8


Where the value 8 is taken straight from the convenient 'input' field under the editor.



Magic code



From this, it's clear that this online editor is performing blind find-and-replace operations on the source code, substitution appearances of gets(stdin) with the user's 'input'. I would personally call this a misuse of the language that's worse than careless preprocessor macros.



In the context of an online coding challenge website, I'm worried by this because it teaches unconventional, non-standard, meaningless, and at least unsafe practices like gets(stdin), and in a manner that can't be repeated on other platforms.



I'm sure it can't be this hard to just use std::cin and just stream input to a program.






share|improve this answer

























  • and it's not even a blind "find and replace" because sometimes it replaces it sometimes it does not.

    – bolov
    Mar 20 at 20:31






  • 4





    @bolov could it be just the first occurrence of gets(stdin) that is replaced? I meant 'blind' in the sense that it appears to be unaware of the language's syntax or grammar.

    – alter igel
    Mar 20 at 20:39











  • yes, you are right. It replaces the first occurence. I tried putting one before main and that's what I got indeed.

    – bolov
    Mar 20 at 20:54






  • 1





    Further research suggests that that site does it for all languages, not just C++ - python/ruby it uses the function call ("raw_input()" or "STDIN.gets") which would typically return a string from stdin, but ends up doing a string substitution of that string instead. I guess finding a regex match for the getline function was too hard, so they went with gets(stdin) for C/C++.

    – Stobor
    Mar 20 at 23:39






  • 3





    @Stobor dang, you're right. I can confirm this happens for Java too, the line System.out.print(FirstFactorial(s.nextLine()9)); prints 89 even when s is undefined.

    – alter igel
    Mar 20 at 23:41
















60














I tried the following addition to main in the Coderbyte editor:



std::cout << "gets(stdin)";


Where the mysterious and enigmatic snippet gets(stdin) appears inside a string literal. This shouldn't possibly be transformed by anything, not even the preprocessor, and any C++ programmer should expect this code to print the exact string gets(stdin) to the standard output. And yet we see the following output, when compiled and run on coderbyte:



8


Where the value 8 is taken straight from the convenient 'input' field under the editor.



Magic code



From this, it's clear that this online editor is performing blind find-and-replace operations on the source code, substitution appearances of gets(stdin) with the user's 'input'. I would personally call this a misuse of the language that's worse than careless preprocessor macros.



In the context of an online coding challenge website, I'm worried by this because it teaches unconventional, non-standard, meaningless, and at least unsafe practices like gets(stdin), and in a manner that can't be repeated on other platforms.



I'm sure it can't be this hard to just use std::cin and just stream input to a program.






share|improve this answer

























  • and it's not even a blind "find and replace" because sometimes it replaces it sometimes it does not.

    – bolov
    Mar 20 at 20:31






  • 4





    @bolov could it be just the first occurrence of gets(stdin) that is replaced? I meant 'blind' in the sense that it appears to be unaware of the language's syntax or grammar.

    – alter igel
    Mar 20 at 20:39











  • yes, you are right. It replaces the first occurence. I tried putting one before main and that's what I got indeed.

    – bolov
    Mar 20 at 20:54






  • 1





    Further research suggests that that site does it for all languages, not just C++ - python/ruby it uses the function call ("raw_input()" or "STDIN.gets") which would typically return a string from stdin, but ends up doing a string substitution of that string instead. I guess finding a regex match for the getline function was too hard, so they went with gets(stdin) for C/C++.

    – Stobor
    Mar 20 at 23:39






  • 3





    @Stobor dang, you're right. I can confirm this happens for Java too, the line System.out.print(FirstFactorial(s.nextLine()9)); prints 89 even when s is undefined.

    – alter igel
    Mar 20 at 23:41














60












60








60







I tried the following addition to main in the Coderbyte editor:



std::cout << "gets(stdin)";


Where the mysterious and enigmatic snippet gets(stdin) appears inside a string literal. This shouldn't possibly be transformed by anything, not even the preprocessor, and any C++ programmer should expect this code to print the exact string gets(stdin) to the standard output. And yet we see the following output, when compiled and run on coderbyte:



8


Where the value 8 is taken straight from the convenient 'input' field under the editor.



Magic code



From this, it's clear that this online editor is performing blind find-and-replace operations on the source code, substitution appearances of gets(stdin) with the user's 'input'. I would personally call this a misuse of the language that's worse than careless preprocessor macros.



In the context of an online coding challenge website, I'm worried by this because it teaches unconventional, non-standard, meaningless, and at least unsafe practices like gets(stdin), and in a manner that can't be repeated on other platforms.



I'm sure it can't be this hard to just use std::cin and just stream input to a program.






share|improve this answer















I tried the following addition to main in the Coderbyte editor:



std::cout << "gets(stdin)";


Where the mysterious and enigmatic snippet gets(stdin) appears inside a string literal. This shouldn't possibly be transformed by anything, not even the preprocessor, and any C++ programmer should expect this code to print the exact string gets(stdin) to the standard output. And yet we see the following output, when compiled and run on coderbyte:



8


Where the value 8 is taken straight from the convenient 'input' field under the editor.



Magic code



From this, it's clear that this online editor is performing blind find-and-replace operations on the source code, substitution appearances of gets(stdin) with the user's 'input'. I would personally call this a misuse of the language that's worse than careless preprocessor macros.



In the context of an online coding challenge website, I'm worried by this because it teaches unconventional, non-standard, meaningless, and at least unsafe practices like gets(stdin), and in a manner that can't be repeated on other platforms.



I'm sure it can't be this hard to just use std::cin and just stream input to a program.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 22 at 13:54









Peter Mortensen

13.8k1987113




13.8k1987113










answered Mar 20 at 20:30









alter igelalter igel

3,50111230




3,50111230












  • and it's not even a blind "find and replace" because sometimes it replaces it sometimes it does not.

    – bolov
    Mar 20 at 20:31






  • 4





    @bolov could it be just the first occurrence of gets(stdin) that is replaced? I meant 'blind' in the sense that it appears to be unaware of the language's syntax or grammar.

    – alter igel
    Mar 20 at 20:39











  • yes, you are right. It replaces the first occurence. I tried putting one before main and that's what I got indeed.

    – bolov
    Mar 20 at 20:54






  • 1





    Further research suggests that that site does it for all languages, not just C++ - python/ruby it uses the function call ("raw_input()" or "STDIN.gets") which would typically return a string from stdin, but ends up doing a string substitution of that string instead. I guess finding a regex match for the getline function was too hard, so they went with gets(stdin) for C/C++.

    – Stobor
    Mar 20 at 23:39






  • 3





    @Stobor dang, you're right. I can confirm this happens for Java too, the line System.out.print(FirstFactorial(s.nextLine()9)); prints 89 even when s is undefined.

    – alter igel
    Mar 20 at 23:41


















  • and it's not even a blind "find and replace" because sometimes it replaces it sometimes it does not.

    – bolov
    Mar 20 at 20:31






  • 4





    @bolov could it be just the first occurrence of gets(stdin) that is replaced? I meant 'blind' in the sense that it appears to be unaware of the language's syntax or grammar.

    – alter igel
    Mar 20 at 20:39











  • yes, you are right. It replaces the first occurence. I tried putting one before main and that's what I got indeed.

    – bolov
    Mar 20 at 20:54






  • 1





    Further research suggests that that site does it for all languages, not just C++ - python/ruby it uses the function call ("raw_input()" or "STDIN.gets") which would typically return a string from stdin, but ends up doing a string substitution of that string instead. I guess finding a regex match for the getline function was too hard, so they went with gets(stdin) for C/C++.

    – Stobor
    Mar 20 at 23:39






  • 3





    @Stobor dang, you're right. I can confirm this happens for Java too, the line System.out.print(FirstFactorial(s.nextLine()9)); prints 89 even when s is undefined.

    – alter igel
    Mar 20 at 23:41

















and it's not even a blind "find and replace" because sometimes it replaces it sometimes it does not.

– bolov
Mar 20 at 20:31





and it's not even a blind "find and replace" because sometimes it replaces it sometimes it does not.

– bolov
Mar 20 at 20:31




4




4





@bolov could it be just the first occurrence of gets(stdin) that is replaced? I meant 'blind' in the sense that it appears to be unaware of the language's syntax or grammar.

– alter igel
Mar 20 at 20:39





@bolov could it be just the first occurrence of gets(stdin) that is replaced? I meant 'blind' in the sense that it appears to be unaware of the language's syntax or grammar.

– alter igel
Mar 20 at 20:39













yes, you are right. It replaces the first occurence. I tried putting one before main and that's what I got indeed.

– bolov
Mar 20 at 20:54





yes, you are right. It replaces the first occurence. I tried putting one before main and that's what I got indeed.

– bolov
Mar 20 at 20:54




1




1





Further research suggests that that site does it for all languages, not just C++ - python/ruby it uses the function call ("raw_input()" or "STDIN.gets") which would typically return a string from stdin, but ends up doing a string substitution of that string instead. I guess finding a regex match for the getline function was too hard, so they went with gets(stdin) for C/C++.

– Stobor
Mar 20 at 23:39





Further research suggests that that site does it for all languages, not just C++ - python/ruby it uses the function call ("raw_input()" or "STDIN.gets") which would typically return a string from stdin, but ends up doing a string substitution of that string instead. I guess finding a regex match for the getline function was too hard, so they went with gets(stdin) for C/C++.

– Stobor
Mar 20 at 23:39




3




3





@Stobor dang, you're right. I can confirm this happens for Java too, the line System.out.print(FirstFactorial(s.nextLine()9)); prints 89 even when s is undefined.

– alter igel
Mar 20 at 23:41






@Stobor dang, you're right. I can confirm this happens for Java too, the line System.out.print(FirstFactorial(s.nextLine()9)); prints 89 even when s is undefined.

– alter igel
Mar 20 at 23:41


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55269252%2fwhat-is-going-on-with-getsstdin-on-the-site-coderbyte%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Færeyskur hestur Heimild | Tengill | Tilvísanir | LeiðsagnarvalRossið - síða um færeyska hrossið á færeyskuGott ár hjá færeyska hestinum

He _____ here since 1970 . Answer needed [closed]What does “since he was so high” mean?Meaning of “catch birds for”?How do I ensure “since” takes the meaning I want?“Who cares here” meaningWhat does “right round toward” mean?the time tense (had now been detected)What does the phrase “ring around the roses” mean here?Correct usage of “visited upon”Meaning of “foiled rail sabotage bid”It was the third time I had gone to Rome or It is the third time I had been to Rome

Slayer Innehåll Historia | Stil, komposition och lyrik | Bandets betydelse och framgångar | Sidoprojekt och samarbeten | Kontroverser | Medlemmar | Utmärkelser och nomineringar | Turnéer och festivaler | Diskografi | Referenser | Externa länkar | Navigeringsmenywww.slayer.net”Metal Massacre vol. 1””Metal Massacre vol. 3””Metal Massacre Volume III””Show No Mercy””Haunting the Chapel””Live Undead””Hell Awaits””Reign in Blood””Reign in Blood””Gold & Platinum – Reign in Blood””Golden Gods Awards Winners”originalet”Kerrang! Hall Of Fame””Slayer Looks Back On 37-Year Career In New Video Series: Part Two””South of Heaven””Gold & Platinum – South of Heaven””Seasons in the Abyss””Gold & Platinum - Seasons in the Abyss””Divine Intervention””Divine Intervention - Release group by Slayer””Gold & Platinum - Divine Intervention””Live Intrusion””Undisputed Attitude””Abolish Government/Superficial Love””Release “Slatanic Slaughter: A Tribute to Slayer” by Various Artists””Diabolus in Musica””Soundtrack to the Apocalypse””God Hates Us All””Systematic - Relationships””War at the Warfield””Gold & Platinum - War at the Warfield””Soundtrack to the Apocalypse””Gold & Platinum - Still Reigning””Metallica, Slayer, Iron Mauden Among Winners At Metal Hammer Awards””Eternal Pyre””Eternal Pyre - Slayer release group””Eternal Pyre””Metal Storm Awards 2006””Kerrang! Hall Of Fame””Slayer Wins 'Best Metal' Grammy Award””Slayer Guitarist Jeff Hanneman Dies””Bullet-For My Valentine booed at Metal Hammer Golden Gods Awards””Unholy Aliance””The End Of Slayer?””Slayer: We Could Thrash Out Two More Albums If We're Fast Enough...””'The Unholy Alliance: Chapter III' UK Dates Added”originalet”Megadeth And Slayer To Co-Headline 'Canadian Carnage' Trek”originalet”World Painted Blood””Release “World Painted Blood” by Slayer””Metallica Heading To Cinemas””Slayer, Megadeth To Join Forces For 'European Carnage' Tour - Dec. 18, 2010”originalet”Slayer's Hanneman Contracts Acute Infection; Band To Bring In Guest Guitarist””Cannibal Corpse's Pat O'Brien Will Step In As Slayer's Guest Guitarist”originalet”Slayer’s Jeff Hanneman Dead at 49””Dave Lombardo Says He Made Only $67,000 In 2011 While Touring With Slayer””Slayer: We Do Not Agree With Dave Lombardo's Substance Or Timeline Of Events””Slayer Welcomes Drummer Paul Bostaph Back To The Fold””Slayer Hope to Unveil Never-Before-Heard Jeff Hanneman Material on Next Album””Slayer Debut New Song 'Implode' During Surprise Golden Gods Appearance””Release group Repentless by Slayer””Repentless - Slayer - Credits””Slayer””Metal Storm Awards 2015””Slayer - to release comic book "Repentless #1"””Slayer To Release 'Repentless' 6.66" Vinyl Box Set””BREAKING NEWS: Slayer Announce Farewell Tour””Slayer Recruit Lamb of God, Anthrax, Behemoth + Testament for Final Tour””Slayer lägger ner efter 37 år””Slayer Announces Second North American Leg Of 'Final' Tour””Final World Tour””Slayer Announces Final European Tour With Lamb of God, Anthrax And Obituary””Slayer To Tour Europe With Lamb of God, Anthrax And Obituary””Slayer To Play 'Last French Show Ever' At Next Year's Hellfst””Slayer's Final World Tour Will Extend Into 2019””Death Angel's Rob Cavestany On Slayer's 'Farewell' Tour: 'Some Of Us Could See This Coming'””Testament Has No Plans To Retire Anytime Soon, Says Chuck Billy””Anthrax's Scott Ian On Slayer's 'Farewell' Tour Plans: 'I Was Surprised And I Wasn't Surprised'””Slayer””Slayer's Morbid Schlock””Review/Rock; For Slayer, the Mania Is the Message””Slayer - Biography””Slayer - Reign In Blood”originalet”Dave Lombardo””An exclusive oral history of Slayer”originalet”Exclusive! Interview With Slayer Guitarist Jeff Hanneman”originalet”Thinking Out Loud: Slayer's Kerry King on hair metal, Satan and being polite””Slayer Lyrics””Slayer - Biography””Most influential artists for extreme metal music””Slayer - Reign in Blood””Slayer guitarist Jeff Hanneman dies aged 49””Slatanic Slaughter: A Tribute to Slayer””Gateway to Hell: A Tribute to Slayer””Covered In Blood””Slayer: The Origins of Thrash in San Francisco, CA.””Why They Rule - #6 Slayer”originalet”Guitar World's 100 Greatest Heavy Metal Guitarists Of All Time”originalet”The fans have spoken: Slayer comes out on top in readers' polls”originalet”Tribute to Jeff Hanneman (1964-2013)””Lamb Of God Frontman: We Sound Like A Slayer Rip-Off””BEHEMOTH Frontman Pays Tribute To SLAYER's JEFF HANNEMAN””Slayer, Hatebreed Doing Double Duty On This Year's Ozzfest””System of a Down””Lacuna Coil’s Andrea Ferro Talks Influences, Skateboarding, Band Origins + More””Slayer - Reign in Blood””Into The Lungs of Hell””Slayer rules - en utställning om fans””Slayer and Their Fans Slashed Through a No-Holds-Barred Night at Gas Monkey””Home””Slayer””Gold & Platinum - The Big 4 Live from Sofia, Bulgaria””Exclusive! Interview With Slayer Guitarist Kerry King””2008-02-23: Wiltern, Los Angeles, CA, USA””Slayer's Kerry King To Perform With Megadeth Tonight! - Oct. 21, 2010”originalet”Dave Lombardo - Biography”Slayer Case DismissedArkiveradUltimate Classic Rock: Slayer guitarist Jeff Hanneman dead at 49.”Slayer: "We could never do any thing like Some Kind Of Monster..."””Cannibal Corpse'S Pat O'Brien Will Step In As Slayer'S Guest Guitarist | The Official Slayer Site”originalet”Slayer Wins 'Best Metal' Grammy Award””Slayer Guitarist Jeff Hanneman Dies””Kerrang! Awards 2006 Blog: Kerrang! Hall Of Fame””Kerrang! Awards 2013: Kerrang! Legend”originalet”Metallica, Slayer, Iron Maien Among Winners At Metal Hammer Awards””Metal Hammer Golden Gods Awards””Bullet For My Valentine Booed At Metal Hammer Golden Gods Awards””Metal Storm Awards 2006””Metal Storm Awards 2015””Slayer's Concert History””Slayer - Relationships””Slayer - Releases”Slayers officiella webbplatsSlayer på MusicBrainzOfficiell webbplatsSlayerSlayerr1373445760000 0001 1540 47353068615-5086262726cb13906545x(data)6033143kn20030215029