i'm with a question related to string comparing...i'm trying to compare two strings (well char pointers in fact :)), im using _stricmp for case insensitive comparison, but even with the conventional strcmp i'm not getting the result i want.

if(_stricmp(value,gen_arr[i])== 0){

							file.seekp(-1,ios::end);
								file.put((i));
								file.flush();
								file.close();
								printf("Editing of field %s successful.\n",field);
								return;
						}

I'm testing this with two "strings", the first coming from the argument line (value) and the other from a char * array (thats it, an array of strings) (gen_arr) and i'm setting value with a known value in the array.

I'm doing some "print" debugging paralel with the proper debug, and the strings are correctly set, they are equal. But the comparison says otherwise. Can you guys give me a hand here?
Thanks a lot ;)

>>if(_stricmp(value,gen_arr)== 0){

gen_arr a single character array? or is it an array of strings? If its a single character array then do this (without indexing) if(_stricmp(value,gen_arr])== 0){

that definition doesn't work to make a 2d char array. if you want a 2d char array you could say char * gen_arr[2][7] = {"String1","String2"}

Its not a 2d char array, its a simple char array... heres the actual declaration:

char * gen_arr[] = {
	"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge",
	"Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B",
	"Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska",
	"Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient",
	"Trip-Hop", "Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical",
	"Instrumental", "Acid", "House", "Game", "Sound Clip", "Gospel",
	"Noise", "AlternRock", "Bass", "Soul", "Punk", "Space", "Meditative",
	"Instrumental Pop", "Instrumental Rock", "Ethnic", "Gothic",
	"Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk",
	"Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta"};

I've noticed that if i do

if(_stricmp("Country",gen_arr[i])== 0){

The comparison is actually being done and the code inside the if clause is executed...

alright sorry total brain fart. char * gen_arr[]; is right. i did a simple test on my machine where i made a variable char * country = "Country"; and the used country and gen_arr[2] in the _stricmp funtion and it was fine. im wondering how you are getting the value of value. is it a char array or a string as in std::string?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

The value of value is got from the command line, specified by the user. It's a const char*...

Heres what i did:

if(_stricmp(field,"genre") == 0){

					//strcpy(newValue,value);
					cout << "value = " << value << endl;

					for(; gen_arr[i]!= 0; ++i){

						cout << "  gen_arr[" << i << "] = " << gen_arr[i] << endl;

						//cout<<strcmp(value,gen_arr[i])<<endl;

						if ( strcmp(value, gen_arr[i]) == 0 ){

							file.seekp(-1,ios::end);
								file.put((i));
								file.flush();
								file.close();
								printf("Editing of field %s successful.\n",field);
								return;
						}	
						//i++;
			}
					strcpy(newValue,value);
					gen_arr[i] = newValue;
					file.seekp(-1,ios::end);
					file.put(i);
					file.flush();
					file.close();
					printf("Editing of field %s successful.\n",field);
					return;
				}

And the output:

value = Country
  gen_arr[0] = Blues
  gen_arr[1] = Classic Ro
  gen_arr[2] = Country
  gen_arr[3] = Dance
  gen_arr[4] = Disco
  gen_arr[5] = Funk
  gen_arr[6] = Grunge
  gen_arr[7] = Hip-Hop
(...)

It's basically the same as what i did before, but now i'm sending you the output.

I'v tried to copy the value from the command line into a local variable using strcpy:

char newValue[30];
strcpy(newValue,value);

if ( strcmp(newValue, gen_arr[i]) == 0 ){
//(...)

But the result remains the same... :S

well im not sure what is going on. i wrote a simple test program and ran it and it works fine for me. this is what i did.

#include <iostream>
#include <string>

using namespace std;

int main()
{
	char * gen_arr[] = {
	"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge",
	"Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B",
	"Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska",
	"Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient",
	"Trip-Hop", "Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical",
	"Instrumental", "Acid", "House", "Game", "Sound Clip", "Gospel",
	"Noise", "AlternRock", "Bass", "Soul", "Punk", "Space", "Meditative",
	"Instrumental Pop", "Instrumental Rock", "Ethnic", "Gothic",
	"Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk",
	"Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta"};
	char country[30];
	cout << "enter the word Coutnry: ";
	cin.getline(country, 30);
	if (_stricmp(country, gen_arr[2]) == 0)
		cout << gen_arr[2];
	cin.get();
	return 0;
}

It's basically the same as what i did before

Maybe something is getting lost in translation here.
Consider attaching the whole code and any necessary input(s) and/or file(s) that produce the issue.

I've cracked it!! Turns out i was getting the data from the command line with an extra space after each string, which causes the comparison to fail. Really lack of concentration there...sorry for that :s

Thanks for the help ;)

This question has already been answered. Start a new discussion instead.