Problem description: Function 'gets' could not be resolved

Hello everybody
I need help with error: gets (str); Problem description: Function 'gets' could not be resolved.

I used eclipse

Thank you so much.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  #include<iostream>

#include<string.h>

using namespace std;

int main(){

char str[100],temp;

cout<<"enter a string:";
gets (str);
int i=strlen(str)-1;

for(int j=0;j<i;j++,i--){

temp=str[j];

str[j]=str[i];

str[i]=temp;

}

cout<<"After reversing: "<<str;

return 0;

}.
> I need help with error: gets (str); Problem description: Function 'gets' could not be resolved.
Because gets() has been permanently banned from ever appearing in any more programs.

Use this.
http://www.cplusplus.com/reference/istream/istream/getline/
What compiler are you using? Do you realize that that dangerous C function is no longer part of the C standard and your C++ compiler may no longer support that exceedingly dangerous C function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
   std::string str;

   std::cout << "Enter a string: ";
   std::getline(std::cin, str);
   std::cout << '\n';

   std::string rev_str = str;

   std::reverse(rev_str.begin(), rev_str.end());

   std::cout << "Before reversing: " << str << '\n';
   std::cout << "After reversing:  " << rev_str << "\n\n";

   // let's reset the reversed string
   rev_str = str;

   std::cout << "Before reversing: " << rev_str << '\n';

   // reverse string: method 2
   for (size_t loop = 0; loop < str.size(); loop++)
   {
      rev_str[loop] = str[str.size() - loop - 1];
   }
   std::cout << "After reversing:  " << rev_str << '\n';
}

Enter a string: This is a test

Before reversing: This is a test
After reversing:  tset a si sihT

Before reversing: This is a test
After reversing:  tset a si sihT

If you are required to use char array strings:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cstring>

int main()
{
   const size_t str_size = 100;

   char str[str_size];

   std::cout << "Enter a string: ";
   std::cin.getline(str, str_size);
   std::cout << '\n';

   std::cout << "Before reversing: " << str << '\n';

   for (size_t i = 0; i < strlen(str) / 2; i++)
   {
      char temp = str[i];

      str[i] = str[strlen(str) - i - 1];

      str[strlen(str) - i - 1] = temp;
   }

   std::cout << "After reversing:  " << str << '\n';
}
If you absolutely have to use C I/O, use C11's gets_s that isn't a known security risk:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cstring>
#include <cstdio>

int main()
{
   const size_t str_size = 100;

   char str[str_size];

   std::cout << "Enter a string: ";
   gets_s(str, str_size);

   std::cout << "Before reversing: " << str << '\n';

   for (size_t i = 0; i < strlen(str) / 2; i++)
   {
      char temp = str[i];

      str[i] = str[strlen(str) - i - 1];

      str[strlen(str) - i - 1] = temp;
   }

   std::cout << "After reversing:  " << str << '\n';
}

use C11's gets_s that isn't a known security risk:

It may not be a security risk, but it is optional and it is not available with all compilers. Since this is C++ better to stick with C++ methods.
salem c, just wanted to say I love your wording of the function being "permanently banned" :D
Usually people just go with the boring "deprecated" wording.
It may not be a security risk


https://en.cppreference.com/w/c/io/gets

The gets() function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflow attacks. It cannot be used safely (unless the program runs in an environment which restricts what can appear on stdin). For this reason, the function has been deprecated in the third corrigendum to the C99 standard and removed altogether in the C11 standard. fgets() and gets_s() are the recommended replacements.

Never use gets().

I'd call the possibility of buffer-overflow attacks as being a security risk.
Yes gets() is a definite security risk, but that quote was talking about the optional "safe" function gets_s().

You chose to quote only half of what I said:
If you absolutely have to use C I/O, use C11's gets_s that isn't a known security risk

That was from my 3rd code sample, the first two used C++ methods.

I'd use gets_s for keyboard input before I use fgets. fgets I'd reserve for file input.

I think our discussion back and forth scared off the OP. Ooops! :)
What do you think is the benefit of using gets_s() over fgets()? Remember that the stdin is a file.

Edit: And yes I saw that you had multiple methods, but IMO mixing C++ io with C io is a poor practice. If you're going with stdio for input stick with stdio for output as well.

You chose to quote only half of what I said:

Because I was only pointing out that the "safe" stdio functions are optional and are not available with all compilers, the rest of what you said was on point.
Last edited on
Topic archived. No new replies allowed.