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.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?

Recommended Answers

All 10 Replies

it's self-explanatory

"searchaddress" is a character, not a string

You need to have, e.g.

#define ADDRESS_LEN_MAX 123
char searchaddress[ADDRESS_LEN_MAX] = "";
char newaddress[ADDRESS_LEN_MAX] = "";

or perhaps use std::string (with appropriate changes to the code)
i.e.

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

Also remove the semicolor ( ; ) at the end of the if statement.

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*.

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!

if( searchaddress == newaddress )

The compare is more like this

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?

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.

char * cstr, *searchaddress;

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];

Do your address have space? cin can not read a string with space. maybe you should use getline to read the address.

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

char streetname = [30]

I will post the whole code when i get the chance

void ChangeAddress()
{
	char name[31];

	cout << "Enter first name of subscriber: ";
	cin >> name;

	for(int i = 0; i < numrec; i++)
	{
		if(strcmp(SubscriberID[i].Firstname,name) == 0 )
		{

			cin.clear();
			cin.ignore(100,'\n');

			cout << "Subscriber found!" << endl;
			cout << "Please enter new details" << endl;
			
			cout << "Enter new street name: " << endl;
			cin.getline(SubscriberID[i].Subscriber_Address.streetname, size);
			
			cout << "Enter new suburb: " << endl;
			cin.getline(SubscriberID[i].Subscriber_Address.suburb, size);

			cout << "Enter new state: " << endl;
			cin.getline(SubscriberID[i].Subscriber_Address.state, size);
			
			cout << "Enter new postcode: " << endl;
			cin >> SubscriberID[i].Subscriber_Address.postcode;
		}
	}
}

I got it to work, thats the working code, thanx for ur help guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.