Strlen(...)

This is a discussion on Strlen(...) within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> int main() { char x; while(strlen(x)) < 8 { cin >> x; } return 0; } This ...

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Strlen(...)

    Code:
    #include <iostream.h>
    
    int main()
    {
    char x;
    
    while(strlen(x)) < 8
    {
    cin >> x;
    }
    return 0;
    }
    This code should allow the user to type in a word in x, but should automatically go on to the next operation when the length of x reaches 8. I can't compile it due to an error on the WHILE line, how can I solve it ?

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    > while(strlen(x)) < 8

    while(strlen(x) < 8 )

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    char text[100];
    do {
        cin.width(100);
        cin >> text;
    }while(strlen(text) < 8);

  4. #4
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Code:
    #include <iostream.h>
    
    int main()
    {
    char x;
    
    while(strlen(x) < 8)
    {
    cin >> x;
    }
    return 0;
    }
    7 C:\3.cpp
    passing `char' to argument 1 of `strlen(const char *)' lacks a cast


    Damn...

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    strlen expects char*, not char

    This might do what you are looking for:
    Code:
    char str[9];
    for(int i = 0; i < 8; ++i)
        cin >> str[i];
    str[8] = '\0';
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Thanks... But then how do you use Strlen ?

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    In the above example, you could use strlen(str) since str is a char*. You just can't use it on a char (since its length is always 1).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Playing around strlen :)
    By audinue in forum C Programming
    Replies: 6
    Last Post: 06-13-2008, 03:22 PM
  2. strlen help
    By stewie1986 in forum C Programming
    Replies: 10
    Last Post: 12-04-2007, 11:15 AM
  3. strlen in expressions
    By justforthis1 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 10:28 AM
  4. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 09:18 AM
  5. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 12:34 PM

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