Thread: string length of a variable

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    string length of a variable

    How do I get the string length of a variable (I have googled this)

  2. #2
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    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.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Mythic Fr0st
    How do I get the string length of a variable (I have googled this)
    Try to post the code that explains what you are trying to do and where do you failed
    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

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by kantze
    Do you mean the length of a string variable?

    If so , strlen(char *) would be what you need
    Correct if we are talking about C-strings not C++ strings
    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

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    hmm

    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

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find String length and Arraysearch
    By s.rajaram in forum C Programming
    Replies: 5
    Last Post: 10-03-2007, 02:28 AM
  2. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  3. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM