Skip to content
Sign upLog in
← Back to Community
HELP: gets() statement not working in C++
Profile icon
hacker
has Hacker Plan
KeaganLandfried

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 [earned 5 cycles]
View Answer
Voters
Profile icon
RafaelHenriqu44
Profile icon
CrisJes
Profile icon
37788843alan
Profile icon
Chernandezz
Profile icon
RicardoCamacho1
Profile icon
DDiazfigueredo
Profile icon
KeaganLandfried
Comments
hotnewtop
Profile icon
UmbraSicaro

the gets() function in c++ (template: char* gets(char* str)) is included in the 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.

Profile icon
Highwayman

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.

Profile icon
UmbraSicaro

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

Profile icon
Highwayman

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

Profile icon
hyperupcall

@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

Profile icon
Highwayman

@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”

Profile icon
SaurabhBadenkal

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

Profile icon
Dart20

import works on python maiby too on c++

Profile icon
[deleted]

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

Profile icon
Dart20

@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.

Profile icon
Dart20

I dont know it try it with import instead of include