strcmp help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 63
Reputation: Cosa is an unknown quantity at this point 
Solved Threads: 0
Cosa Cosa is offline Offline
Junior Poster in Training

strcmp problem

 
0
  #1
Mar 12th, 2008
  1. void ChangeAddress()
  2. {
  3. char searchaddress;
  4. char newaddress;
  5.  
  6. cout << endl;
  7. cout << "Please enter street name to be changed ";
  8. cin >> searchaddress;
  9. for (int i=0; i<numrec; i++)
  10. {
  11. if (strcmp(SubscriberID[i].Subscriber_Address.streetname, searchaddress) == 0);
  12. {
  13. cout << "Enter the new street name: ";
  14. cin >> newaddress;
  15.  
  16. }
  17. }
  18.  
  19. }

error: invalid conversion from ‘char’ to ‘const char*’
error: initialising argument 2 of ‘int strcmp(const char*, const char*)'

I get this error wen using "strcmp(SubscriberID[i].Subscriber_Address.streetname, searchaddress) == 0);"

This function is part of a program which enters subrcribers into a file system. This specific function is supposed to edit the address of a subscriber. The subscribers are loaded to the array via input stream, and then the array is saved back to the text file. I can assume that there is no subscribers with any identical information.
searching the net hasnt given me much help, all i have found is that const char* is a pointer. All i want to do is compare the string in the array to the string entered by the user. Then if the string in array is equal to the one entered, i will then enter a new string to replace the one in the array. any ideas?
Last edited by Cosa; Mar 12th, 2008 at 7:39 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: strcmp help

 
0
  #2
Mar 12th, 2008
it's self-explanatory

"searchaddress" is a character, not a string
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: strcmp problem

 
0
  #3
Mar 12th, 2008
You need to have, e.g.
  1. #define ADDRESS_LEN_MAX 123
  2. char searchaddress[ADDRESS_LEN_MAX] = "";
  3. char newaddress[ADDRESS_LEN_MAX] = "";
or perhaps use std::string (with appropriate changes to the code)
i.e.
  1. std::string searchaddress;
  2. std::string newaddress;
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: strcmp help

 
0
  #4
Mar 12th, 2008
Also remove the semicolor ( ; ) at the end of the if statement.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: Cosa is an unknown quantity at this point 
Solved Threads: 0
Cosa Cosa is offline Offline
Junior Poster in Training

Re: strcmp help

 
0
  #5
Mar 12th, 2008
  1. error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘2’ to ‘int strcmp(const char*, const char*)

I get this error if i use

std::string searchaddress;
std::string newaddress;

And #define isnt supposed to be used in this program. Any other ideas?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: strcmp help

 
0
  #6
Mar 12th, 2008
You need to use the c_str( ) method of the string type to get a c-style string for the strcmp function. This method returns a char*.
  1. std::string searchaddress;
  2. std::string newaddress;
  3.  
  4. if ( strcmp( newaddress.c_str(), searchaddress.c_str() ) == 0)

But, as long as you're using string types, just compare them directly!
  1. if( searchaddress == newaddress )
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: Cosa is an unknown quantity at this point 
Solved Threads: 0
Cosa Cosa is offline Offline
Junior Poster in Training

Re: strcmp help

 
0
  #7
Mar 12th, 2008
The compare is more like this

  1. strcmp(SubscriberID[i].Subscriber_Address.streetname, searchaddress)

So its comparing the searchaddress to the address in the array. (To store each users details i made an array of structs of type SubscriberID). Then if they are equal i will enter the new address and then replace the address in the array with the new one entered. So would it work like this?

  1. std::string searchaddress;
  2. std::string newaddress;
  3.  
  4. if ( strcmp( SubscriberID[i].Subscriber_Address.streetname, searchaddress.c_str() ) == 0)

If i use this it compiles and runs, but nothing happens after i type in the address to be searched.
  1. char * cstr, *searchaddress;
Last edited by Cosa; Mar 12th, 2008 at 6:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: strcmp help

 
0
  #8
Mar 12th, 2008
What is the datatype of .streetname? Perhaps if you posted a larger code segment, showing the class or struct definition(s), we'd clear this up more easily.

Simply declaring char * searchaddress; does not give you any place to store input from the user - you've got to give it some memory. Either with the new operator, or declare it as an char array instead char searchaddress[100];
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

Re: strcmp help

 
0
  #9
Mar 13th, 2008
Do your address have space? cin can not read a string with space. maybe you should use getline to read the address.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: Cosa is an unknown quantity at this point 
Solved Threads: 0
Cosa Cosa is offline Offline
Junior Poster in Training

Re: strcmp help

 
0
  #10
Mar 13th, 2008
I am not at home at the moment so i cant post any code, however i will try declaring the char as an array. I think i already tried that but im not to sure. Street name is of data type char, but i think it might be declared in this fashion
  1.  
  2. char streetname = [30]

I will post the whole code when i get the chance
Last edited by Cosa; Mar 13th, 2008 at 1:00 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC