char/string hierarchy...

This is a discussion on char/string hierarchy... within the C++ Programming forums, part of the General Programming Boards category; i need my program to use greater-than (>) and less-than (<) IF/ELSE statements for char arrays or strings. but so ...

  1. #1
    OCD script writer syrel's Avatar
    Join Date
    Sep 2004
    Posts
    12

    char/string hierarchy...

    i need my program to use greater-than (>) and less-than (<) IF/ELSE statements for char arrays or strings. but so far, it wont work, even though my professor demonstrated it with confidence. i am using the <string> header and i tried <cstring> but that only complicated things.

    is it possible to compare the values of strings without converting every character to an int value? is there anything that can be done?
    (3.0 Units of C++ Data Structures) - (Lab Time) = Death

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::string str1 = "Apple";
        std::string str2 = "Pear";
    
        if( str1 < str2 )
            std::cout << str1 << " is less than " << str2 << std::endl;
        else if( str2 < str1 )
            std::cout << str2 << " is less than " << str1 << std::endl;
        else
            std::cout << str1 << " is equal to " << str2 << std::endl;
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    OCD script writer syrel's Avatar
    Join Date
    Sep 2004
    Posts
    12
    see, thats what i have been doing but it wont work. other people are telling me to use the <cstring> function strcmp(st1, st2) which will return 0, 1, and -1. if anyone has a better suggestion, i am all ears/eyes.
    (3.0 Units of C++ Data Structures) - (Lab Time) = Death

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You have to tell us what won't work, why, etc. Are you using the string class or C style strings? If you are using the string class, hk_mp5kpdw's example should work. If you are using C style strings, then strcmp should work. If it is not, then it is because of how you implemented it or some other problem with information you haven't mentioned. Show us some code.

  5. #5
    OCD script writer syrel's Avatar
    Join Date
    Sep 2004
    Posts
    12
    i got it working with the strcmp(x,y) function, but here is the code i was referring to earlier incase u wanted to see what i was talking about.


    Code:
    void insert(node *&p, char x[15])
    {
    	if (p==NULL)
    	{
    		p=new(node);
    		strcpy(p->info, x);
    		p->left=NULL;
    		p->right=NULL;
    	}
    	else if(x < p->info)
    	{	cout<<"inserting p->LEFT";
    		insert(p->left, x);
    	}
    	else 
    	{	cout<<"inserting p->RIGHT";
    		insert(p->right, x);
    	};
    }
    thanx for the help....
    (3.0 Units of C++ Data Structures) - (Lab Time) = Death

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    To use hk_mp5kpdw's example, this function
    Code:
    void insert(node *&p, char x[15])
    should take c++ style string as an argument like this
    Code:
    void insert(node *&p, string x)
    and the rest of your code will work fine
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-04-2008, 07:15 PM
  2. Hierarchy Chart software suggestions?
    By MatthewDoucette in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-09-2005, 12:05 PM
  3. Hierarchy container?
    By WannaB_Geek in forum C++ Programming
    Replies: 7
    Last Post: 11-30-2004, 05:33 PM
  4. C++ hierarchy as tree
    By cfriend in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2004, 07:50 AM
  5. wtf is a hierarchy chart??
    By dawiz66 in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2002, 09:38 AM

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