Re: string comparision
priyanka wrote:
I am trying to compare two strings. But I am not able to do so. I use
if(function_name[function_number] == function_label[k]){
but this does not work.
What does it mean "does not work"? Does not compile? Misbehaves? What?
I have written down my code:
const char *function_name[(code_addr_t)code_size];
string function_label[(code_addr_t)code_size];
int function_number = 0;
int i = 0;
ifstream inputStream("no_args.txt");
if(!inputStream)
die();
while(!inputStream.eof())
{
getline(inputStream,funcName);
i++;
function_label[i] = funcName;
You are missing the zeroth element of the 'function_label' array. Is
that on purpose? Perhaps you inteded to use this idiom:
function_label[i++] = funcName;
instead of the two lines above.
}
int tot_func = i;
You're using a misnomer here. 'i' is the last index, not the total
number of the function names.
function_number = function_number + 1;
Why are you adding 1 here?
function_name[function_number] = "sumarray";
You're introducing undefined behaviour right here, if your 'function_number'
is larger than (code_size - 1). Just a thought...
for(int k = 1; k <=tot_func;k++){
In C++ indexing is done from 0 to (size-1). What book are you reading
that doesn't explain that simple feature?
if(function_name[function_number] == function_label[k]){
printf("inside if\n");
}
The execution never enters inside the if { printf("insde if\n");
How do you know? Did you debug your program? What does your 'no_args.txt'
file contains? Are you sure there is at least one occurrence of "sumarray"
string in there?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask