473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

cplusplus help w/comparing strings

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. class Address{
  7. private:
  8.     string street, city, state, zip, country;
  9.  
  10. public:///////////////std::string Address::street2
  11.     Address();
  12.     Address(string, string, string, string, string);
  13.     Address(const Address &);
  14.     bool compare(Address &);
  15.     bool merge (Address &);
  16.  
  17.  
  18.  
  19.  
  20.     void setStreet(string);
  21.     void setCity(string);
  22.     void setState(string);
  23.     void setZip(string);
  24.     void setCountry(string);
  25.  
  26.     string getStreet()const;
  27.     string getCity()const;
  28.     string getState()const;
  29.     string getZip()const;
  30.     string getCountry()const;
  31.  
  32.     void print()const;
  33.     void read();
  34. };
  35. Address::Address(){
  36.     street="";
  37.     city="";
  38.     state="";
  39.     zip="";
  40.     country="";
  41. }
  42. Address::Address(string st, string c, string s, string z, string ct){
  43.     street=st;
  44.     city=c;
  45.     state=s;
  46.     zip=z;
  47.     country=ct;
  48. }
  49. Address::Address(const Address & copy){
  50.     street=copy.street;
  51.     //street2=copy.street2;
  52.     city=copy.city;
  53.     state=copy.state;
  54.     zip=copy.zip;
  55.     country=copy.country;
  56. }
  57. void Address:: setStreet(string st){
  58.     street=st;
  59. }
  60. void Address::setCity(string c){
  61.     city=c;
  62. }
  63. void Address::setState(string s){
  64.     state=s;
  65. }
  66. void Address::setZip(string z){
  67.     zip=z;
  68. }
  69. void Address::setCountry(string ct){
  70.     country=ct;
  71. }
  72. string Address::getStreet()const{
  73.     return street;
  74. }
  75. string Address::getCity()const{
  76.     return city;
  77. }
  78. string Address::getState()const{
  79.     return state;
  80. }
  81. string Address::getZip()const{
  82.     return zip;
  83. }
  84. string Address::getCountry()const{
  85.     return country;
  86. }/////////////////////////////////////////////////////////////////////////////
  87. bool Address::compare(Address & b){
  88. if (street==b.street)
  89. {cout<<"same street"<<endl;}
  90.     if(state==b.state)
  91.     {cout<<"same state"<<endl;}
  92.         if(city==b.city)
  93.         {cout<<"samae city"<<endl;}
  94.             if(zip==b.zip)
  95.             {cout<<"same code"<<endl;}
  96.                 if(country==b.country)
  97.                 {cout<<"same country"<<endl;}
  98.                     return true;
  99. return false;
  100. }
  101. void Address::print()const{
  102.     cout<<"Your Current Street:"<<street<<endl;
  103.     cout<<"Your Current City:"<<city<<endl;
  104.     cout<<"Your Current State:"<<state<<endl;
  105.     cout<<"Your Current Code:"<<zip<<endl;
  106.     cout<<"Your Current Country:"<<country<<endl;
  107.  
  108.  
  109. }
  110. void Address::read(){
  111.     cout<<"Enter Your Current Street:"<<endl;
  112.     getline(cin,street);
  113.     cout<<"Enter Your Current City:"<<endl;
  114.     getline(cin,city);
  115.     cout<<"Enter Your Current State:"<<endl;
  116.     getline(cin,state);
  117.     cout<<"Enter Your Current Code:"<<endl;
  118.     getline(cin,zip);
  119.     cout<<"Enter Your Current Country:"<<endl;
  120.     getline(cin,country);
  121. }
  122.  
  123. int main(){
  124.     Address location1("1001 Kalapoosa St","Tuscaloosa","Al","35401","USA"),location2;/*("90","Northport","Al","35476","USA")*/
  125.     Address copy("1002 10th Ave","Tuscaloosa","Al","35406","USA");
  126.  
  127. location2.setStreet("298");
  128. location2.setCity("Talapoosa");
  129. location2.setState("GA");
  130. location2.setZip("35478");
  131. location2.setCountry("USA");
  132.  
  133. cout<<endl;
  134. cout<<"location1"<<endl;
  135. location1.print();
  136. cout<<endl;
  137.  
  138. cout<<endl;
  139. cout<<"location2"<<endl;
  140. location2.print();
  141. cout<<endl;
  142.  
  143. Address location3(copy);
  144. cout<<endl;
  145. cout<<"location3"<<endl;
  146. location3.print();
  147. cout<<endl;
  148. ///////////////////
  149. /*
  150. Address b.location(location3);
  151. cout<<endl;
  152. cout<<"new address"<<endl;
  153. b.print();///////////////////
  154. */
  155. string a, b, c;
  156. if (a.compare(b))
  157. cout<<"This is truely a pair"<<endl;
  158. else 
  159. cout<<"false, not a pair"<<endl;
  160.  
  161. location3.setStreet("1");
  162. //location3.setStreet2("O st");
  163. location3.setCity("Tuscaloosa");
  164. location3.setState("TX");
  165. location3.setZip("34576");
  166. location3.setCountry("UK");
  167. /*
  168. cout<<endl;
  169. cout<<"new location 2"<<endl;
  170. location2.compareState(location3);
  171. location2.print();
  172. cout<<endl;
  173. */
  174.  
  175. //c.getState(b);
  176. location2.read();
  177. cout<<endl;
  178. cout<<"location 2 new info changed to:"<<endl;
  179. location2.print();
  180.  
  181.  
  182. system ("PAUSE");
  183. return 0;
  184. }
Jan 25 '07 #1
5 1732
Ganon11
3,652 Expert 2GB
...and your question is...?
Jan 25 '07 #2
RedSon
5,000 Expert 4TB
Um....what is your question?

Ganon, did you delete this poor kid's question?
Jan 25 '07 #3
Ganon11
3,652 Expert 2GB
Nossir, I added code tags and that is all. Made it easier to read, you see.
Jan 25 '07 #4
DeMan
1,806 1GB
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).
Jan 25 '07 #5
RedSon
5,000 Expert 4TB
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]
Jan 25 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: beliavsky | last post by:
By mistake I coded something like print ("1" > 1) and got the result "True". Comparing an integer and a string seems meaningless to me, and I would prefer to have an exception thrown. Can...
26
by: William Park | last post by:
How do you compare 2 strings, and determine how much they are "close" to each other? Eg. aqwerty qwertyb are similar to each other, except for first/last char. But, how do I quantify that? ...
5
by: Curtis Gilchrist | last post by:
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
6
by: BrianJones | last post by:
I have a problem with the int strcmp(str1,str2) function: When I do: char *pass; char *passv; pass = getpass("Please enter....."); passv = getpass("Please verify.....");
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
2
by: Manny Chohan | last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny
15
by: luc.saffre | last post by:
Hello, here is something that surprises me. #coding: iso-8859-1 s1=u"Frau Müller machte große Augen" s2="Frau Müller machte große Augen" if s1 == s2: pass
1
by: Jetboy555 | last post by:
Sample input: 2000 Georgia Tech 30 Virginia 20 1999 Virginia 20 Virginia tech My Problem is in taking the input in correctly. I take the year in correctly, but i'm having trouble with the...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.