Guess Word Game - all vowels shown when guessed

Mar 5, 2012 at 10:06pm
Hey all, I have an assignment which I can run the guess game section, to perform from a file of words that end with *** as a sentinel, but the second part of "if a vowel is guessed right then all traces of that vowel is shown."

I'm looking to learn this material so any extra wording or ideas that would help me learn more would be highly appreciated!!!


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>
using std::cin;
using std::cout;

#include <fstream>
using std::ifstream;

#include <string>
using namespace std;

int main () {

    ifstream ifs_words("words.txt");
    string word;
    ifs_words >> word;

    while (word != "***") {
        string mask(word.length(), '.');
        int num_guesses =0;
        do {
            cout << mask << "\n\nLetter? ";
            char letter = '-';
            cin >> letter;
            cout << "\n";
            ++num_guesses;
            for (int i=0; i < word.length(); ++i)
                if (word[i] == letter) mask[i] = letter;
        } while (word != mask && num_guesses < 5);

        if (word == mask) {
            cout << mask << "\n\nCongrats!"
                 << " You found the word with only "
                 << num_guesses << " guesses!\n\n";
        } else {
            cout << mask << "\n\nYou must spell the entire word now:";
            string spell;
            cin >> spell;
            if (spell == word)
                cout << "Got it!\n\n";
            else
                cout << "Sorry, not it.\n\n";
        }

        ifs_words >> word;
    }

    cout << "There are no more words. Restart to play again!";

    ifs_words.close();
    return 0;
}
Last edited on Mar 5, 2012 at 10:31pm
Mar 5, 2012 at 10:27pm
please put the [code][/code] tags around your script, impossible to read otherwise
Mar 5, 2012 at 10:31pm
Thanks..added the tags.
Mar 5, 2012 at 10:54pm
I dont see any problems with this code, it looks good... could you elaborate on any issues you are having with it?
Mar 5, 2012 at 11:28pm
if the word is "alpha" (***** to the user)

and the user guesses a

then the word should appear as "a***a"

That's what I understand from his post.
Mar 6, 2012 at 12:23am
closed account (zvRX92yv)
Use an array of booleans? :P

Use a loop when outputting the array, if n slot has been answered then output the char else output an asterisk.

Same thing with checking if entered char is in the word; loop through it.
Last edited on Mar 6, 2012 at 12:30am
Mar 6, 2012 at 2:45am
Ok.

(1) The question being asked more clearly is: how do I write a statement so that when a player guesses a vowel, all vowels of that particular letter in the word show up?

(2) I haven't seen an array of booleans yet in my studies. Is this the way to start?
Mar 6, 2012 at 3:12am
closed account (zwA4jE8b)
The program works fine for me. At least it does what you are asking, I hard-coded in string word = "alpha"; and when I guess the letter 'a' a***a shows up.
Mar 6, 2012 at 10:13pm
If you try the word "banana" and enter the letter "n", does it produce all the "n"s in the word?

Hmmmm...
Topic archived. No new replies allowed.