Repeating the program?

I'm working on a madlib program for my class, and I'm trying to code a specific part of the program which will basically repeat the entire program depending on the users input.

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 <fstream>
using namespace std;

void getFileName(char fileName[]) //Implemented Correctly
{
	cout << "Please enter the filename of the Mad Lib: ";
	cin.getline(fileName, 256);
	
	return;
}

void playAgain()
{
	char yesOrNo;
	
	cout << "Do you want to play again (y/n)? ";
	cin >> yesOrNo;
	
	if (yesOrNo == 'n')
	{	
		cout << "Thank you for playing.";
		return;
	}
		
	if (yesOrNo == 'y')
		main();
		
	if (yesOrNo != 'y' || yesOrNo != 'n')
	{
		cout << "Invalid entry.  Do you want to play again (y\n)?";
		cin >> yesOrNo;
	}	
	
}

int main()
{
	char fileName[256];
	//char madLibStory[32][256];
	
	getFileName(fileName);
	
	ifstream readFile;
	
}


I've read before that you should never repeat main, I guess in this case I really don't know what else to do though. As you can see from my playAgain() function, the program asks the user to enter y or no to play again, and depending on that input, it will either end the program, or run it through the entire process again.

My playAgain function works for the no and invalid statements, but the yes statement is doing some funky stuff that I wasn't expecting. What would the "appropriate" way be to go about repeating the entire function?
You cannot call the main function, rather repeat that specific part of the function depending on the users input
Mutually recursive:
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
#include <iostream>
#include <string>

std::string get_file_name()
{
    std::string file_name ;
	std::cout << "Please enter the filename of the Mad Lib: ";
	std::cin >> file_name ;
    return file_name ;
}

int madlib_main() ;

int play_again()
{
    char yes_or_no ;
    std::cout << "Do you want to play again (y/n)? ";
    std::cin >> yes_or_no ;

    if( yes_or_no == 'y' || yes_or_no == 'Y' ) return madlib_main() ;

    else if( yes_or_no == 'n' || yes_or_no == 'N' ) return 0 ;

    else
    {
        std::cout << "Invalid entry. " ;
        return play_again() ;
    }
}

int madlib_main()
{
    {
        const std::string file_name = get_file_name() ;

        // read file

        // etc...
    }

    return play_again() ;
}



int main()
{
    // wecome message
    // any other one-time initialization eg. std::srand( std::time(nullptr) ) ;

    return madlib_main() ;
}



Recursive:
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
#include <iostream>
#include <string>

std::string get_file_name()
{
    std::string file_name ;
	std::cout << "Please enter the filename of the Mad Lib: ";
	std::cin >> file_name ;
    return file_name ;
}

int madlib_main() ;

bool play_again()
{
    char yes_or_no ;
    std::cout << "Do you want to play again (y/n)? ";
    std::cin >> yes_or_no ;

    if( yes_or_no == 'y' || yes_or_no == 'Y' ) return true ;

    else if( yes_or_no == 'n' || yes_or_no == 'N' ) return false ;

    else
    {
        std::cout << "Invalid entry. " ;
        return play_again() ;
    }
}

int madlib_main()
{
    {
        const std::string file_name = get_file_name() ;

        // read file

        // etc...
    }

    if( play_again() ) return madlib_main() ;
    else return 0 ;
}



int main()
{
    // wecome message
    // any other one-time initialization eg. std::srand( std::time(nullptr) ) ;

    return madlib_main() ;
}



Iterative:
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
#include <iostream>
#include <string>
#include <cctype>

std::string get_file_name()
{
    std::string file_name ;
	std::cout << "Please enter the filename of the Mad Lib: ";
	std::cin >> file_name ;
    return file_name ;
}

int madlib_main() ;

bool play_again()
{
    char yes_or_no ;
    do
    {
        std::cout << "Do you want to play again (y/n)? ";
        std::cin >> yes_or_no ;
        yes_or_no = std::tolower(yes_or_no) ;
    }
    while( yes_or_no != 'y' && yes_or_no != 'n' && std::cout << "Invalid entry. " ) ;

    return yes_or_no == 'y' ;
}

int madlib_main()
{
    do
    {
        const std::string file_name = get_file_name() ;

        // read file

        // etc...
    }
    while( play_again() ) ;

    return 0 ;
}



int main()
{
    // wecome message
    // any other one-time initialization eg. std::srand( std::time(nullptr) ) ;

    return madlib_main() ;
}
Topic archived. No new replies allowed.