473,324 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

string comparision

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[i] = 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,

Jul 18 '06 #1
3 2491
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
Jul 18 '06 #2
priyanka wrote:
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[i] = 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,
Jul 18 '06 #3

priyanka wrote:
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[i] = 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

Jul 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
6
by: rakesh | last post by:
Hi, I have a requirement in which I need to search a string in a file stored on a harddisk. String which needs to be searched would be stored in a file with delimiter as new line character. some...
32
by: Wolfgang Draxinger | last post by:
I understand that it is perfectly possible to store UTF-8 strings in a std::string, however doing so can cause some implicaions. E.g. you can't count the amount of characters by length() | size()....
3
by: kd | last post by:
Hi All, How to perform case-insensitive comparision of strings? Would there be some kind of an indicator, which when set to true, would allow case-insenitive comparision of strings using...
2
by: nirav.lulla | last post by:
I have been given the task to come up with Requirements, Comparision and Migration document from Shadow Direct to DB2 Connect. I am very new much to all this, but kind of know a little bit about...
30
by: Steve Edwards | last post by:
Hi, I'm re-writing some code that had relied on some platform/third-party dependent utility functions, as I want to make it more portable. Is there a standard C/C++/stl routine for changing an stl...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
8
by: abhi147 | last post by:
Hi all , I need to convert a 32 bit string containing hex digits like "76d408cedd600bb578bc0256a751cea2" into it's corresponding 16bit ascii value like "vÎÝ` µx¼V§Q΢" . Now the presence of...
9
by: subramanian100in | last post by:
Suppose we have char *a = "test message" ; Consider the comparison if (a == "string") ..... Here "string" is an array of characters. So shouldn't the compiler
35
by: user34 | last post by:
(Sorry if this gets posted twice) Hi all. I am trying to learn C.As a simple exercise i tried to write a code which would compare two strings using pointers. However i am not getting the correct...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.