ABanerjee
ABanerjee

Reputation: 195

String not being printed/initialized properly

I've been working on a Hangman game in Cpp... So I created a variable called SoFar which stores dashes in the starting, but is gradually uncovered.

for(i = 0; i <= TheWord.length(); i++)
            SoFar[i] = '-';

So, I (try to) initialize SoFar to contain dashes in the starting, and be the same length as TheWord

Later on, when I print SoFar, it's just empty!

cout << "\nSo far, the word is : " << SoFar << endl;

Picture showing output

Any and all advice is appreciated. Here is my full program, for reference:

#include <iostream>
 #include <cstdlib>
 #include <string>
 #include <vector>
 #include <ctime>
 #include <cctype>

using namespace std;

 int main()
 {

    vector<string> words;
    words.push_back("SHAWARMA");
    words.push_back("PSUEDOCODE");
    words.push_back("BIRYANI");
    words.push_back("TROLLED");
    srand((unsigned)time(NULL));
    string TheWord = words[(rand() % words.size()) + 1];
    string SoFar;        
    const int MAXTRIES = 8;
    string used = "";
    int tries, i;
    i = tries = 0;
    char guess;
        for(i = 0; i <= TheWord.length(); i++)
            SoFar[i] = '-';

    while(tries <= MAXTRIES && SoFar != TheWord)
    {
        /****************************************************************/
        /*                          I/0                                 */
        /****************************************************************/
        cout << "\nYou haz " << MAXTRIES - tries << " tries to go!\n" ;
        cout << "You've used the following letters : ";

        for(i = 0; i <= used.length(); i++)
            cout << used[i] << " : " ;

        cout << "\nSo far, the word is : " << SoFar << endl;
        cout << "\nEnter your guess : " ;
        cin >> guess;
        /****************************************************************/
        /*                          Processing input                    */
        /****************************************************************/
        if(used.find(guess) != string::npos)
            continue;
        guess = toupper(guess);
        if(TheWord.find(guess) != string::npos)
        {
            for(i = 0; i <= TheWord.length(); i++)
            {
                if(guess == TheWord[i])
                    SoFar[i] = guess;
            }

        }
        else
        {
            cout << "\nSorry, but the word doesn't have a letter like " << guess << " in it...\n";
            tries++;
        }   
                used += guess;

    }

    if(tries == MAXTRIES)
        cout << "\nYep, you've been hanged...\n";
    else if(SoFar == TheWord)
        cout << "\nYou got it! Congratulations...\n(Now Im gonna add some psuedorandomly generated words just for you <3 :P)";

    cout << "\nThe word was : " << TheWord;

    return 0;
 }

Upvotes: 0

Views: 102

Answers (3)

Balaji Kondalrayal
Balaji Kondalrayal

Reputation: 1751

you have specified that the soFar as string so try using double quotes for value

for(i = 0; i <= TheWord.length(); i++)
SoFar[i] = "-";

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490108

You define SoFar as:

string SoFar;        

...then you try to write to it:

    for(i = 0; i <= TheWord.length(); i++)
        SoFar[i] = '-';

When you write to it, SoFar still has a length of 0, so every time you execute SoFar[i] = '-';, you get undefined behavior.

Try:

std::string SoFar(TheWord.length(), '-');

This defines SoFar already containing the right number of dashes.

Here's a quick demo:

#include <string>
#include <iostream>

int main(){ 
    std::string TheWord{"TROLLED"};

    std::string SoFar(TheWord.length(), '-');

    std::cout << TheWord << "\n";
    std::cout << SoFar << "\n";
}

At least for me, this seems to produce the correct length of result:

TROLLED
-------

Upvotes: 2

David Schwartz
David Schwartz

Reputation: 182753

You're trying to modify objects that don't exist:

string SoFar;        
const int MAXTRIES = 8;
string used = "";
int tries, i;
i = tries = 0;
char guess;
    for(i = 0; i <= TheWord.length(); i++)
        SoFar[i] = '-';

Since SoFar is empty, trying to modify SoFar[i] is undefined behavior.

Upvotes: 2

Related Questions