Thread: newbie problem

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    34

    newbie problem

    how do i define a string variable in the tutorial there are only int float and char

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    const int STRING_LEN=512;
    char String[STRING_LEN];
    Be a leader and not a follower.

  3. #3
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669
    [owner has put his foot in his mouth]

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    34
    lol i found it there is a way just by including the file string
    #include <string>

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Just declare

    Code:
    string s;
    string good, char* bad!
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    34
    how do i check the value of a string?

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    As long as you are using the string class and not char*, you can use the normal comparison operators (<,>,==,!=,<=,>=). Also, strings have a method compare( string ) that gives a more quantitative comparison:

    Code:
    string s1 = "a string", s2 = "another string";
    
    int comparison = s1.compare( s2 );
    
    if (comparison == 0)
        cout << "The two strings are the same\n";
    else if (comparison < 0)
        cout << "s1 is lexicographically less than s2\n";
    else cout << "s2 is lexicographically less than s1\n";
    If you are using char*'s, you can use the strcmp function; it works the same way as the string.compare() function above.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  2. Newbie problem with scanf function...
    By Mithal in forum C Programming
    Replies: 1
    Last Post: 11-13-2005, 10:28 PM
  3. Newbie ... looping problem
    By StupidIntel in forum C++ Programming
    Replies: 12
    Last Post: 05-13-2004, 06:45 PM
  4. Problem with code (newbie question)
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2002, 01:39 AM
  5. newbie coding problem
    By rippascal in forum C++ Programming
    Replies: 10
    Last Post: 01-08-2002, 11:45 PM