#problem with for loop

10 messages · Page 1 of 1 (latest)

hearty stump
#

I have this function that checks the array and makes a for loop dedicated to checking if the user input matches the array. When the user types "action" (the first string in the array) everything works fine, but every other string returns a false statement until they type it again

trim sentinelBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

hearty stump
#
void Select(string arr[],string a){
      bool found = false;
  //while the user is putting in bad inputs, it will constantly ask for a new genre choice
  while(!found){
    for(int x=0; x<=9; x++){
      //i believe the only reason why this works best with "action" is because it is the first string in the array
    if(a!=arr[x]){
    cout << "idk what that is, try again \n > ";
    std::cin >> a;}  
      break;
      }
    //what if you took the for loop below out of this while loop
      for(int x=0; x<9; x++){
    if(a==arr[x]){
      std::cout << " okay so you wanna read a " + a + " book, what category of book are you intrested in?\n";
      cout << "-------------------------\n";
      found = true;
      break;}
  }
    }
}```
#
  string genre[9]={"action","thriller","romance","psychological","horror","non fiction", "fiction", "fantasy", "sci-fi", };```
#

I feel like its because the for loop starts at 0 and thats why it works best with the first array

#

but then why does it accept any ther string in the array the second time, is it the placement of my 'cin'?

trim sentinelBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
trim sentinelBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
cerulean finch
#

I would reccomend stepping trough your code with a debugger to see why mistakes happen

trim sentinelBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.