### Recursion problem Hi, I am having trouble having my code read the recursive search. If I enter above the limit it lets me know and I can try again or if I go below it does the same but if I enter a number inside the parameter it just exits the program and doesn't even hit the second if/else statement inside my main function.

I am thinking maybe it has something to do with the boolean methodStatus but I am not sure.

any help would be appreciated thank you.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384`` `````` #include using namespace std; const int LENGTH = 500; void arrayInit(int input[], int size); int recursiveLinearSearch(int input[], int key, int size, bool &methodStatus, int starter); int main() { const int size = LENGTH; int thatOneArray[size]; arrayInit(thatOneArray, size); bool methodStatus = NULL; int key = NULL; int starter = 0; char sentinel = 'y'; while (sentinel == 'y' || sentinel == 'Y') { cout << "Please enter the value you would like to search for: "; cin >> key; if (key > 0 && key < size) { break; } else { cout << "Please enter a value greater than 0 and less than " << size << "." << endl; } int index = recursiveLinearSearch(thatOneArray, key, size, methodStatus, starter); if (methodStatus == true) { cout << "The value was in position number " << index + 1 << "." << endl; cout << "The function was called " << index << " times." << endl; } else if(methodStatus == false) { cout << "The number does not exist in the array" << endl; cout << "The function was called " << index << " times" << endl; } cout << "Would you like to continue? enter y or Y, all other entries exit program: "; cin >> sentinel; } return 0; } void arrayInit(int input[], int size) { for (int i = 0; i < size; i++) { input[i]; } cout << "array initialized" << endl; } int recursiveLinearSearch(int input[], int key, int size, bool& methodStatus, int starter) { if (input[starter] == 0) { methodStatus = true; return starter; if (starter > size && starter != key) { methodStatus = false; return 0; } else { int returner = recursiveLinearSearch(input, key, size, methodStatus, starter); return returner; } } } `````` I think you are mixing up your “input validation” loop with your “run again” loop. It looks almost like you’re trying to do them both at once. I suggest trying to remove that from the other parts of the program logic using functions so there’s less confusion.

So maybe something more like this kinda thing:
 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````// variables bool run_again = false; bool valid_input = false; string input_str; char input_char; // run again loop do { // input validation loop input_str = get_valid_input_str(); // only the words "hello" and "goodbye" are valid input... // actual program if(input_str == "hello") { cout << "hey" << endl; } else { cout << "bye" << endl; } // ask if they want to go again. cout << "run again? (Y/N)" << endl; cin >> input_char; if(input_char == 'Y') { run_again = true; } else { run_again = false; } } while(run_again);``````

Last edited on ``12345678910111213141516171819`` ``````#include int search(int const* xs, int sz, int x) { return sz == 0 || *xs == x? 0: 1 + search(xs + 1, sz - 1, x); } int const size = 10; int main() { int const xs[size] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (int x; std::cin >> x; ) { int const off = search(xs, size, x); if (off == size) std::cout << x << " not found\n"; else std::cout << x << " found at offset " << off << '\n'; } }``````