c++ - String not being printed/initialized properly -
I have been working on a hangman game in CPP ... then I created a variable called Therefore, I try to initialize Later, when I print sofar, it is just empty Any and all advice Is appreciated. Here's my full program for reference: SoFar
to include the dash, and TheWord
of the court
#include
you defined SoFar
Like:
wire SoFar;
... then you try to write it:
< For pre (i = 0; i & lt; = TheWord.length (); i ++) SoFar [i] = '-';
when If you write it, then Suffer
still has a length of 0, so every time you execute SoFar [i] = '-';
, you are undefined Try it.
Try:
std :: string SoFar (TheWord.length (), '-');
It defines SoFar
already has the correct number of dashes.
Here's a quick demo:
#include & lt; String & gt; # Include & lt; Iostream & gt; Int main () {std :: string TheWord {"trolled"}; Std :: string SoFar (TheWord.length (), '-'); Std :: cout & lt; & Lt; The word & lt; & Lt; "\ N"; Std :: cout & lt; & Lt; Suffer & lt; & Lt; "\ N"; }
At least for me, it starts to produce the correct length results:
trolled -------
Comments
Post a Comment