How to check if a guess matches an index of array or just a value

Working on an assignment and stuck on this one portion. So what I need to do is if a char of a guess doesn't match the given word print a "_", if that guess char matches the position of the char in the given word print an "o", if the guess char matches but isn't in the same position print an "x"

How do I go about doing this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
      for(int i = 0; i < guess.length(); i++)
    {
            if(guessed[i] != guess[i])
            {
                cout << "_";
            }
            else if(guessed[i] == guess[i])
            {
                cout << "o";
            }
            else if(guessed[i] == guess[i])
            {
                cout << "x";
            }
    }


Here is an example output (I have everything printing out for testing)
1
2
3
4
5
6
7
8
9
15
53 command
mnaomdc
Your code is 7 characters long
Please make your guess or enter # to stop: commadc
You have guessed 1 times
You entered commadc
That is not the code word.
_____ooYour code is 7 characters long
Last edited on
you can get clever with it later. to start out, just
loop every letter in the input against every letter in the code word, a double for loop.

-what if the letter IS an x?
maybe mark those with a ? or other invalid character assuming you want to show the ones they get correct?

so what I would do for brute force is just make an output string of all underscores the size of the code word.
loop all the letters as I said. if you match position and value, the output at that position is set to the value. if you match value but not position, mark to ? or some symbol, IF it was not already marked a letter (if so, leave it alone).

doing that you get
mnaomdc
?_???dc
or if you insist on hiding what they know:
?_???oo
or you can use the x and o thing, I just felt it would be nice to show what they got right.
Last edited on
Not important but to answer your question on the x's and o's thing I just have to follow the assignment.

Okay I think I understand I did try the double loop thing but couldn't figure out how to not have it print out both the x's and o's.

For the output string how would I make that so it is the length of "guess.length()" since the word lengths vary?

- One thing I did try is creating an array with "string check[guess.length()]" but I guess arrays can't have their length set by comparison.
Last edited on
string s(guess.length(), '_');
no, arrays must have a compile time constant size. nonstandard language compilers allow this frequently. It is best not to use that feature, though.
vector will work, but you don't need a vector of strings. a vector of char:
vector<char> c(guess.length());
but you don't need it...

the output length should be of the real code word's size regardless of the input.
Last edited on
Okay that helped regarding getting a separate string.

I can't seem to figure out where to put the other for loop so that it can check if it's the same position or just value. This is how far I've gotten but just comes out to x's when I type in the correct letters and I never get o's.

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
    string check(guess.length(), '_');
    
    cout << "You have guessed " << counter << " times" << endl;
    cout << "You entered " << guessed << endl;
    cout << "That is not the code word." << endl;
    
    for(int i = 0; i < guess.length(); i++)
    {
        if(guessed[i] != guess[i])
        {
            check[i] = '_';
        }
        if(guessed[i] == guess[i])
        {
            check[i] = 'o';
        }
        
        for(int j = 0; j < guess.length(); j++)
        {
            if(guessed[i] == guess[j])
            {
                check[j] = 'x';
            }
        }

    }
the two loops work together (its actually possible to do it with a lot less work, but keeping it simple for now)

for(i = all the letters in the real word)
for(j = all the letters in the guess)
{
if i == j and letter==letter then its a position and a value match, that is 'o'
else if letter==letter, its a value match, wrong position, mark 'x' if not already marked 'o'
else not a match, do nothing.
}

I think that is what you want. ?
Last edited on
Yes thanks so much but now I'm running into a few issues

1. I need a total for each type found (val & pos) but it doesn't seem to be resetting even though logically it would seem to, and sometimes just shoots random stuff

2. Is there a way to reset the string of blanks (string check(guess.length(), '_')) Because if any o's or x's are found it seems stuck that way

3. The blank spaces aren't being skipped if not found

This is where I'm at currently anything helps!

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
void guessWord(string& guess, string& guessed, string list[], int& word, int& counter)
{
    
    cout << "Your code is " << guess.length() << " characters long" << endl;
    cout << "Please make your guess or enter # to stop: ";
    cin >> guessed;
    
    while(guessed == "#" || guessed.length() != guess.length())
    {
        if(guessed == "#")
        {
            fail(list, word, guess, guessed, counter);
        }
        else
        {
            cout << "The guess must be the same number of characters as your code word." << endl;
            cin.clear();
    		cin.ignore(1000, '\n');
        }
        
        cout << "Your code is " << guess.length() << " characters long" << endl; 
        cout << "Please make your guess or enter # to stop: ";
        cin >> guessed;
    }
    
    if(guessed != guess)
    {
        wrong(guess, guessed, counter);
    }
    while(guessed == guess)
    {
        right(list, word, guess, guessed, counter);
    }
}

void wrong(string& guess, string& guessed, int& counter)
{
    counter = counter + 1;
    string check(guess.length(), '_');
    int pos = 0;
    int val = 0;
    
    cout << "You have guessed " << counter << " times" << endl;
    cout << "You entered " << guessed << endl;
    cout << "That is not the code word." << endl;
    
    for(int i = 0; i < guess.length(); i++)
    {
        for(int j = 0; j < guess.length(); j++)
        {
            if(guessed[j] == guess[j] && i == j)
            {
                check[j] = 'x';
                pos++;
            }
            else if(guessed[j] == guess[j] && i != j)
            {
                check[i] = 'o';
                val++;
            }
            else
            {
                continue;
            }

        }
        
    }
    
    cout << "You have " << pos << " characters in the right position, and you have " << val << " correct characters in the wrong position." << endl;
    cout << "Comparison: ";
    
    for(int i = 0; i < guess.length(); i++)
    {
        cout << check[i];
    }
    cout << endl;
    
}
Last edited on
pos and val reset every time you call wrong.
if they have unexpected values, you have a bug.

yes, you can reset your string.
http://www.cplusplus.com/reference/algorithm/fill/
or just loop over it and assign each character yourself, that is all fill will do

3) not sure what blank spaces you are talking about. you used cin which does not respect whitespace.


Last edited on
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 <string>

int main()
{
    // INPUT
    std::string guess{"coxmacd"};
    std::string word{ "command"};

    // VERIFY LENGTHS MATCH
    size_t word_size = word.length();
    if( guess.length() != word_size )
    {
        std::cout << "Lengths mismatch\n";
        return -1;
    }

    std::string answer = std::string(word_size, '_'); // MISMATCH '-'

    size_t pos{0};
    for(int i = 0; i < word_size; i++)
    {
        // MATCHES AND IN CORRECT POSITION 'x'
        if(guess[i] == word[i] and answer[i] == '_')
        {
            answer[i] = 'x'; // COUNT TO DECIDE ON SUCCESS
        }

        // MATCHES BUT IN WRONG POSITION 'o'
        for (
             pos = 0;
             (pos = word.find_first_of(guess[i], pos)) != std::string::npos;
             ++pos
             )
        {
            if(pos != i and answer[i] == '_')
            {
                answer[i] = 'o';
            }
        }
    }

    std::cout << answer << '\n';

    return 0;
}



xx_xxox
Program ended with exit code: 0
Solved it thank you so much!
Topic archived. No new replies allowed.