1

I am trying to solve e problem. I am taking characters as input and using gets(). But the function is showing the above mentioned error.

I don't know why this function is misbehaving. Please help me to find the fault. I am a beginner.

As mentioned the error message is:

Use of undeclared identifier 'gets'

My C++ code:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    char line[1000];
    bool open = true;
    while (gets(line))  //***in this line gets() is showing error***
    {
        int len = strlen(line);
        for (int i = 0; i < len; i++)
        {
            if (line[i] == '"')
            {
                if (open)
                {
                    printf("``");
                }
                else
                {
                    printf("''");
                }
                open = !open;
            }
            else
            {
                printf("%c", line[i]);
            }
        }
        printf("\n");
    }

    return 0;
}

enter image description here

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 2
    There are so many problems in the code you show. Lets begin with ``. [*don't* include that](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). Then `gets`, that's [a dangerous function](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that should never be used. Then there's the problem of only two lines in your code being specific to C++, the rest could be plain C. Who, where or what have taught you "C++"? – Some programmer dude Sep 12 '20 at 10:53
  • Besides that, welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please [don't post images of code](https://idownvotedbecau.se/imageofcode). If you have build errors, then copy-paste the full and complete build output (as text!) into the question instead. – Some programmer dude Sep 12 '20 at 10:55
  • I usually don't use ```bits/stdc++.h>``` but I use the header file in this file because of ```gets()``` was showing error and I tried so many header file but doesn't work. That's why I used `````` in this code. –  Sep 12 '20 at 11:02

2 Answers2

2

std::gets was deprecated in C++11 and removed from C++14, this a dangerous function and it should never be used, though some compilers still provide it, it looks like it's not your case, which is a good thing.

You should use something like std::getline, note that for this you'll need the line argument to be std::string.

string line;

//...

while (getline(cin, line)){
    //...
}

Alternatively, if you really need a char array, you can use fgets instead:

char line[1000];
//...
while(fgets(line, sizeof line, stdin)){
   //remove newline character using strcspn
   line[strcspn(line, "\n")] = '\0';
   //or with C++ std::replace
   replace(&line[0], &line[1000], '\n', '\0'); //&line[1000] one past the array end
   //...
}

Side note:

Consider not using using namespace std; and #include <bits/stdc++.h>, follow the links for details.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
0

gets() is deprecated as already mentioned. Read about it here

But let's get to the root cause of why you got the error in the first place. An

undeclared identifier 'gets'

error is because the compiler can't find the declaration of the function you are using. In your case gets() is defined in stdio.h

I also see that you're using std::getline() as recommended and for that you need to include the string header.

Take a look at the 2 links I've mentioned to understand proper usage.

pcodex
  • 1,812
  • 15
  • 16