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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  #include <iostream>
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

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';
  }
}


Registered users can post here. Sign in or register to post.