Redundant Function (recursion)

I'm simply trying to allow user to input a string, and then select the character he is searching for in the string and the program outputs the position of every occurrence of that character. It works good except there is a random very large number appearing in the output at the end after every search. 'my code:

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
// Template

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void findX (string s, vector<unsigned>& found, unsigned& idx, unsigned& pos, char& findChar)
{
	if (idx != 0){
		pos = found[idx-1];
	}

	found.push_back (s.find(findChar, pos+1));
	cout << findChar << " is " << found[idx] << " places into the string." << endl;
	
	if (found[idx]!=string::npos){		// if more matches, then...
		idx++;
		findX(s, found, idx, pos, findChar);		// Recursion
	}
}

int main()
{
	char findChar = ' ';
	unsigned idx = 0;
	string str = " ";
	unsigned pos = 0;
	vector<unsigned> found;

	cout << "Please input a string of text: \n";
	getline(cin, str);
	cout << "Please enter a character to find in this string: ";
	cin >> findChar;
	findX(str, found, idx, pos, findChar);		// Call recusrive function
}


EdIT* : Also, is passing by reference this abundantly a bad thing?

This also feels for some reason, like overkill.
Last edited on
I also don't know why, if the letter being searched for is the first letter in the string entered, it won't find that first letter.
> It works good except there is a random very large number appearing in the output
> at the end after every search
Because you don't check that you have indeed found it, you are printing `std::string::npos'


Also
16:26: warning: comparison is always true due to limited range of data type [-Wtype-limits]


Also, I think there are too many parameters, pos and idx aren't needed

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
void findX (string s, vector<unsigned>& found, char& findChar)
{
    unsigned  pos = 0;

    unsigned len = found.size();

    if (len != 0)
    {
        pos = found[len-1] + 1;
    }

    size_t test = s.find(findChar, pos);

    if (test !=string::npos)
    {
        found.push_back (test);
        cout << findChar << " is " <<  test << " places into the string." << endl;
        findX(s, found, findChar);        // Recursion
    }
}

int main()
{
    char findChar = ' ';
    string str = " ";

    vector<unsigned> found;

    cout << "Please input a string of text: \n";
    getline(cin, str);
    cout << "Please enter a character to find in this string: ";
    cin >> findChar;
    findX(str, found, findChar);        // Call recusrive function
}
Last edited on
Thanks Chervil and ne555. I moved the message into the test check conditional and removed pos. This works great, but I still don't see the benefit of using recursion over loops. Perhaps when i have more complicated applications.
Topic archived. No new replies allowed.