cplusplus help w/comparing strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fowle040
    New Member
    • Sep 2006
    • 14

    #1

    cplusplus help w/comparing strings

    Code:
    #include <iostream>
    #include <string>
    #include <ctime>
    using namespace std;
    
    class Address{
    private:
    	string street, city, state, zip, country;
    	
    public:///////////////std::string Address::street2
    	Address();
    	Address(string, string, string, string, string);
    	Address(const Address &);
    	bool compare(Address &);
    	bool merge (Address &);
    
    
    
    	
    	void setStreet(string);
    	void setCity(string);
    	void setState(string);
    	void setZip(string);
    	void setCountry(string);
    
    	string getStreet()const;
    	string getCity()const;
    	string getState()const;
    	string getZip()const;
    	string getCountry()const;
    	
    	void print()const;
    	void read();
    };
    Address::Address(){
    	street="";
    	city="";
    	state="";
    	zip="";
    	country="";
    }
    Address::Address(string st, string c, string s, string z, string ct){
    	street=st;
    	city=c;
    	state=s;
    	zip=z;
    	country=ct;
    }
    Address::Address(const Address & copy){
    	street=copy.street;
    	//street2=copy.street2;
    	city=copy.city;
    	state=copy.state;
    	zip=copy.zip;
    	country=copy.country;
    }
    void Address:: setStreet(string st){
    	street=st;
    }
    void Address::setCity(string c){
    	city=c;
    }
    void Address::setState(string s){
    	state=s;
    }
    void Address::setZip(string z){
    	zip=z;
    }
    void Address::setCountry(string ct){
    	country=ct;
    }
    string Address::getStreet()const{
    	return street;
    }
    string Address::getCity()const{
    	return city;
    }
    string Address::getState()const{
    	return state;
    }
    string Address::getZip()const{
    	return zip;
    }
    string Address::getCountry()const{
    	return country;
    }/////////////////////////////////////////////////////////////////////////////
    bool Address::compare(Address & b){
    if (street==b.street)
    {cout<<"same street"<<endl;}
    	if(state==b.state)
    	{cout<<"same state"<<endl;}
    		if(city==b.city)
    		{cout<<"samae city"<<endl;}
    			if(zip==b.zip)
    			{cout<<"same code"<<endl;}
    				if(country==b.country)
    				{cout<<"same country"<<endl;}
    					return true;
    return false;
    }
    void Address::print()const{
    	cout<<"Your Current Street:"<<street<<endl;
    	cout<<"Your Current City:"<<city<<endl;
    	cout<<"Your Current State:"<<state<<endl;
    	cout<<"Your Current Code:"<<zip<<endl;
    	cout<<"Your Current Country:"<<country<<endl;
    	
    	
    }
    void Address::read(){
    	cout<<"Enter Your Current Street:"<<endl;
    	getline(cin,street);
    	cout<<"Enter Your Current City:"<<endl;
    	getline(cin,city);
    	cout<<"Enter Your Current State:"<<endl;
    	getline(cin,state);
    	cout<<"Enter Your Current Code:"<<endl;
    	getline(cin,zip);
    	cout<<"Enter Your Current Country:"<<endl;
    	getline(cin,country);
    }
    
    int main(){
    	Address location1("1001 Kalapoosa St","Tuscaloosa","Al","35401","USA"),location2;/*("90","Northport","Al","35476","USA")*/
    	Address copy("1002 10th Ave","Tuscaloosa","Al","35406","USA");
    
    location2.setStreet("298");
    location2.setCity("Talapoosa");
    location2.setState("GA");
    location2.setZip("35478");
    location2.setCountry("USA");
    
    cout<<endl;
    cout<<"location1"<<endl;
    location1.print();
    cout<<endl;
    
    cout<<endl;
    cout<<"location2"<<endl;
    location2.print();
    cout<<endl;
    
    Address location3(copy);
    cout<<endl;
    cout<<"location3"<<endl;
    location3.print();
    cout<<endl;
    ///////////////////
    /*
    Address b.location(location3);
    cout<<endl;
    cout<<"new address"<<endl;
    b.print();///////////////////
    */
    string a, b, c;
    if (a.compare(b))
    cout<<"This is truely a pair"<<endl;
    else 
    cout<<"false, not a pair"<<endl;
    
    location3.setStreet("1");
    //location3.setStreet2("O st");
    location3.setCity("Tuscaloosa");
    location3.setState("TX");
    location3.setZip("34576");
    location3.setCountry("UK");
    /*
    cout<<endl;
    cout<<"new location 2"<<endl;
    location2.compareState(location3);
    location2.print();
    cout<<endl;
    */
    
    //c.getState(b);
    location2.read();
    cout<<endl;
    cout<<"location 2 new info changed to:"<<endl;
    location2.print();
    
    
    system ("PAUSE");
    return 0;
    }
    Last edited by Ganon11; Jan 25 '07, 10:47 PM. Reason: code tags added
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    ...and your question is...?

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      Um....what is your question?

      Ganon, did you delete this poor kid's question?

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Nossir, I added code tags and that is all. Made it easier to read, you see.

        Comment

        • DeMan
          Top Contributor
          • Nov 2006
          • 1799

          #5
          You can't use == to compare strings. you need to use strcmp or strncmp (which I think is in string.h)

          strcmp compares 2 null termintaed strings and returns 0 if they are the same. I'm not sure you can rely on this, but normally the way it works is to return the difference of the first unlike character, so if the first string's first different character has a lower ascii code than the seconds a negative number is returned, otherwise a positive)

          strcmp is not a 'safe' method because it expects the null terminator, so it is better to use strncmp. srncmp is much the same except it takes an extra argument of a length to compare over (and thus avoiding the problem of needing terminated string) making it a little more difficult to use , but checking that the length of 2 strings are the same can quickly tell you that they are NOT the same (but not necessarily ARE).

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            Originally posted by Ganon11
            Nossir, I added code tags and that is all. Made it easier to read, you see.
            Oh I thought your delete button might be right next to the one labeld [Code]

            Comment

            Working...