Recursion?

I am trying to write a section of a program that generates names of 3 letters. First letter a capitol consonant, a second letter that is a vowel, the third that is a lowercase consonant.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char genFirst(){
char c = (rand()%26)+65;
if (c == 97 || c == 101 || c == 105 || c == 111 || c ==117){
	genFirst();
}
return c;
}
char genVowel(){
char c = (rand()%26)+97;
if (c == 97 || c == 101 || c == 105 || c == 111 || c ==117){
	return c;
}
	genVowel();
}
char genLast(){
char c = (rand()%26)+97;
if (c != 97 || c != 101 || c != 105 || c != 111 || c != 117){
	return c;
}
	genLast();
}


This code dosnt work.
Why?
Wouldnt the function call inside of itself, in the event that if statement conditions are or are not being met make the function eventually return what I want? Obviously it's not. I want to know why. Any help would be great. Thanks in advance.
what is the point of calling the function instead of looping in your code?
check this out and adjust it cuz int check it...

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
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

char genFirst()
{
	char c = NULL;

	do{
		c = (rand()%26)+65;

	}while(c == 97 || c == 101 || c == 105 || c == 111 || c ==117);

	return c;
}

char genVowel()
{
	char c = NULL;

	do{
		c = (rand()%26)+97;

	}while(c != 97 || c != 101 || c != 105 || c != 111 || c !=117);
	
	return c;
}
char genLast()
{
	char c = NULL;

	do{
		c = (rand()%26)+97;

	}while(c == 97 || c == 101 || c == 105 || c == 111 || c ==117);
	
	return c;

}

int main()
{
	genFirst();
    return 0;
}
I was just trying to see if I understood recursion but apparently I do not.

Loops are the best way to go?
http://danzig.jct.ac.il/cpp/recursion.html this a good tutorial
however have you tried to check that using IDA?
Last edited on
Ah! So I cant use recursion because I need to return something and it has to be re-generated anytime the if statement fails?
EDIT: Nvm I figured it out.
Last edited on
Topic archived. No new replies allowed.