| | |
strcmp help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 63
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
void ChangeAddress() { char searchaddress; char newaddress; cout << endl; cout << "Please enter street name to be changed "; cin >> searchaddress; for (int i=0; i<numrec; i++) { if (strcmp(SubscriberID[i].Subscriber_Address.streetname, searchaddress) == 0); { cout << "Enter the new street name: "; cin >> newaddress; } } }
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.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
You need to have, e.g.
or perhaps use std::string (with appropriate changes to the code)
i.e.
C++ Syntax (Toggle Plain Text)
#define ADDRESS_LEN_MAX 123 char searchaddress[ADDRESS_LEN_MAX] = ""; char newaddress[ADDRESS_LEN_MAX] = "";
i.e.
C++ Syntax (Toggle Plain Text)
std::string searchaddress; std::string newaddress;
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Mar 2008
Posts: 63
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
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?
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*.
But, as long as you're using string types, just compare them directly!
C++ Syntax (Toggle Plain Text)
std::string searchaddress; std::string newaddress; if ( strcmp( newaddress.c_str(), searchaddress.c_str() ) == 0)
But, as long as you're using string types, just compare them directly!
C++ Syntax (Toggle Plain Text)
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Mar 2008
Posts: 63
Reputation:
Solved Threads: 0
The compare is more like this
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?
If i use this it compiles and runs, but nothing happens after i type in the address to be searched.
C++ Syntax (Toggle Plain Text)
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?
C++ Syntax (Toggle Plain Text)
std::string searchaddress; std::string newaddress; 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.
C++ Syntax (Toggle Plain Text)
char * cstr, *searchaddress;
Last edited by Cosa; Mar 12th, 2008 at 6:48 pm.
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
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Mar 2008
Posts: 63
Reputation:
Solved Threads: 0
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
I will post the whole code when i get the chance
C++ Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- no matching function for call to 'strcmp(std::string&, std::string&)' (C++)
- Can somebody explain to me about the 'strcmp' (C)
- C++ strcmp (C++)
- strcmp of 2-D array (C)
- need info? can't use strcmp/string compare (C++)
Other Threads in the C++ Forum
- Previous Thread: Extract a string
- Next Thread: Multithreading C++
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






