"not declared in this scope" when using strlen ()

I am trying to compile this piece of code, but for some reason it will not work. Can anybody help me? I want to know how to use strlen () correctly:

#include<iostream> using namespace std; int main() { char buffer[80]; cout << "Enter a string:"; cin >> buffer; cout << strlen(buffer); return 0; } 

I tried using cin.getline (buffer, 80); but I get the same compilation error error.

My compiler says this is a bug

error: strlen has not been declared in this area

+7
c ++
source share
2 answers

You forgot to include <cstring> or <string.h> .

cstring will give you strlen in the std , and string.h store it in the global namespace.

+24
source share

You need to include the cstring header for strlen :

  #include <cstring> 

you can alternatively include string.h , and that would put strlen in the global namespace, and not in the std . I think it's better to use cstring and stop using using namespace std .

+3
source share

All Articles