string length of a variable

This is a discussion on string length of a variable within the C++ Programming forums, part of the General Programming Boards category; How do I get the string length of a variable (I have googled this)...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    as I said strlen works for C-strings not C++

    Code:
    	std::string my ("test");
    	string::size_type len = my.length();
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 03: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, 10:37 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21