string comparision

P

priyanka

Hi there,

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.

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 = funcName;
}

int tot_func = i;

function_number = function_number + 1;
function_name[function_number] = "sumarray";

for(int k = 1; k <=tot_func;k++){
if(function_name[function_number] == function_label[k]){
printf("inside if\n");

}

The execution never enters inside the if { printf("insde if\n");

Thank you in advance,
 
V

Victor Bazarov

priyanka said:
I am trying to compare two strings. But I am not able to do so. I useif(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 = 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
 
R

Rolf Magnus

priyanka said:
Hi there,

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.

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 = funcName;
}


This is not the right way to go through a stream. eof() will only return
true *after* you tried to read past the end of the file, so your loop is
executed once too often. And if some problem occurs during reading the
file, you'll go into an endless loop.
Make it:

while(getline(inputStream,funcName))
{
....
}
if (!inputStream.eof())
{
std::cout << "Some error occured during reading\n";
}
int tot_func = i;

function_number = function_number + 1;
function_name[function_number] = "sumarray";

for(int k = 1; k <=tot_func;k++){
if(function_name[function_number] == function_label[k]){
printf("inside if\n");

}

The execution never enters inside the if { printf("insde if\n");

Thank you in advance,
 
J

Jon Clements

priyanka said:
Hi there,

I am trying to compare two strings. But I am not able to do so. I use

Well to be honest - I have no idea what you're really trying to achieve
here: another problem being, I'm not 100% sure you do either...
const char *function_name[(code_addr_t)code_size];

This would be an array of char pointers - in fact pointing to nothing
particular. Reading directly into those is going to cause you a head
ache later... Perhaps you're confusing this with something such as
char[code_size] which will give you a char array of code_size space. In
fact, why bother to read into a char* when you are just assigning to a
std::string straight after?
getline(inputStream,funcName);
i++;
function_label = funcName;


Read directly into the string - also, please read what Rolf has
mentioned re: what's called read-ahead - it's sound information.

I'm not sure if you want to store what function is on what line. If so,
use a std::map<std::string,int>, then you can use this to lookup if a
function exists, and get it's line number if required. Also the size of
the map will give you the total functions read.

If this is what you're after - it's possible to change the while to a
for loop and get the line number and string at the same time and assign
to the map.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top