How do I get the string length of a variable (I have googled this)
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
Last edited by kantze; 01-17-2007 at 03:29 AM.
Try to post the code that explains what you are trying to do and where do you failedOriginally Posted by Mythic Fr0st
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Correct if we are talking about C-strings not C++ stringsOriginally Posted by kantze
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
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(); }
Last edited by Salem; 01-17-2007 at 06:40 AM. Reason: Please stick to code tags for actual code
as I said strlen works for C-strings not C++
Code:std::string my ("test"); string::size_type len = my.length();
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler