How do I get the string length of a variable (I have googled this)
Printable View
How do I get the string length of a variable (I have googled this)
Do you mean the length of a string variable?
If so , strlen(char *) would be what you need
Try to post the code that explains what you are trying to do and where do you failedQuote:
Originally Posted by Mythic Fr0st
Correct if we are talking about C-strings not C++ stringsQuote:
Originally Posted by kantze
Erm, it doesn't seem to work for variables...
int ul = strlen(usr);
Code:#include <iostream>
using namespace std;
int main ()
{
string cusername = "Sokjin";
string cpassword = "abc";
string admin = "Mythic Fr0st";
string adminpw = "Myth";
string usr;
string pw;
bool con;
cout<<"Please signup"<<"\n";
cout<<"Username: ";
getline(cin, usr);
cout<<"Password: ";
getline(cin, pw);
cout<<"Age: ";
getline(cin, usr);
cout<<"E-mail: ";
getline(cin, pw);
if (usr == cusername || usr == admin && pw == cpassword || pw == adminpw)
{
int ul = strlen(usr);
int pl = strlen("OMG");
if (ul <= 12 && pl <= 7)
{
con=true;
}
}
if (con == true)
{
for (int rt = 0; rt <= 25; rt++)
{
if (rt == 7)
{
cout<<"Thank you for logging in."<<"\n"<<"\n";
cout<<"Details: "<<"\n";
cout<<"Username: "<<usr<<"\n";
cout<<"Password: "<<pw<<"\n";
}
cout<<"\n";
}
}
else
{
cout<<"\n"<<"I am sorry, but your username or password is incorrect."<<"\n";
}
cin.get();
}
as I said strlen works for C-strings not C++
Code:std::string my ("test");
string::size_type len = my.length();