Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I am trying to compile this piece of code but for whatever reason it won't work. Can someone help me? I want to know how to use strlen() properly:

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

I've tried using cin.getline(buffer, 80); but I get the same compile error issue.

My compiler says the error is this

error: strlen was not declared in this scope

like image 622
Person Avatar asked Jul 13 '13 03:07

Person


People also ask

How do I fix the string was not declared in this scope?

You have the following options: Write using namespace std; after the include and enable all the std names: then you can write only string on your program. Write using std::string after the include to enable std::string : then you can write only string on your program. Use std::string instead of string.

How do you declare a strlen in C++?

strlen() Syntax The syntax of the strlen() function is: strlen(const char* str); Here, str is the string whose length we need to find out, which is casted to a const char* .

Where is strlen declared?

You can get the length of a string using the strlen function. This function is declared in the header file string.

Can we use strlen in C++?

The strlen() function returns the length of the given string, excluding the null character('\0'). It is defined in the cstring header file in C++.


1 Answers

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

cstring will give you strlen in the std namespace, while string.h will keep it in the global namespace.

like image 166
Rapptz Avatar answered Dec 14 '22 01:12

Rapptz