do while loop needs help.

Need help with it.
The while loop for play again doesn't work.
I am not sure what is the reason that caused the error, any suggestion will be helped.
The idea is to make this program running again once the selection of play again is Y.
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

const int MAX_TRIES = 5;
int letterFill(char, string, string&);
bool again = false;
char Y,N, selection;
int level;

int main(){
    
    string name;
	char letter;
	int num_of_wrong_guesses = 0;
	string word;

	srand(time(NULL));
do{    
    
    do{
    cout << "Please enter the difficulty level (1-3):" << endl;
	cin >> level;
    }
    while( level > 3);
    
  
	 if (level == 1)
    {
        string easy[] = { "fun", "car", "test", "book", "class"};
        string word;
        int n = rand() % 4;
        word = easy [n];
        string unknown(word.length(), '-');
        
        while (num_of_wrong_guesses < MAX_TRIES)
        {
            cout << unknown << endl;
            cout << "Enter a letter :" << endl;
            cin >> letter;
            
                if(letterFill(letter, word, unknown) == 0)
                {
                    cout << "Wrong guesses :(" << num_of_wrong_guesses + 1 << ")" << letter << endl;
                    num_of_wrong_guesses ++;
                }
                
                if (word == unknown)
                {
                    cout << "You have Won!" << endl;
                    break;
                }
            if (num_of_wrong_guesses == MAX_TRIES)
            {
                cout << "You have lost!" << endl;
            }

        }
    }
    
    else if( level == 2)
    {
        string average [] = { "mouse" , "notepad", "keyboard", "computer", "blackboard" };
        int n = rand () % 10;
        word = average[n];
        
        string unknown(word.length(), '-');
        
        while (num_of_wrong_guesses < MAX_TRIES)
        {
            cout << unknown << endl;
            cout << "Enter a letter :" << endl;
            cin >> letter;
            
            if(letterFill(letter, word, unknown) == 0)
            {
                cout << endl << "Wrong guesses : (" << num_of_wrong_guesses + 1 << ")" << letter << endl;
				num_of_wrong_guesses ++;
            }
            
            if (word == unknown)
            {
                cout << "You have WON!" << endl;
                break;
            }
            
            if(num_of_wrong_guesses == MAX_TRIES)
            {
                cout << "You have lost!" << endl;
            }
        }
    }
    else if( level == 3)
    {
        string hard[] = {"kayak", "oxygen", "awkward", "memento", "bagpipes"};
        
        int n = rand() % 10;
        word = hard[n];
        
        string unknown(word.length(), '-');
        
        while (num_of_wrong_guesses < MAX_TRIES)
        {
            cout << unknown << endl;
            cout << "Enter a letter :" << endl;
            cin >> letter;
            
            if(letterFill(letter, word, unknown) == 0)
            {
                cout << endl << "Wrong guesses : (" << num_of_wrong_guesses + 1 << ")" << letter << endl;
				num_of_wrong_guesses ++;
            }
            
            if (word == unknown)
            {
                cout << "You have WON!" << endl;
                break;
            }
            
            if(num_of_wrong_guesses == MAX_TRIES)
            {
                cout << "You have lost!" << endl;
            }
        }
    }
    return 0;
    
    cout << "Play again? Y/N" << endl;
    cin >> selection;
    if(selection == 'Y')
    {
        again = true;
    }
    else 
    {
        again = false;
        return 0;
    }
    
}while(again == true);




int letterFill(char guess, string secretword, string &guessword)
{
	int i;
	int matches = 0;
	int len = secretword.length();
	for (i = 0; i< len; i++)
	{

		if (guess == guessword[i])
			return 0;

		if (guess == secretword[i])
		{
			guessword[i] = guess;
			matches++;
		}
	}
	return matches;
}



return 0;

Your main function finishes before the end of the while loop.
Hello,
After moved the return 0 inside the if statement after the cout, it came out with another error.

if(num_of_wrong_guesses == MAX_TRIES)
{
cout << "You have lost!" << endl;
return 0;
}

In function 'int main()':
150:1: error: a function-definition is not allowed here before '{' token
167:1: error: expected '}' at end of input


There should be a closing brace for main after the while.
Duplicate of http://www.cplusplus.com/forum/beginner/234497/ . Please DON'T do that.
Topic archived. No new replies allowed.