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
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.
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* .
You can get the length of a string using the strlen function. This function is declared in the header file string.
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++.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With