Looping two functions

I am having trouble looping two functions as shown below... It checks vowels fine, but after it finds out that there is an repeated letter, it ask u to enter another letter. At that point you can enter a vowel inside. But I want to make the program to go back to part where it checks the vowel again. How can I do that? I tried making a while loop to repeat it but when I did it, it kept on looping on the cin >> letter command instead.


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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <cstdlib>
#include <cmath>
#include <string>
#include <iostream>
#include <cstring>
#include <vector>
#include <ctime>

using namespace std;

int main()
{
    int test = 0;
    string phrase, sentence;
    std::vector<char> guesses;

    srand (time(NULL));
    test = rand() % 4 + 1;
    cout << "#" << test << endl;

    switch(test) {
    case 1:
        phrase = "thing";
        sentence = "computer science";
        break;

    case 2:
        phrase = "Subject";
        sentence = "math and science";
        break;

    case 3:
        phrase = "Subject";
        sentence = "pony and unicorn";
        break;

    case 4:
        phrase = "Subject";
        sentence = "dinosaurs and rhino";
        break;
    }

    cout << "The phrase is..." << phrase << endl;
    cout << "Here is your sentence..." << sentence << endl;

    int length;
    length = sentence.length();
    char letter;
    int arysize[length];
    string rounds = "";
    string again = "";
    int wordsCorrect;
    int wordsCount;
    bool letterCheck = false;
    int size;


    for(int z = 0; z < length; z++) {
        arysize[z] = 0;
    }

    int count = 0;

    while(count != 10) {
        cout << "Type S (to spin wheel), P(to guess word), or V(to buy a vowel)" << endl;
        char option;

        while(cin) {
            cin >> option;
            if(option == 'S' || option == 'P' || option == 'V')
                break;
            else {
                cout << "You have to enter either S, P or V." << endl;
            }
        }

        switch(option) {

        case 'S': {
            letter = 0;
            cout << "Enter a letter" << endl;
            wordsCount = 0;
            wordsCorrect = 0;

            letterCheck = false;
            cin >> letter;
//********************************************************************************************
//NEEDS TO BE LOOPED
            while(letterCheck != true) {
                if(letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u') {
                    letterCheck = false;
                    cout << "You cannot enter a vowel. Please enter a consonant." << endl;
                    cin >> letter;
                } else {
                    letterCheck = true;
                    guesses.push_back(letter);
                }
            }

            size = guesses.size();

            if(size > 1) {
                letterCheck = false;
                while(letterCheck != true) {
                    for(int y = 0; y < size-1 ; y++) {

                        if(letter == guesses[y]) {
                            letterCheck = false;
                            guesses.pop_back();
                            cout << "You cannot enter the same letter twice. Please try a different letter." << endl;
                            cin >> letter;
                            guesses.push_back(letter);
                        } else if(y == size-2) {
                            letterCheck = true;
                        }
                    }
                }
            }
//**********************************************************************************************

            if(letterCheck == true) {
                for(int j = 0; j < length; j++) {
                    if(sentence[j] == letter) {
                        arysize[j] = 1;
                        wordsCorrect++;
                    } else if (sentence[j] == ' ')
                        arysize[j] = 1;
                }

                for (int m = 0; m < length; m++) {
                    if(arysize[m] == 1) {
                        cout << sentence[m];
                    } else
                        cout << "_";
                }

                cout << "\nThe amount of words correct is... " << wordsCorrect << endl;

                for(int a = 0; a < length; a++) {
                    if(arysize[a] == 1)
                        wordsCount++;
                }

                if(wordsCount == length) {
                    cout << "You win this round!" << endl;
                    break;
                }

                count++;
                cout << "\nYou have " << 10 - count << " tries left." << endl;

                for(int asd = 0; asd < size; asd++) {
                    cout << "For #" << asd << " = " << guesses[asd] << endl;
                }



                break;
            }
            break;
        }

        case 'P': {
            string guessPhrase = "";

            cout << "Enter your guess here." << endl;
            cin.ignore();
            getline(cin,guessPhrase);
            if(sentence == guessPhrase) {
                cout << "You win!" << endl;
                int temp = (10 - count);
                count += temp;
            } else {
                count++;
                cout << "You lose all your money." << endl;
                cout << "\nYou have " << 10 - count << " tries left." << endl;
                break;
            }
            break;
        }

        case 'V': {

            letter = 0;
            cout << "Enter a vowel (A, E, I, O, U)." << endl;
            letterCheck = false;
            wordsCount = 0;
            wordsCorrect = 0;

            while(letterCheck != true) {
                cin >> letter;
                if(letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u' ) {
                    guesses.push_back(letter);
                    letterCheck = true;
                } else {
                    cout << "You must enter a vowel (A, E, I, O, U)." << endl;
                }

            }
            size = guesses.size();

            if(size > 1) {
                letterCheck = false;
                while(letterCheck != true) {
                    for(int y = 0; y < size-1 ; y++) {

                        if(letter == guesses[y]) {
                            letterCheck = false;
                            guesses.pop_back();
                            cout << "You cannot enter the same letter twice. Please try a different letter." << endl;
                            cin >> letter;
                            guesses.push_back(letter);
                        } else if(y == size-2) {
                            letterCheck = true;
                        }
                    }
                }
            }

            if(letterCheck == true) {
                for(int j = 0; j < length; j++) {
                    if(sentence[j] == letter) {
                        arysize[j] = 1;
                        wordsCorrect++;
                    } else if (sentence[j] == ' ')
                        arysize[j] = 1;
                }

                for (int m = 0; m < length; m++) {
                    if(arysize[m] == 1) {
                        cout << sentence[m];
                    } else
                        cout << "_";
                }

                cout << "\nThe amount of words correct is... " << wordsCorrect << endl;

                for(int a = 0; a < length; a++) {
                    if(arysize[a] == 1)
                        wordsCount++;
                }

                if(wordsCount == length) {
                    cout << "You win this round!" << endl;
                    break;
                }

                count++;
                cout << "\nYou have " << 10 - count << " tries left." << endl;

                for (int asd = 0; asd < size; asd++) {
                    cout << "For #" << asd << " = " << guesses[asd] << endl;
                }
                break;
            }


            break;
        }
        }
    }
}
Last edited on
Using a function would be the easiest way to do this, otherwise your code is just going to get more confusing. Functions break your code down into smaller chunks which makes it easier to manage, troubleshoot and reuse.

This site has information on functions
http://www.cplusplus.com/doc/tutorial/functions/

Also a google search on "introduction to c++ user defined functions" will give you plenty of information.

Using this you should be able to come up with a function like
bool isVowel(char letter)

Which you could use to return a true if it is a vowel and false if it is not.
May i ask what exactly does this program do or the instructions if any ?
Well, I am trying to create a custom wheel of fortune game. Not fully complete, only problem is where i marked it needs to be looped.
As said above, whenever I pass the vowel check and fail the duplicate I can enter a vowel right after.
Topic archived. No new replies allowed.