![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#2 |
|
Sexy Programmer
|
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! |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3
![]() |
Without them is possible the expression is being evaluated as
index = (text.find(word, index)) != string::npos) index += word.length()
__________________
Visit my website BinaryNotions. |
|
|
|
|
|
#5 |
|
Sexy Programmer
|
(index = text.find(word, index)) 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! |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Okay, listen up. In the following code,
(index = text.find(word, index)) != 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 |
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
Quote:
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? |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|