Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 3rd, 2007, 11:19 PM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
What the parentheses do

I'm working on this little program that returns the index position of the word "is" contained in a string. Here is the code:

[code]
int main()
{
string text = "My name is Randy.";
string word = "is";
int count = 0;

cout << "The string is " << endl << text;

for (int index = 0; (index = text.find(word, index)) != string::npos ;
index += word.length(), count++)

cout << "Index position of the word is " << index << endl;

return 0;
}

The program works and it tells me the index position of the word "is" is 8.
What I don't understand is why it is necessary to put parentheses around the test condition:

(index = text.find(word, index))

I took them out and then ran my program and I got an infinite loop and it said the index position was 1 and it printed that message in a infinite loop on the screen.

I understand that parentheses force the compiler to evaluate what is in them first, but I guess that's all I know. I don't quite follow why I must include them here.
357mag is offline   Reply With Quote
Old Jun 3rd, 2007, 11:59 PM   #2
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
Because the function find() is called first and then returns a integer value and then tests that to see if it does not equal string:npos.
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Jun 4th, 2007, 3:20 AM   #3
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Sorry. I'm a musician by trade. I don't really follow. If I don't put the parentheses in somehow the test condition never fails and I get an infinite loop or something? The author in the book never explained why the parentheses are needed either.
357mag is offline   Reply With Quote
Old Jun 4th, 2007, 3:57 AM   #4
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
Without them is possible the expression is being evaluated as
index = (text.find(word, index)) != string::npos)
meaning 'index' becomes a boolean value, probably 1 or 0. And because this happens during each iteration of the loop 'index' is being rest and so never advances beyond a single
index += word.length()
__________________
Visit my website BinaryNotions.
Eoin is offline   Reply With Quote
Old Jun 4th, 2007, 6:59 AM   #5
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
(index = text.find(word, index))
That line of code below searches for the first occurrence of the substring word in the current string, starting at position index. If found, return the position of the first character. If not, return a special value - string::npos.

index = text.find(word, index) != string::npos

is the same as

index = (text.find(word, index) != string::npos)
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Jun 5th, 2007, 12:03 AM   #6
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
I understand basically how the program works. It will start at index position 0 which is the beginning of the string and it will return the index position of where the word "is" was found. But I still don't understand why the parentheses are needed to make the code work properly.

You said the find function is called first. Why is that? Is it because the parentheses force the compiler to do that? And then you said something about it returns an integer value. What integer value? Is it a certain number? Or any number? A 1 or 0 perhaps?

I'm trying, but failing.
357mag is offline   Reply With Quote
Old Jun 5th, 2007, 2:22 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Okay, listen up. In the following code,
(index = text.find(word, index)) != string::npos
index will be set equal to text.find (word, index), then that will be tested against string::npos.

Without the parentheses, text.find (word, index) will be tested against string::npos, first and then the result of that test (a boolean value) will be assigned to index. Being a musician has nothing to do with it. Set aside some time to study precedence.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 5th, 2007, 4:34 AM   #8
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Quote:
Originally Posted by DaWei View Post
Okay, listen up. In the following code,
(index = text.find(word, index)) != string::npos
index will be set equal to text.find (word, index), then that will be tested against string::npos.

Without the parentheses, text.find (word, index) will be tested against string::npos, first and then the result of that test (a boolean value) will be assigned to index. Being a musician has nothing to do with it. Set aside some time to study precedence.

I noticed without the parentheses the output window said "found at position 1" and then it kept running an infinite loop. So 1 would be the boolean value representing true then I guess? So then without the parentheses it finds the word I'm looking for ("is") at position 8 in the string, and that evaluates to true (which is 1) and that 1 is assigned to index. I don't know, is that correct?
357mag is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:28 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC