473,796 Members | 2,429 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string comparision

Hi there,

I am trying to compare two strings. But I am not able to do so. I use
:
if(function_nam e[function_number] == function_label[k]){

but this does not work.

I have written down my code:

const char *function_name[(code_addr_t)co de_size];
string function_label[(code_addr_t)co de_size];
int function_number = 0;
int i = 0;

ifstream inputStream("no _args.txt");
if(!inputStream )
die();

while(!inputStr eam.eof())
{
getline(inputSt ream,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_nam e[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 2534
priyanka wrote:
I am trying to compare two strings. But I am not able to do so. I use
>>
if(function_nam e[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)co de_size];
string function_label[(code_addr_t)co de_size];
int function_number = 0;
int i = 0;

ifstream inputStream("no _args.txt");
if(!inputStream )
die();

while(!inputStr eam.eof())
{
getline(inputSt ream,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_numbe r'
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_nam e[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_nam e[function_number] == function_label[k]){

but this does not work.

I have written down my code:

const char *function_name[(code_addr_t)co de_size];
string function_label[(code_addr_t)co de_size];
int function_number = 0;
int i = 0;

ifstream inputStream("no _args.txt");
if(!inputStream )
die();

while(!inputStr eam.eof())
{
getline(inputSt ream,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(i nputStream,func Name))
{
....
}
if (!inputStream.e of())
{
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_nam e[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)co de_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(inputSt ream,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::s tring,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
10629
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 namespace std; : : vector<string>* getTyphoon()
6
1854
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 of the ways of doing this are 1. Read each line and do string compare immediately. 2. Read each line, add each string into a stl vector and search for the given string using stl bineary_search algorithm.
32
49734
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(). Instead one has to iterate through the string, parse all UTF-8 multibytes and count each multibyte as one character. To address this problem the GTKmm bindings for the GTK+ toolkit have implemented a own string class Glib::ustring...
3
1642
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 if/select case, etc; after performing the case-insenisitive compare, the indicator can be reset to allow the conventional comparision of strings? kd
2
2666
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 both of them. There has been a lot of pressure from business to move to DB2 Connect from Shadow Direct which the company is currently using since almost 7 years. I would greatly appreciate if any one of you can help or send me some comparision...
30
3322
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 string to all lowercase? (I know how to do it manually, but in the interests of portability...) Thanks Steve
33
4693
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 of articles I read from different experts and programmers tell me that their "gut feelings" for using stringBuilder instead of string concatenation is when the number of string concatunation is more then N ( N varies between 3 to max 15 from...
8
5745
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 Hex digits like "0a" ,"00" , "0b" whose ascii value is New line , NULL , Tab respectively brings the rest of the string to new line or put a tab in between .
9
5043
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
2902
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 result. On tracing the program i noticed that the strings "s" and "t" get modified somehow in the comp function and as a result i am getting incorrect results. Can anyone please point out the error?
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.