c++ - String not being printed/initialized properly -


I have been working on a hangman game in CPP ... then I created a variable called which initially stores the dash, but is slowly open. i <+>;

Therefore, I try to initialize SoFar to include the dash, and TheWord

Later, when I print sofar, it is just empty

  of the court  

Output display picture

Any and all advice Is appreciated. Here's my full program for reference:

  #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

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -