0

In my old C++ project I use to use the gets() command. I've done my research and noticed that it is not reliable anymore and my project won't run with using it.

I use this bit of code right here: Load(gets(new char[50]));

How would I now get this line of code to work properly? And if you could provide an explanation.

SirRyan98
  • 23
  • 6

1 Answers1

1

Here's a simple solution:

std::string text;
std::cout << "Enter some text to load: ";
std::getline(cin, text);
Load(text.c_str());

If you must use character arrays, here's a code fragment:

const size_t ARRAY_CAPACITY = 64U;
char text[ARRAY_CAPACITY];
std::cout << "Enter some text to load: ";
cin.getline(&text[0], ARRAY_CAPACITY);
Load(text);
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154