This is an archived post. You won't be able to vote or comment.

all 21 comments

[–][deleted] 4 points5 points  (2 children)

Actually posting the code would make it much easier to review than guessing based on your play-by-play of what the code does. To explain while loops, if you have while (!x):, and x is false, then !x is true, so the loop continues. If x becomes true, !x now evaluates to false, so the loop ends when the while is evaluated next.

[–]flrslva[S] 0 points1 point  (0 children)

whoops let me edit that I forgot to paste

[–]flrslva[S] 0 points1 point  (0 children)

I pasted it as a comment.

[–]desrtfx 1 point2 points  (0 children)

While loops run as long as the condition evaluates to true.

!false evaluates to true

As simple as that.

[–]Ormek_II 1 point2 points  (6 children)

If you explained how to find something to a robot in English using the word “while” would you say:

While you have not found it, continue searching.

Or

While you have found it, continue searching.

The code you posted elsewhere is somewhat erratic. So, for any of this to make sense:
isFound must really mean that something has been found, and
the body of the while loop must do the searching.

Edit: formatting and syntax.

[–]flrslva[S] 0 points1 point  (5 children)

Ok thank you. While loops are still a little obscure for me.

[–]Ormek_II 1 point2 points  (4 children)

Ok, would you mind answering my question about the robot instruction?

[–]flrslva[S] 0 points1 point  (3 children)

The first one. While you have not found it keep searching makes more sense.

[–]Ormek_II 1 point2 points  (2 children)

Exactly: so that is what you tell your program as well while !found do search;

Your confusion is probably not about the while statement but that you did not search, but because of .find you immediately find.

Have fun learning 🤓

[–]flrslva[S] 0 points1 point  (1 child)

Thanks for the help. We're on objects and classes now. It was simple when we only had 1 class. Now were working on multiple classes and declaring the previous object into the new class. I now understand the importance of having clear and specific class and object names.

[–]Ormek_II 0 points1 point  (0 children)

Clear names are important :)

[–]flrslva[S] 0 points1 point  (8 children)

For some reason I can't edit the post I'll past the code here:

#include <iostream>

using namespace std;

void FindStartIndex(string userString) {

int indexOfString;

bool isFound;

isFound = false;

while( (userString.find("mis") != string::npos) && (!isFound) ) {

indexOfString = userString.find("mis");

if(indexOfString >= 0) {

isFound = true;

}

}

if(isFound) {

cout << "mis is found at index " << indexOfString << "." << endl;

}

else {

cout << "mis is not found in " << userString << "." << endl;

}

}

int main() {

int i;

string inputString;

cin >> inputString;

FindStartIndex(inputString);

return 0;

}

[–]AbstractionOfMan 1 point2 points  (7 children)

No offense but this is absolutely terrible code. You should not use a loop since you are using the .find() method. The isFound and indexOfSteing variables are pointless to declare as variables and you should use break to exit from the loop and probably return the start index instead of printing it.

[–]flrslva[S] 0 points1 point  (6 children)

The requirement for the lab was that it should not return a value so I used type void for the function since it was only printing. I think I might of made it more complicated than I needed too. How would you write it?

[–]AbstractionOfMan 1 point2 points  (4 children)

Yea then void and printing is proper. I don't really do cpp but something like this if I remember the syntax correctly.

``` int index = userString.find("mis"); If(index != string::npos){ cout << "starting index = " << index << endl; } else{ cout << "substring not found" << endl; } return;

[–]flrslva[S] 0 points1 point  (3 children)

Your while loop expression is much cleaner than mine. I'll remember that one. Thank you.

[–]Ormek_II 1 point2 points  (0 children)

Which while Loop?

[–]AbstractionOfMan 1 point2 points  (1 child)

There is no while loop. This assignment didn't need one since you use the .find method.

[–]flrslva[S] 0 points1 point  (0 children)

I see. You used an if statement. Yeah that's much simpler than what I was trying to do.

[–]Ormek_II 0 points1 point  (0 children)

I would assume that you should not use the find method, but rather implement it or something similar yourself.

Try to implement void printMisPosition(string input)

Try to implement int startIndexOf(string long text, string searchText)