Question regarding string.append()

Hello!

I need to bug the community once more.
I have this bit of code, below.

I'm trying to append a string (chance) to a second string (triedLetters), previously reducing the size of chance to 1.
"triedLetters" is meant to chain every "chance".

While the compiler does not complain, when I run the program, when it should cout<<triedLetters, it only outputs the "chance" from that cycle.



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

void dataForPlayer2 (string guessedLetters, string triedLetters)
{
    cout<<""<<endl;
    cout<<guessedLetters<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<"Tried and failed letters: "<<triedLetters<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
}



int wordGuessingCycle (int availableRounds,int &spentRounds, int&remainingRounds,string player1,string player2, string wordToGuess,string &guessedLetters, bool &endMatch, int &points1, int &points2, int winner)
{
char letterToLookUp;
string triedLetters, chance;
bool found=false;

...
cout<<"Input word"<<endl;
cin>>chance;
if (chance.size()!=1)
    {
        revision(wordToGuess,player1,points1,player2,endMatch,points2,remainingRounds,avavailableRounds,winner,chance);
    }
else
    {
        letterToLookUp = chance[0];
  
        unsigned int position=0;
        for (position=0;position<=(wordToGuess.size()+1);position++)
        {
            if (wordToGuess[position]==letterToLookUp)
            {
                ...
            }


            if ((position>wordToGuess.size())&&(not found))
            {
                    cout<<"Letter not present."<<endl;
                    spentRounds++;
                    triedLetters.append(chance);
                    dataForPlayer2(guessedLetters,triedLetters);
            }
        }
        ...
    };


return 0;


EDIT: changed entirely to english.
Last edited on
You probably should choose a better name than try since it is a keyword in c++. As far as your code I can't really help since it is in a foreign language. Also, you didn't provide minimal compilable code. Last thing, if it is not appending maybe the if statement is not being called on line 42 or "try" is empty.
Posting another bit of code.
I think I might've axed a bit too much, but it still repeats the error, now is in english, and compiles. It doesn't do what it has to - if it did I'd have to copy over 100 extra lines. This is an overly simplified version of it.


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
#include <iostream>

using namespace std;


void dataForPlayer2 (string guessedLetters, string triedLetters)
{
    cout<<""<<endl;
    cout<<guessedLetters<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<"Tried and failed letters: "<<triedLetters<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
}



int wordGuessingCycle (string player1,string player2, string wordToGuess,string &guessedLetters)
{
char letterToLookUp;
string triedLetters="", chance;
bool found=false;


cout<<"Try to guess the hidden word (one char at a time)"<<endl;
cin>>chance;
if (chance.size()==1)
    {
        letterToLookUp = chance[0];
        unsigned int position;
        for (position=0;position<=(wordToGuess.size()+1);position++)
        {
           
           if (wordToGuess[position]==letterToLookUp)
            {
                guessedLetters[position]=letterToLookUp;
            }

           if ((position>wordToGuess.size())&&(not found))
            {
                    cout<<"Letter not present."<<endl;
                    triedLetters.append(chance);            //this is the line I've got problems with
                    dataForPlayer2(guessedLetters,triedLetters);
            }
        }

    };


return 0;
}

int main()
{
    string player1,player2,wordToGuess,guessedLetters="";
    int i;

    cout << "input player 1" << endl;
    cin>>player1;
    cout << "input player 1" << endl;
    cin>>player2;
    cout << "input word to guess" << endl;
    cin>>wordToGuess;
    for (i=0;i<=wordToGuess.size();i++)
    {guessedLetters.append("*");}
    while (guessedLetters!=wordToGuess)
    {
        wordGuessingCycle(player1,player2,wordToGuess,guessedLetters);
    }

    return 0;
}
Last edited on
well line 43 should be chance[0].
Also the tried letters are reset each time. (line 22)

If I understand you correctly you are trying to do this:

1) input a word to have other player guess
2) guess a letter
3) if the word contains the letter display it otherwise add to invalid letter list
4) if the word is not discovered return to step 2
5) hooray word was guessed


You could try something like this (I used std::set instead of appending to string and searching all the letters) It's not very neat either sorry busy right now.

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
#include <iostream> //std::cout, std::cin, std::endl
#include <string>   //std::string
#include <set>      //std::set<T>

void getLetter(std::string const &wordToBeGuessed, std::string &discoveredLetters, std::set<char> &guessedLetters);

int main()
{
    std::string wordToBeGuessed;
    std::cout << "Please enter a word to be guessed: ";
    std::cin >> wordToBeGuessed;

    std::string discoveredLetters(wordToBeGuessed.size(), '*');
    std::set<char> guessedLetters;
    unsigned guessesTaken = 0u;

    while(discoveredLetters != wordToBeGuessed)
    {
        std::cout << "Disocvered letters: " << discoveredLetters << std::endl;
        getLetter(wordToBeGuessed, discoveredLetters, guessedLetters);
        ++guessesTaken;
    }

    std::cout << "To guess the word '" << wordToBeGuessed << "' it took " << guessesTaken << " guesses." << std::endl;
    std::cout << "The invalid guesses were: ";
    for(const auto &it: guessedLetters) std::cout << it << ' ';
}

void getLetter(std::string const &wordToBeGuessed, std::string &discoveredLetters, std::set<char> &guessedLetters)
{
    char guessedLetter;
    std::cout << "Please enter your guess(1 character): ";
    std::cin >> guessedLetter;

    std::size_t pos = 0u;
    if(guessedLetters.find(guessedLetter) == guessedLetters.end() && (pos = wordToBeGuessed.find(guessedLetter)) != std::string::npos)
    {
        while((discoveredLetters[pos++] = guessedLetter) && (pos = wordToBeGuessed.find(guessedLetter, pos)) != std::string::npos);
    }
    else
    {
        guessedLetters.insert(guessedLetter);
    }
}
Please enter a word to be guessed: giblit
Discovered Letters: ******
Please enter your guess(1 character): g
Discovered Letters: g*****
Please enter your guess(1 character): i
Discovered Letters: gi**i*
Please enter your guess(1 character): a
Discovered Letters: gi**i*
Please enter your guess(1 character): b
Discovered Letters: gib*i*
Please enter your guess(1 character): c
Discovered Letters: gib*i*
Please enter your guess(1 character): l
Discovered Letters: gibli*
Please enter your guess(1 character): t
To guess the word 'giblit' it took 7 guesses.
The invalid guesses were: a c
Thanks for responding.

Oh, sorry, I must've missed it.
Close guess!

What I'm doing is basically a hangman game.

The routine had to go like this:

There are three strings.

user 1 inputs a word (str1)
output is a string of * of the same length as user1word
user 2 inputs (str2) either
a) a string, in which case the program compares if it's equal to user1word. If it is, user 2 wins, if not, user 1 wins.

b) a char, in which case the program still requires a string, but axes it to the first character only. Then the program checks if the letter is in user1word.

if it is
replace the corresponding * for the letter user 2 just put
output string of * with the replaced char (like, for example ***a** if user 2 put an "a")
if it's not
output "letter not found" and the program should append the letter that wasn't found to (str3) triedLetters.



This is all in a cycle that limits the amount of times user 2 can place a letter. Every time he misses a letter, triedLetters should be appended with that, and it should be written to the screen.


As for your suggestion, I must have erased the [0] when translating. It is in my code.

I'll check the code you sent me.
Last edited on
Topic archived. No new replies allowed.