0
#include <iostream>
#include <cstdio>

using namespace std;

int main() {
    char str[100];
    cout << "Enter a string: ";
    gets(str);
    cout << "You entered: " << str;

    return 0; 
}

This is code sample has been coppied exactly form programiz.com and i have no idea how this works I am learning c++ and i wanted to see how the gets function works in action. whenever i try to run this code it says that the identifier is unidentified... I think that either the header cstdio or the function gets has been outdated, but there must be some way to access a string with whitespaces... please help.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 6
    It's been removed from the language for good reasons. It's impossible to use it safely. – Ted Lyngmo Jun 19 '22 at 15:14
  • 3
    As such, I think the correct solution to this problem is to stop reading programiz.com if they are going to present such bad examples. – Nate Eldredge Jun 19 '22 at 15:15
  • 1
    Proper material to learn C++ from are for example [these books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user17732522 Jun 19 '22 at 15:17
  • 3
    And the proper solution for C++ is to use `std::string` from `` instead of `char[100]` as string type and then [`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline) will allow you to read a full line of input regardless of whitespace it contains. – user17732522 Jun 19 '22 at 15:20
  • 1
    @NateEldredge To their credit, it turns out the example is actually only on their reference page for `gets` and a note specifically says that the function shouldn't be used for security concerns and has been removed since C++14, which makes me wonder what OP expected if they know they are specifically using C++20. (https://www.programiz.com/cpp-programming/library-function/cstdio/gets) – user17732522 Jun 19 '22 at 15:26
  • Correct code should look something like this: [https://ideone.com/aIVJGv](https://ideone.com/aIVJGv) – drescherjm Jun 19 '22 at 15:28

0 Answers0