Ask coding questions

← Back to all posts
HELP: gets() statement not working in C++
h
KeaganLandfried (39)

I was wondering why I am producing an error on line 11 of this code. In Microsoft Visual Studio 2010, this code runs fine. However, the gets() statement will not function properly on repl.it. Does anybody know the error or bug?

Answered by UmbraSicaro (9) [earned 5 cycles]
View Answer
Comments
hotnewtop
UmbraSicaro (9)

the gets() function in c++ (template: char gets(char str)) is included in the <cstdio> header. Include that, also what goes in the parenthesis of gets() is the output variable, not the specific location you want. You need to use an if statement to call gets() when the location you want comes up. You also need to declare whatever variable you put in the parenthesis as your output variable.

Highwayman (1483)

This function was actually deprecated at some point I’m pretty sure. Your other compiler is either behind in standards, or just wanted to keep it. Either way it’s not a steady thing to count on any more so use fgets. (fgets is basically used the same way Umbra used it, but now you also pass the FILE stream and the number of chars you wish read.)

If you’re too attached to gets, just make a macro, but don’t depend on gets anymore it’s not viable.

UmbraSicaro (9)

@Highwayman The function was depreciated in c++11 and removed in c++14, I think

Highwayman (1483)

@UmbraSicaro yeah, his compiler is just one year too late.

eankeen (2184)

@Highwayman its a complete shame that iso/iec standards, especially with the case of iso/iec 9899, are not published freely in their final form. i wanted to check out iec 9899:2018, and unlike ecma262 and others, i cant just read it - there is a paywall

it makes learning c far less enjoyable

Highwayman (1483)

@eankeen I did not actually know exactly what iso/iec until you mentioned it and then when I learned I was like “oh crap that’s some vital sh”

SaurabhBadenkal (2)

its deprecated as its not safe
can switch to fgets(name[i], 50 , stdin);
which also needs #include <cstdio>

Dart20 (36)

import works on python maiby too on c++

[deleted]

@Dart20 their different languages. It wont work, as import for C++ and c is #include

Dart20 (36)

@TaylorLiang there are many same commands at program-languages. Look at cmd for windows. there are IF-, FOR-, EXIT-Statements like in python and many other statements.

Dart20 (36)

I dont know it try it with import instead of include